如果我不首先使用 WM_SETICON 来设置图标,那么 WM_GETICON 总是返回 0。这很奇怪。请帮忙。
这是我的代码,可以复制粘贴到暂存器并运行。
当执行 SendMessage(targetWindow_handle, WM_GETICON , ICON_SMALL, ctypes.voidptr_t(0))
时,hIconSmall_orig
和 hIconBig_orig
总是返回 0 I不知道为什么。如果您首先在窗口上使用 WM_SETICON,那么它会正确获取 HICON,但整个目的是获取默认图标。
Cu.import('resource://gre/modules/ctypes.jsm');
var user32 = ctypes.open('user32.dll');
/* http://msdn.microsoft.com/en-us/library/windows/desktop/ms644950%28v=vs.85%29.aspx
* LRESULT WINAPI SendMessage(
* __in HWND hWnd,
* __in UINT Msg,
* __in WPARAM wParam,
* __in LPARAM lParam
* );
*/
var SendMessage = user32.declare('SendMessageW', ctypes.winapi_abi, ctypes.uintptr_t,
ctypes.voidptr_t,
ctypes.unsigned_int,
ctypes.int32_t,
ctypes.voidptr_t
);
var WM_GETICON = 0x007F;
var WM_SETICON = 0x0080;
var ICON_SMALL = 0;
var ICON_BIG = 1;
var ICON_SMALL2 = 2; //for use with WM_GETICON only, not applicable to WM_SETICON
// RUNNING STUFF BELOW - ABVOE WAS JUST DEFINING STUFF
var baseWindow = window.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIWebNavigation)
.QueryInterface(Ci.nsIDocShellTreeItem)
.treeOwner
.QueryInterface(Ci.nsIInterfaceRequestor)
.nsIBaseWindow;
var nativeHandle = baseWindow.nativeHandle;
var targetWindow_handle = ctypes.voidptr_t(ctypes.UInt64(nativeHandle));
var hIconSmall_orig = SendMessage(targetWindow_handle, WM_GETICON , ICON_SMALL, ctypes.voidptr_t(0));
var hIconBig_orig = SendMessage(targetWindow_handle, WM_GETICON , ICON_BIG, ctypes.voidptr_t(0));
Services.wm.getMostRecentWindow(null).alert('hIconSmall_orig = ' + hIconSmall_orig + '\nhIconBig_orig = ' + hIconBig_orig);
user32.close();
最佳答案
因为你从我那里得到了 WM_GETICON
东西(在另一个问题的另一个答案中)让我首先说:已经有一段时间了......所以我忘记了 WM_GETICON
当窗口没有分配特定的窗口图标时将返回 null,而是图标取自已注册的窗口类。
所以你应该:
- 检查
WM_GETICON
以查看窗口是否分配了特定图标。 - 通过
GetClassLongPtr(hwnd, GCLP_HICON/* or GCLP_HICONSM */)
检查类 - 如果失败,您可以随时尝试从
.exe
加载主图标
- 如果失败,您可以随时尝试加载股票图标。
这是一些 C++ 代码,我用它来实际从我的 "mintrayr" extension 中的窗口获取图标。 :
// Get the window icon
HICON icon = reinterpret_cast<HICON>(::SendMessageW(hwnd, WM_GETICON, ICON_SMALL, 0));
if (icon == 0) {
// Alternative method. Get from the window class
icon = reinterpret_cast<HICON>(::GetClassLongPtrW(hwnd, GCLP_HICONSM));
}
// Alternative method: get the first icon from the main module (executable image of the process)
if (icon == 0) {
icon = ::LoadIcon(GetModuleHandleW(0), MAKEINTRESOURCE(0));
}
// Alternative method. Use OS default icon
if (icon == 0) {
icon = ::LoadIcon(0, IDI_APPLICATION);
}
关于c++ - WM_GETICON 不工作 (Windows),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24047470/