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