WM_GETICON 不起作用(Windows)

Noi*_*art 2 c++ icons user32 firefox-addon jsctypes

如果我不首先使用 WM_SETICON 来设置图标,那么 WM_GETICON 总是返回 0。这很奇怪。请帮忙。

这是我的代码,可以复制粘贴到暂存器并运行。

当执行SendMessage(targetWindow_handle, WM_GETICON , ICON_SMALL, ctypes.voidptr_t(0)), hIconSmall_origandhIconBig_orig时总是返回 0 我不知道为什么。如果您首先在窗口上使用 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();
Run Code Online (Sandbox Code Playgroud)

nma*_*ier 6

既然你WM_GETICON从我这里得到了这些东西(在另一个问题的另一个答案中),让我首先说:已经有一段时间了......所以我忘记了WM_GETICON当窗口没有分配特定的窗口图标时会返回 null,但图标是从注册的窗口类中获取。

所以你应该:

  1. 检查WM_GETICON窗口是否分配了特定图标。
  2. 检查班级GetClassLongPtr(hwnd, GCLP_HICON /* or GCLP_HICONSM */)
  3. 如果失败,您可以随时尝试从.exe
  4. 如果失败,您可以随时尝试加载库存图标。

下面是一些 C++ 代码,我用它来实际从“mintrayr”扩展中的窗口获取图标:

  // 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);
  }
Run Code Online (Sandbox Code Playgroud)