问题
I have some windows application that can change his icon, using win api functions
SendMessage(hwnd, WM_SETICON, ICON_BIG, icon_handle);
SendMessage(hwnd, WM_SETICON, ICON_SMALL, icon_handle);
Shell_NotifyIcon(...);
It changes icon in taskbar and tray (taskbar notification area), but icon in taskmanager still not changed. How can I change icon in taskmanager? Is it possible?
回答1:
From this SO answer
It's important to change all icons, including the application, both small and big:
//Change both icons to the same icon handle.
SendMessage(hwnd, WM_SETICON, ICON_SMALL, hIcon);
SendMessage(hwnd, WM_SETICON, ICON_BIG, hIcon);
//This will ensure that the application icon gets changed too.
SendMessage(GetWindow(hwnd, GW_OWNER), WM_SETICON, ICON_SMALL, hIcon);
SendMessage(GetWindow(hwnd, GW_OWNER), WM_SETICON, ICON_BIG, hIcon);
EDIT:
According to the this SO answer, the icon needs to be a .ICO file created by an icon editor; this SO article also mentions that you need to send the message to the top-most window of the application.
I could switch the icon in the task bar, alt-tab and in the task manager by
a) creating an icon using the Visual Studio Resource Editor
b) loading the icon with code like HICON hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1));
c) sending WM_SETICON, ICON_SMALL
to the topmost window -- I was using a MFC SDI application, so I sent the message to the main frame window (AfxGetApp()->m_pMainWnd
)
NOTE: a comment in the MSDN Docs for WM_SETICON mentions
The system does not make a copy of the icon. Do not destroy the icon before destroying the window
回答2:
It is a general Windows bug. Task manager and explorer remember icons associated with files for a very long time. If your icon has lowest ID in exe, it should appear as the application icon in task manager (root node). But if you changed it recently, it may not work. The icon of the window itself is a quite different thing - if it isn't displaying, your code is wrong.
回答3:
BOOL sendWndIconToTaskbar(HWND hWnd,HICON hIcon)
{
BOOL ret = TRUE;
ASSERT(hWnd);
if(!::IsWindow(hWnd))
return FALSE;
CWnd* pWnd;
pWnd = pWnd->FromHandle(hWnd);
ASSERT(pWnd);
if(!pWnd)
return FALSE;
if(pWnd->GetParent())
{
if(::SetWindowLong(hWnd,GWL_HWNDPARENT,NULL) == 0)
return FALSE;
}
if(!(pWnd->ModifyStyle(NULL,WS_OVERLAPPEDWINDOW)))
ret = FALSE;
pWnd->SetIcon(hIcon,TRUE);
pWnd->SetIcon(hIcon,FALSE);
return ret;
}
HICON hIconSm = (HICON)LoadImage(NULL, "default.ico", IMAGE_ICON, 32, 32, LR_LOADFROMFILE);
sendWndIconToTaskbar(pOcxDlg->m_hWnd,hIconSm);
来源:https://stackoverflow.com/questions/19655327/how-to-change-app-icon-in-taskmanager