LoadIcon question

1
2
3
//resource.rc
WindowIcon ICON "MainIcon.ico"
TabIcon ICON "AltTab.ico"


//These are declared globally
HICON hWindowIcon = 0;
HICON hTabIcon = 0;

I have this now in WM_CREATE
1
2
3
4
5
6
7
8
hWindowIcon = (HICON)LoadImage(GetModuleHandle(NULL), 
"WindowIcon", IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), 0);
hTabIcon = (HICON)LoadImage(GetModuleHandle(NULL), "TabIcon", IMAGE_ICON,
GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), 0);
if(hWindowIcon)
SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hWindowIcon);
if(hTabIcon)
SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM)hTabIcon);


I use DestroyIcon(hWindowIcon) and DestroyIcon(hTabIcon) in WM_DESTROY

but I would like to use this instead since it's less code to write
1
2
3
4
WNDCLASSEX wc;
wc.hIcon = LoadIcon(hInstance, "TabIcon");
wc.hIconSm = (HICON)LoadImage(hInstance, "WindowIcon", 
IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), 0);


Do I need to call DestroyIcon for both LoadIcon and LoadImage in the second instance? Or will windows free the memory itself since the icons are assigned to the window class?
Last edited on
Use LoadImage with LR_SHARED flag and forget about destroying the icons afterwards.
Topic archived. No new replies allowed.