c++ - WM_GETICON 不工作 (Windows)

标签 c++ firefox-addon icons user32 jsctypes

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

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

当执行 SendMessage(targetWindow_handle, WM_GETICON , ICON_SMALL, ctypes.voidptr_t(0)) 时,hIconSmall_orighIconBig_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,而是图标取自已注册的窗口类。

所以你应该:

  1. 检查 WM_GETICON 以查看窗口是否分配了特定图标。
  2. 通过GetClassLongPtr(hwnd, GCLP_HICON/* or GCLP_HICONSM */)检查类
  3. 如果失败,您可以随时尝试从 .exe
  4. 加载主图标
  5. 如果失败,您可以随时尝试加载股票图标。

这是一些 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/

相关文章:

c++ - 在 C++11 中为嵌套循环返回 vector 的最佳实践

c++ - 任何 Microsoft 库都使用 Windows SBCS 代码页中的非拉丁数字来表示 C 字符串中的数字数据吗?

javascript - Firefox 插件内的网站

html - fa-icon 内的文字

ios - 在 XCode 中将 PDF 用于 AppIcon(.appiconset 集合)

svg - 如何从集合中删除 icomoon 中的图标

c++ - 类模板方法特化

C++ : Array no been initialized when size is determined in runtime

firefox - Vimperator/Conkeror 类链接选择

javascript - 使用 document.execCommand ("paste"从剪贴板粘贴数据);在 Firefox 扩展中