Page 1 of 1

Associating an Icon with a wxFrame

Posted: Mon Jan 23, 2006 7:41 pm
by Ksmith22
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.

Posted: Mon Jan 23, 2006 8:16 pm
by Jamie
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.

Posted: Mon Jan 23, 2006 8:18 pm
by Ksmith22
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.

Posted: Mon Jan 23, 2006 8:25 pm
by Jamie
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.

Posted: Mon Jan 23, 2006 8:31 pm
by Ksmith22
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.

Posted: Mon Jan 23, 2006 8:39 pm
by Jamie
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

Posted: Mon Jan 23, 2006 8:51 pm
by Ksmith22
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.

Posted: Tue Jan 24, 2006 7:52 am
by Bundy
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

Posted: Sun Oct 26, 2008 1:53 pm
by TrV
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 :)

Posted: Sun Oct 26, 2008 2:03 pm
by doublemax
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.

Posted: Sun Oct 26, 2008 3:00 pm
by TrV
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.