WM_GETICON not working (Windows)

后端 未结 2 1988
遇见更好的自我
遇见更好的自我 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(::SendMessageW(hwnd, WM_GETICON, ICON_SMALL, 0));
      if (icon == 0) {
        // Alternative method. Get from the window class
        icon = reinterpret_cast(::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);
      }
    

提交回复
热议问题