Associating an Icon with a wxFrame

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
Ksmith22
I live to help wx-kind
I live to help wx-kind
Posts: 199
Joined: Mon Nov 21, 2005 4:34 pm

Associating an Icon with a wxFrame

Post by Ksmith22 » Mon Jan 23, 2006 7:41 pm

I'm having the hardest time doing this. According to the wxWidgets book all I should have to do is do something like this:

Code: Select all

wxIcon icon(wxT("myicon.ico"), wxBITMAP_TYPE_ICO);

frame->SetIcon(icon);
And that should work. But it doesn't. Both for the upper left icon and using ALT-TAB I get the generic Win32 "blank EXE box" icon instead of the icon I specify. I've tried a number of things but everything seems to produce the same results.

Does anyone have any idea what I'm doing wrong? If it makes any difference I'm using Windows XP with Visual Studio .NET 2003. I've tried setting icons in the resource file too but they only show up on the EXE itself and minimized windows in the task bar. Never on the actual frames themselves.

Edit: Ok, if I use:

Code: Select all

	SetIcon(wxICON(WXDEFAULT_FRAME));
To set my icon from the resource file (WXDEFAULT_FRAME is the icon name in the resource file) it will show up for the upper left corner of the frame. But it still doesn't show when I ALT-TAB for some reason.

Jamie
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 205
Joined: Wed Mar 30, 2005 10:56 pm

Post by Jamie » Mon Jan 23, 2006 8:16 pm

Try this:

Code: Select all

frame->SetIcon(wxIcon(wxT("myicon.ico")));
You are using wxBITMAP_TYPE_ICO and this loads the icon from a file, not resources.

Ksmith22
I live to help wx-kind
I live to help wx-kind
Posts: 199
Joined: Mon Nov 21, 2005 4:34 pm

Post by Ksmith22 » Mon Jan 23, 2006 8:18 pm

Jamie wrote:Try this:

Code: Select all

frame->SetIcon(wxT("myicon.ico"));
You are using wxBITMAP_TYPE_ICO and this loads the icon from a file, not resources.
Yeah, I've tried that too. It didn't work. I've even tried using an XPM. Like I said in the edit above, if I use that wxICON command to load the resource it half-works but it still doesn't work for ALT-TAB. Maybe this is a problem with the icon itself but I don't know. The icon is 32x32 so I would think it would size correctly (since it seems to work anytime it needs to display as 16x16 but apparently not as 32x32)

Edit: Maybe not. I tried sample.ico which works perfectly with the minimal sample and it still will not display when I ALT-TAB. So it must be some problem with the code and not the icon.

Jamie
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 205
Joined: Wed Mar 30, 2005 10:56 pm

Post by Jamie » Mon Jan 23, 2006 8:25 pm

I've just noticed this with the samples too, I think I remember that this used to work.

Edit: Just tested 2.6.1 and this did used to work, definately a bug. Looking at it now. Can you test the minimal sample on your machine and see if it doesn't work there either?

Edit: Nevermind, really should test on clean version first. Minimal sample does work correctly.
Last edited by Jamie on Mon Jan 23, 2006 8:32 pm, edited 1 time in total.

Ksmith22
I live to help wx-kind
I live to help wx-kind
Posts: 199
Joined: Mon Nov 21, 2005 4:34 pm

Post by Ksmith22 » Mon Jan 23, 2006 8:31 pm

Jamie wrote:I've just noticed this with the samples too, I think I remember that this used to work.

Edit: Just tested 2.6.1 and this did used to work, definately a bug. Looking at it now. Can you test the minimal sample on your machine and see if it doesn't work there either?
The minimal sample does work for me, but it's possible that uses something pre-compiled. I don't know.

Jamie
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 205
Joined: Wed Mar 30, 2005 10:56 pm

Post by Jamie » Mon Jan 23, 2006 8:39 pm

Are you sure the icon name is the same in the resource file?

Edit: Drop the .ico extension, ie

SetIcon(wxICON(myicon));

Otherwise I have no idea, sorry

Ksmith22
I live to help wx-kind
I live to help wx-kind
Posts: 199
Joined: Mon Nov 21, 2005 4:34 pm

Post by Ksmith22 » Mon Jan 23, 2006 8:51 pm

Jamie wrote:Are you sure the icon name is the same in the resource file?

Edit: Drop the .ico extension, ie

SetIcon(wxICON(myicon));

