Change Window Icon at Runtime

I searched around a bit, and all I could find was stuff about changing the icon of the EXE or using icons built into the EXE with a resource file. However, this is not what I want to do. I want to, given a file path, at runtime change the icon of the application. I have a variable that i think controls the icon of the window, and it seems to be of type HICON. I know this is possible because I see programs like windows explorer do it all the time. So what code and includes do I need to do this?
Yes it is possible.

Read here and look for
hIcon = LoadImage(NULL, "menu_two.ico", IMAGE_ICON, 32, 32, LR_LOADFROMFILE);
Er, he wants to modify the resources in another executable.
That isn't as simple as it looks at first blush. You might as well employ another application/library to do it. I use Resource Hacker.
Hope this helps.
No, No, Duoas. I wanted to change the icon of the window at runtime, no files involved.

blackcorder41, thanks. I'll have a look at that.

EDIT: It says it cannot convert from "HANDLE" to "HICON". What should I do?
Last edited on
Cast it hIcon = (HICON)LoadImage(NULL, "menu_two.ico", IMAGE_ICON, 32, 32, LR_LOADFROMFILE);
Forgive me for being so slow on this.

That works, but it doesn't seem to have changed the icon. Do I need to refresh it? How should I refresh the window icon? Thanks for everything so far!
Press ALT+TAB and you'll see the icon..

There are two icons in the window, 32x32 and 16x16. If you want to change the the icon at the task bar and title bar you should do
hIconSm = (HICON)LoadImage(NULL, "menu_two.ico", IMAGE_ICON, 16, 16, LR_LOADFROMFILE);

LoadImage just loads the icon from the file. You need to assign the icon to the window. You do this by sending a WM_SETICON message:

SendMessage( yourwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon );
// you can also use ICON_BIG instead of ICON_SMALL to set the big icon.
//  exactly what the difference is, I'm not sure 

Last edited on
Thanks! It's working perfectly. I really appreciate your help!
What work? Disch's method?
Yeah, it was the combined code that worked.

Also, I found out the difference with the ICON_SMALL and ICON_BIG, if you only set ICON_SMALL then the Alt-Tab icon is stretched and looks bad. If you set the ICON_BIG as well as ICON_SMALL, then everything looks nice.

Thanks again!
Last edited on
Topic archived. No new replies allowed.