Icon Resource Problem (simple?) Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
martin the third
Earned a small fee
Earned a small fee
Posts: 16
Joined: Fri Aug 18, 2006 3:52 am

Icon Resource Problem (simple?)

Post by martin the third » Tue Aug 18, 2009 11:25 pm

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

leiradella
I live to help wx-kind
I live to help wx-kind
Posts: 172
Joined: Sun Sep 07, 2008 9:49 pm
Location: Rio de Janeiro, Brazil

Re: Icon Resource Problem (simple?)

Post by leiradella » Wed Aug 19, 2009 1:07 am

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

timg
Earned some good credits
Earned some good credits
Posts: 148
Joined: Mon Jan 23, 2006 6:52 pm

Post by timg » Wed Aug 19, 2009 1:22 am

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.

leiradella
I live to help wx-kind
I live to help wx-kind
Posts: 172
Joined: Sun Sep 07, 2008 9:49 pm
Location: Rio de Janeiro, Brazil

Post by leiradella » Wed Aug 19, 2009 2:04 am

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

martin the third
Earned a small fee
Earned a small fee
Posts: 16
Joined: Fri Aug 18, 2006 3:52 am

Post by martin the third » Wed Aug 19, 2009 5:42 am

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.

TrV
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 630
Joined: Wed Jul 04, 2007 1:12 pm

Post by TrV » Wed Aug 19, 2009 2:46 pm

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.

Post Reply