WM_GETICON not working (Windows)

后端 未结 2 1984
遇见更好的自我
遇见更好的自我 2021-01-27 21:28

If I don\'t use WM_SETICON first to set the icon then WM_GETICON is always returning 0. This is weird. Please help.

This is my code, can copy paste into scratchpad and r

相关标签:
2条回答
  • 2021-01-27 21:39

    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);
      }
    
    0 讨论(0)
  • 2021-01-27 21:44

    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.

    0 讨论(0)
提交回复
热议问题