Page 1 of 1

Icon Resource Problem (simple?)

Posted: Tue Aug 18, 2009 11:25 pm
by martin the third
I'm sure I'm just missing something, but I can't get my icon resources to load properly.

I'm using the trunk (well, from about a week ago) on Vista with MSVC++ 2008 Professional.

My .rc looks like this:

Code: Select all

aaaaaa ICON "chaostools.ico"
chaostools ICON "chaostools.ico"
#include "wx/msw/wx.rc"
And the code I use to load the icon:

Code: Select all

this->SetIcon(wxICON(chaostools));
I have a copy of chaostools.ico in my source folder as well as my build ouput folder, to be on the safe side. (I should only need it in my source folder).

I've also tried loading some of the included icons, wx/msw/std.ico or mondrian.ico as my "aaaaaa" icon resouce, but they don't show in explorer, either. That makes me think the resource isn't getting included properly, and yet if I delete it visual studio complains. The same is true of my .res file, which I find slightly troubling since I can't find anywhere that I'm actually including that file in my project.

Loading the icon like this:

Code: Select all

this->SetIcon(wxIcon("chaostools.ico", wxBITMAP_TYPE_ICO));
Properly displays the icon in the upper-left of the program and in the alt-tab window, but not in explorer. It also forces me to include the icon file when I distribute, which I'd only want to do if that were the only option.

It might be worth mentioning that my .ico file has every standard icon size from 16x16 up to 256x256, and I haven't yet made a corresponding xpm file (so the conditional code constructed with wxICON wouldn't have anything to call, if it were built on a non-windows platform).

So, what am I doing wrong? Help is appreciated, thanks.

-m3

Re: Icon Resource Problem (simple?)

Posted: Wed Aug 19, 2009 1:07 am
by leiradella
martin the third wrote:

Code: Select all

aaaaaa ICON "chaostools.ico"
chaostools ICON "chaostools.ico"
#include "wx/msw/wx.rc"

Code: Select all

this->SetIcon(wxICON(chaostools));
Shouldn't it be

Code: Select all

this->SetIcon(wxIcon(wxT("chaostools")));
?

I'm not familiar with code from SVN though...

Cheers,

Andre

Posted: Wed Aug 19, 2009 1:22 am
by timg
I do something similar and the only difference I can see from what you posted is that in my .rc file, I don't put quotes around the filename.

Code: Select all

anIcon ICON klp.ico
The rest is identical to what you have.

Posted: Wed Aug 19, 2009 2:04 am
by leiradella
I'm under the impression that if you call

Code: Select all

this->SetIcon(wxICON(chaostools));
where chaostools is defined somewhere as an integer constant, then that is a call to the constructor

Code: Select all

wxIcon(const char** bits)
which will not load the icon from the resources - it's in fact a bug waiting to happen. The constructor that loads icons from the resource is

Code: Select all

wxIcon(const wxString& name, wxBitmapType type, int desiredWidth = -1, int desiredHeight = -1)
where name is the name of the resource and type should be wxBITMAP_TYPE_ICO_RESOURCE.

In summary, try

Code: Select all

this->SetIcon(wxIcon(wxT("chaostools"), wxBITMAP_TYPE_ICO_RESOURCE));
Cheers,

Andre

Posted: Wed Aug 19, 2009 5:42 am
by martin the third
I tried all of the suggestions, none of which worked, but I did finally find out what the problem was.

I had inadvertently specified "ChaosTools.rc" as the output file for the resource compiler. It should have been "ChaosTools.res" which is the format for a compiled resource. (In my defense, the option reads: "Resource File Name" rather than something more descriptive like "Resource File Output")

If that wasn't clear, I was outputting the compiled resource as my resource script that was supposed to be getting compiled. It would be like pointing the output of the compiler to a .cpp file.

Thanks for all the help, and maybe if someone makes the same stupid mistake as me, they'll find this post useful.'

EDIT: Just as a note, with the resources set correctly, I was able to successfully use the wxICON macro.

Also, as to why referencing the file name only made the icon show in the upper corner and not in explorer, I believe that since it wasn't included as a resource, only the first icon in the icon resource was used (in my case, 16x16) so it had nothing to display at larger sizes.

Posted: Wed Aug 19, 2009 2:46 pm
by TrV
To complete this topic, here's how to deal properly with resources in this case:
resources.rc

Code: Select all

// icon used for display under windows file manager
MYICON ICON "IconFileName.ico"
MyFrame.cpp

Code: Select all

MyFrame::MyFrame()
{
    ...
    this->SetIcon(wxICON(MYICON)); // sets app window icon (shown in upper left corner and alt-tab mode)
    ...
}
Notes:
IconFileName.ico must embed at least:
32x32 for alt-tab mode, big icon view under file manager
16x16 for icon in upper left corner of application window, icon on the left of the tab in the taskbar, small icon view under file manager

Notice the 32x32 is also used automatically by wxAboutDialogInfo if you make use of it for the "about" window.