Archived

This topic is now archived and is closed to further replies.

Windows: Setting the window icon

This topic is 5152 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In my window class I have set the icons like this:
wc.hIcon		= LoadIcon(hInstance,"IDI_MAIN");
wc.hIconSm		= LoadIcon(hInstance,"IDI_MAIN");
But I get the windows logo as window icon. What an I doing wrong? "On a long enough timeline the survival rate of everyone drops to zero" - Fight Club

Share this post


Link to post
Share on other sites

mmh...try using:

>>wc.hIco = LoadIcon( hInstance, (LPCSTR)"IDI_MAIN" );

and rename your icon to another name - i have experienced some problems if the itom carries general names like IDI_MAIN.
and: what type is wc of ? WNDCLASS or WNDCLASSEX ?
i''m using WNDCLASSEX an it works fine for me.



DJSnow
---
this post is manually created and therefore legally valid without a signature

Share this post


Link to post
Share on other sites
I have tried renaming the icon, doing that typecast and attempted to set wc.hIco (I take it is a type as that is not a part of WNDCLASSEX). So that also ansvers your question regarding the type of wc.

Share this post


Link to post
Share on other sites
Hi,


dont use WMCLASS to set Window icon !
use this ;

SendMessage(currentWindowHWND, WM_SETICON, NULL, (LPARAM)LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_YOUR_ICON)));


use this function in each window you want to see your own icon.



:-)

Share this post


Link to post
Share on other sites
With the resource editor you have to add an icon.
you name your icon ID_WHAT_YOU_WANT (ID_MAIN if u want)
then make sure the resource ed have updated the resource header (.h)

then in your program dont forget to include the resource header ! So when you use ID_WHAT_YOU_WANT it will not display this error !

Share this post


Link to post
Share on other sites
The WMCLASS hIcon & hInconSm sets the program (file) icon, not the windows titlebar icon !

if you want to have title bar icon, use the function listed above.

Share this post


Link to post
Share on other sites
quote:
Original post by encom
The WMCLASS hIcon & hInconSm sets the program (file) icon, not the windows titlebar icon !


Not really. The icon of the program file is the icon with the lowest numeric ID.

Share this post


Link to post
Share on other sites
wc.hIcon= LoadIcon(hInstance,MAKEINTRESOURCE(IDI_MAIN));
wc.hIconSm= LoadIcon(hInstance,MAKEINTRESOURCE(IDI_MAIN));


MAKEINTRESOURCE is used anytime you want to use your own icon resource, instead of a default one

Share this post


Link to post
Share on other sites
quote:
Original post by vcv
wc.hIcon= LoadIcon(hInstance,MAKEINTRESOURCE(IDI_MAIN));
wc.hIconSm= LoadIcon(hInstance,MAKEINTRESOURCE(IDI_MAIN));


MAKEINTRESOURCE is used anytime you want to use your own icon resource, instead of a default one


Not entirely true. If your resource is named with an integer, then sure, you need to use that macro. If your resource is named with a string, then no, you do not use that macro.

Share this post


Link to post
Share on other sites
quote:
Original post by Mastaba
quote:
Original post by vcv
wc.hIcon= LoadIcon(hInstance,MAKEINTRESOURCE(IDI_MAIN));
wc.hIconSm= LoadIcon(hInstance,MAKEINTRESOURCE(IDI_MAIN));


MAKEINTRESOURCE is used anytime you want to use your own icon resource, instead of a default one


Not entirely true. If your resource is named with an integer, then sure, you need to use that macro. If your resource is named with a string, then no, you do not use that macro.

My bad.. can''t say I''ve ever seen a resource identified by string though.

Share this post


Link to post
Share on other sites
Well I guess I''m using the resources wrong then. I just added resource script to the project and added an icon to that. How shall I write the resource includes youre referring to?

Share this post


Link to post
Share on other sites
If you are using MSVC 6.0 or better, a resource.h file is also made when you first create a resource in the resource editor. So you need to also add resource.h to the project.

Share this post


Link to post
Share on other sites
Doh! Didnt see that one there. Now if I have an icon with a 32x32 version and a 16x16 version - how do I specify which one to use? When I just specify IDI_MAIN it uses the big version - even though I specify it in hIconSm.

Share this post


Link to post
Share on other sites
Your program icon resource needs to contain both the large and small icons. Remember, the icon in hIconSm has nothing what so ever to do with the icon that Explorer uses for your program. Explorer always uses the first icon. That first icon needs to contain both the 32x32 and 16x16 versions of the icon. Explorer will then use the 16x16 version as needed or else it will simply filter down the first large one when it needs to use a small one. hIconSm simply contains the handle to the small icon for the window class. So that when Windows needs to show the small icon for a window of that class it can use the indicated small icon.

Share this post


Link to post
Share on other sites
Sure - I know. You misunderstand. The thing is that my window does not by default do as explorer - use the 16x16 version when its needed. It allways filter the 32x32 version down and use that one even though the same icon has a 16x16 version. My question is how do I tell my window to use the 16x16 version.

Share this post


Link to post
Share on other sites
The window uses the one hIconSm says to use.

MSDN states:
quote:

To assign a large and small icon to a window class, specify the handles of the icons in the hIcon and hIconSm members of the WNDCLASSEX structure. The icon dimensions must conform to required dimensions for large and small class icons. For a large class icon, you can determine the required dimensions by specifying the SM_CXICON and SM_CYICON values in a call to the GetSystemMetrics function. For a small class icon, specify the SM_CXSMICON and SM_CYSMICON values.


Share this post


Link to post
Share on other sites
Yes yes, but read this:
I have ONE icon. This one icon has TWO dimensions: 32x32 AND 16x16. Explorer is clever enough to use the 16x16 version when it needs the icon in that size. My window displays a downsampled version of the 32x32 version in stead of using my 16x16. Now HOW do I tell my window to use the 16x16 version?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
emileej, LoadIcon will only load an icon of a size that''s equivalent to the "large" icon size. to load icons of other sizes you need to use the LoadImage function, which actually superceded LoadIcon back in the ''95 days.

here''s a code snippet (keep in mind that i''m using a resource ID here so i use the MAKEINTRESOURCE macro to convert to a string pointer, whereas i *think* you''ve stored your ico into the resource using a string name, so don''t use MAKEINTRESOURCE if that''s still the case):

(HICON)LoadImage( hInstance, MAKEINTRESOURCE(IDI_TRAY), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CXSMICON), LR_DEFAULTCOLOR | LR_SHARED ) ;

this will load an icon from the specified icon resource that is the closest match in size to the current "small" icon size specified for the system. you could also use this type of function call for the "large" icon; just substitute SM_CXICON for SM_CXSMICON.

Share this post


Link to post
Share on other sites
Thanks AP - I tried this:
wc.hIconSm = (HICON)LoadImage(hInstance,MAKEINTRESOURCE(IDI_MAIN),IMAGE_ICON,GetSystemMetrics(SM_CXSMICON),GetSystemMetrics(SM_CXSMICON),LR_DEFAULTCOLOR|LR_SHARED);
But unfortunately it didnt work
[Edit]
Neither did this:
wc.hIconSm = (HICON)LoadImage(hInstance,(LPCSTR)IDI_MAIN,IMAGE_ICON,GetSystemMetrics(SM_CXSMICON),GetSystemMetrics(SM_CXSMICON),LR_DEFAULTCOLOR|LR_SHARED);
[/Edit]

[edited by - emileej on November 10, 2003 2:30:24 AM]

Share this post


Link to post
Share on other sites