Problems with setting application icon

前端 未结 5 1872
伪装坚强ぢ
伪装坚强ぢ 2021-01-05 14:53

(I\'m using Visual Studio 2008, though I remember having similar problems with older versions as well.)

I\'ve tried several different methods (many of them mentioned

相关标签:
5条回答
  • 2021-01-05 15:11

    I've revisited this to see if I can close out my question. I have been unable to get the app's icon to show up in the alt-tab list just through embedding it in the executable; it will show up in the taskbar, as the file's icon in Explorer, and elsewhere just fine.

    I figured I'd try something simpler for setting the icon manually, and went with LoadIcon() instead, as the code below shows:

    HICON hIcon = LoadIcon( GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1) );
    if( hIcon )
    {
       SendMessage( GetHandle(), WM_SETICON, ICON_BIG, (LPARAM)hIcon );
       DestroyIcon( hIcon );
    }
    // ... Same for ICON_SMALL
    

    This seems to have done the trick. I really don't know why, but so far it's the only change that had any effect. It's really not an issue I should spend any more time on, so I'll just go with this.

    0 讨论(0)
  • 2021-01-05 15:12

    I've used a single .ico file with multiple resolutions 16x16, 32x32, 48x48, 96x96 without problems, as application icon. Then windows will pick up the right size.

    The windows shell have an icon cache, there is a trick to restart it without rebooting or logout from the current session, or killing explorer.exe from task manager.

    0 讨论(0)
  • 2021-01-05 15:20

    For anyone coming to this same difficulty if you are going to change ICON_BIG you must first send WM_SETICON with ICON_SMALL and then proceed to ICON_BIG.

    For example:

    SetLastError(0);
    SendMessage(windowHandle, WM_SETICON, ICON_SMALL, (LPARAM)iconsmallHandle);
    [do error handling]
    SetLastError(0);
    SendMessage(windowHandle, WM_SETICON, ICON_BIG, (LPARAM)iconbigHandle);
    [do error handling]
    

    You will need to use SetLastError after the first SendMessage to clear any returned error.

    If you are just setting ICON_SMALL you can ignore ICON_BIG. For whatever reason in all of my tests you must set ICON_SMALL regardless if that icon needs to change or not, before attempting to change ICON_BIG, otherwise you will always get error code 0x6 (invalid handle).

    0 讨论(0)
  • 2021-01-05 15:21

    If found the solution for me. I created an invisible CFrameWnd application window and then a few other main windows (which are dialog windows). The magic undocumented rule is: You have to change the big icon in the first created CFrameWnd. While each window keeps it's own ICON_BIG instance, it does not use them. So it seems not possible to show different taskbar icons for different windows inside one application.

    0 讨论(0)
  • 2021-01-05 15:26

    OK, this worked a treat for me :

    HICON hIconSmall =(HICON)LoadImage(handleToYourApplicationInstance, MAKEINTRESOURCE(IDI_ICON1), IMAGE_ICON,16, 16, 0);
    
    HICON hIconLarge =(HICON)LoadImage(handleToYourApplicationInstance, MAKEINTRESOURCE(IDI_ICON1), IMAGE_ICON,256, 256, 0); // Big for task bar, small loaded otherwise.
    
    SendMessage(yourWindowHandle, WM_SETICON, ICON_SMALL, (LPARAM)hIconSmall) ;
    
    SendMessage(yourWindowHandle, WM_SETICON, ICON_BIG, (LPARAM)hIconLarge) ;
    
    0 讨论(0)
提交回复
热议问题