WM_GETICON not working (Windows)

痴心易碎 提交于 2019-12-02 12:01:49

Since you got the WM_GETICON stuff from me (in another answer to another question) let me first say: It has been a while... So I forgot that WM_GETICON will return null when the window has no particular window icon assigned, but instead the icon is taken from the registered window class.

So you should:

  1. Check WM_GETICON to see if the window has a specific icon assigned.
  2. Check the class by GetClassLongPtr(hwnd, GCLP_HICON /* or GCLP_HICONSM */)
  3. If that fails, you can always try to load the main icon from the .exe
  4. If that fails, you can always try to load a stock icon.

Here is some C++ code which I use to actually get an icon from a window in my "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);
  }

Well, there is a surprisingly easy (and cross-platform) way to retrieve the default icon of Firefox.

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

You can treat iconurl the same way as any other image-pointing url. The default size is 16x16, append ?size=32 to get a bigger icon. It seems that these two values are the only supported on Windows. This might not be the case for other OSes.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!