Set Icon on Window (Correctly Sized)

Share your advanced PureBasic knowledge/code with the community.
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Set Icon on Window (Correctly Sized)

Post by pdwyer »

I spend about an hour on this. Not just to set the icon on the window but stuffing around with the built in loadImage() and SendMessage_() only to find that the 32x32 icon resized badly and looked awful. After hunting around on the forums reading about people resaving their icons so the 32x32 layer is taken out and PB reads the 16x16, and other people who tried resizeimage only to be dissapointed (with a known -to some- bug) I found some bits that eventually worked so I thought I'd put them all together here in a couple of lines.

I'm sure 99.99% of PBer's know this already but the new comers will lose a little hair getting here.

Code: Select all

    Window_Icon.l=LoadImage_(GetModuleHandle_(0),"D:\icons\MyIcon.ico",#IMAGE_ICON,16,16,#LR_LOADFROMFILE)
    SendMessage_(WindowID(#Window_0), #WM_SETICON, 0, Window_Icon)
If you set this to 32, 32 you'll get what I had for the inbuilt loadimage method... yuk :?
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
fizban
User
User
Posts: 31
Joined: Thu Sep 20, 2007 7:36 am
Location: Spain

Post by fizban »

Thanks for the info. I'm a newcomer and I find this very useful. Still, is there a way of setting the window icon without having to load it from a file at runtime? I guess there is no way of setting it at design time (like the main window and executable icon), but maybe loading it from a resource file would be ok...
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

This is great! Thank you. :)
Thanks for the info. I'm a newcomer and I find this very useful. Still, is there a way of setting the window icon without having to load it from a file at runtime? I guess there is no way of setting it at design time (like the main window and executable icon), but maybe loading it from a resource file would be ok...
I think the simplest method would be to add the icon as a resource of your exe and load the image from there. You could also have it read from an icon in the directory and if none was found default to the exe icon.
Edwin Knoppert
Addict
Addict
Posts: 1073
Joined: Fri Apr 25, 2003 11:13 pm
Location: Netherlands
Contact:

Post by Edwin Knoppert »

That's because you didn't read the help about WM_SETICON..

You *should* set a big and small icon.
The big icon is shown in the ALT+TAB dialog for example.
Post Reply