Search code examples
c++winapiembedded-resource

What values should I use for hIcon and hIconSm in the WNDCLASSEX structure?


I know that hIconSm is used in the Window title bar or in the taskbar. But What does the hIcon represents, I read that this icon should be 24x24 and is shown in the Alt+Tab dialog, but in Windows 7 the Alt+Tab dialog does not show an icon, so should I still specify a 24x24 icon?

Also when I call LoadImage() and specify an icon size, does LoadImage() retrieve the largest icon in the ICO file and resize it to the specified size, or does it retrieve the appropriate icon with the specified size (if it exist) from the ICO file?

This is what I use:

wc.hIcon = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(1), IMAGE_ICON, 32, 32, 0);
wc.hIconSm = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(1), IMAGE_ICON, 16, 16, 0);

Solution

  • You should not hardcode 16 or 32 as the values. Instead use GetSystemMetrics:

    wc.hIcon = (HICON)LoadImage(hInstance,
        MAKEINTRESOURCE(1), IMAGE_ICON,
        ::GetSystemMetrics(SM_CXICON),
        ::GetSystemMetrics(SM_CYICON), 0);
    wc.hIconSm = (HICON)LoadImage(hInstance,
        MAKEINTRESOURCE(1), IMAGE_ICON,
        ::GetSystemMetrics(SM_CXSMICON),
        ::GetSystemMetrics(SM_CYSMICON), 0);