How to add icon to window?

I kinda figured out how to add an icon to my (Windows) executable by creating a .rc file in the compilation directory with this line in it:

MAINICON ICON "my_icon.ico"

and having my icon there too, and then in build.bat before compiling I call this thing that creates some kind of resource thing (.res file) with the icon:

rc.exe /nologo my_game.rc

and then I compile by passing the resource thing:

cl %CompilerFlags% /Femy_game.exe my_game.res ...

I copied this from some code on GitHub. And with this my exe gets the icon shown as its document icon and on its task-bar icon, but the window I create doesn't get this icon. I see the WNDCLASSW struct you have to make to create a window has a HICON member... So can you make a window and tell it to use the executable's icon? If not, how should I set the window's icon?

Thanks!


Edited by Opoiregwfetags on

Put icon with integer as name in your rc:

1 ICON "my_icon.ico"

Then assign LoadIconA(GetModuleHandle(0), MAKEINTRESOURCE(1)) to hIcon member in WNDCLASS.

I think lookup by name also should work - LoadIconA(GetModuleHandle(0), "MAINICON") - but I have not tried this myself.


Edited by Mārtiņš Možeiko on

Thanks a lot Martins!


Replying to mmozeiko (#25907)