Otherwise I have no idea, sorry
Yeah, what I have exactly right now is:
SetIcon(wxICON(WXDEFAULT_FRAME));

With WXDEFAULT_FRAME being the name of the icon file in resources. I also tried it with a resource that had the same name as my icon and it didn't make a difference.

So yeah, I don't know.

Bundy
I live to help wx-kind
I live to help wx-kind
Posts: 153
Joined: Fri Apr 29, 2005 9:46 am
Location: Poland

Post by Bundy » Tue Jan 24, 2006 7:52 am

First:

To add icon try this:

Code: Select all

#include "icon.xpm" //icon.xpm contains a icon bitmap in xpm format
then i constructor:

Code: Select all

this->SetIcon(wxIcon(icon_xpm));
Notice: Some converter make not in xpm file a table named 'fileName'+ '_xpm' just simple ''fileName' so if you don't have icon in left upper corner try this:

Code: Select all

this->SetIcon(wxIcon(icon));
Second:

Maybe You want to set icon for .exe file on Windows? This is set in (for example) VisualStudio projekt file, not in application code


Regards
Bundy
"Fate rewards prepared mind"

Quote from movie "Liberator 2"

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

Post by TrV » Sun Oct 26, 2008 1:53 pm

Hi,

I get back this topic to life because i haven't found how to set an icon to my app when using alt-tab.

resources.rc

Code: Select all

1 ICON "MyApp_1.ico"
MyFrame.cpp

Code: Select all

#include "MyApp_2.xpm"
...

    this->SetIcon(wxIcon(MyApp_2_xpm)); // sets app window icon (shown in upper left corner)

    this->trayIcon = new MyTaskBarIcon(this); // creates taskbar icon
    this->trayIcon->SetIcon(wxIcon(MyApp_2_xpm), "AppName"); // sets taskbar icon image and name
MyApp_1.ico contains:
- 32bits-colored 48x48 / 32x32
- 24bits-colored 48x48 / 32x32
- 8bits-colored 48x48 / 32x32 / 16x16
versions of my icon

MyApp_2.xpm contains "MyApp_2_xpm" structure which is .xpm version of 8bits-colored 16x16 version icon (built using MyApp_2.ico which contains only 8bits-colored 16x16 version)

What works:
- icon for .exe in file manager (ressources.rc)
- icon for app frame in upper left corner (this->SetIcon(wxIcon(MyApp_2_xpm)))
- icon for taskbar (this->trayIcon->SetIcon(wxIcon(MyApp_2_xpm), "AppName"))

What does not work:
- icon for application when using alt-tab

Does anyone have some hints to help me ?

Thanks by advance :)

User avatar
doublemax
Moderator
Moderator
Posts: 15263
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Post by doublemax » Sun Oct 26, 2008 2:03 pm

for ALT-TAB the icon of the frame is used, but it probably needs a bigger resolution.

Instead of

Code: Select all

this->SetIcon(wxIcon(MyApp_2_xpm)); // sets app window icon (shown in upper left corner)
try

Code: Select all

this->SetIcon(wxICON(1)); // assuming that '1' is the name of the icon in the resource file
Alternatively use wxTopLevelWindow::SetIcons(const wxIconBundle& icons) to set multiple icons in a bundle.
Use the source, Luke!

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

Post by TrV » Sun Oct 26, 2008 3:00 pm

OK. I get it.

In fact, in ressources.rc, "1" is the name of my "ICON" ressource. And, as any function, method, class, variable of whatever is named into a source code, it does not support to begin with a number.
As i previously didn't really understand contents of ressources file, i missed this.

So, when changing my ressources to smtg like "MYICON ICON "MyApp_1.ico""), writing "this->SetIcon(wxICON(Icon_ressource_name))" does finally make sense, and everything works now like a charm.

Thank you doublemax to have pushed me to seek further the "wxICON(Icon_ressource_name)" way :)

Final code :

resources.rc

Code: Select all

MYICON ICON "MyApp_1.ico"
MyFrame.cpp

Code: Select all

#include "MyApp_2.xpm"
...

    this->SetIcon(wxICON(MyICON)); // sets app window icon (shown in upper left corner)

    this->trayIcon = new MyTaskBarIcon(this); // creates taskbar icon
    this->trayIcon->SetIcon(wxIcon(MyApp_2_xpm), "AppName"); // sets taskbar icon image and name
Notice that 32x32 and 16x16 versions are enough to get everything working fine.

Post Reply