WM_GETICON not working (Windows)

WM_GETICON not working (Windows)

本文关键字:Windows working not GETICON WM      更新时间:2023-10-16

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

这是我的代码,可以复制粘贴到记事本上运行。

当做SendMessage(targetWindow_handle, WM_GETICON , ICON_SMALL, ctypes.voidptr_t(0)), hIconSmall_orighIconBig_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();

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

所以你应该:

  1. 检查WM_GETICON,查看窗口是否有指定的特定图标。
  2. 检查GetClassLongPtr(hwnd, GCLP_HICON /* or GCLP_HICONSM */)的类
  3. 如果失败,你可以尝试从.exe
  4. 加载主图标
  5. 如果失败,你可以尝试加载一个股票图标。

下面是一些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);
  }

有一种非常简单(而且是跨平台的)的方法来检索Firefox的默认图标。

var foxexe = FileUtils.getFile("XREExeF", []);
var iconurl = "moz-icon:" + Services.io.newFileURI(foxexe).spec;

您可以像对待任何其他指向图像的url一样对待iconurl。默认大小为16x16,添加?size=32以获得更大的图标。似乎这两个值是Windows上唯一支持的。对于其他操作系统可能不是这样。