I'm trying to allow the user of my application to set their own custom icons, but loading the icon file through LoadImage function and setting it via WM_SETICON message doesn't seem to do the trick. The icon in Windows explorer doesn't update, and the ones on task bar and in upper left corner reset each time I run the program (the user has to set them again). None of these problems occurred when I used resources instead, but I can't change the .rc file after I compile the program, do I? What am I doing wrong? I have a menu function which calls GetOpenFileName to get icon file, then loads it through LoadImage and sets via SendMessage.
Here's my code (I skipped checking return values of functions to shorten the code)
void DoFileIcon(HWND hwnd)
{
OPENFILENAME ofn;
char szFileName[MAX_PATH] = "";
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = "Icons (*.ico)\0*.ico\0All files (*.*)\0*.*\0";
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
ofn.lpstrDefExt = "ico";
if(GetOpenFileName(&ofn))
{
HICON hIcon, hIconSm;
hIcon = LoadImage(NULL, szFileName, IMAGE_ICON, 32, 32,
LR_LOADFROMFILE);
SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
hIconSm = LoadImage(NULL, szFileName, IMAGE_ICON, 16, 16,
LR_LOADFROMFILE);
SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIconSm);
}
}