2
votes

I want to change the default icon that shows up at the top left corner of the frame. I have tried many approaches- xpm, ico, bmp, using SetIcon(wxIcon(wxT("icon.xpm"))); as suggested here.
I tried different icon sizes, 16x16, 24x24 and 32x32.
I've also tried adding MYICON1 ICON "Logo.ico" in the resource.rc file, #define MYICON1 101 in the resource.h file and SetIcon(wxIcon(MYICON1)); to the frame constructor..

btw, i'm using wxwidgets 2.8 on visual studio 2010

EDIT:

I've also tried adding MYICON1 ICON "Logo.ico" in the resource.rc file, #define MYICON1 101 in the resource.h file and SetIcon(wxIcon(MYICON1)); to the frame constructor..

With this approach, I get an error in the wxIcon(int) constructor..

1>xsframe.cpp(17): error C2248: 'wxString::wxString' : cannot access private member declared in class 'wxString'
1> C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\wx/string.h(682) : see declaration of 'wxString::wxString'
1> C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\wx/string.h(659) : see declaration of 'wxString'

PS, xsframe is my main frame. whose icon i'm trying to change.

5

5 Answers

4
votes

SetIcon(wxICON(MYICON1));

in file gdicmn.h ,line 166

/* Useful macro for creating icons portably, for example:

    wxIcon *icon = new wxICON(sample);

  expands into:

    wxIcon *icon = new wxIcon("sample");      // On Windows
    wxIcon *icon = new wxIcon(sample_xpm);    // On wxGTK/Linux
 */
2
votes

I am quoting Vaclav's answer from here:

You can set your main frame's icon with wxFrame::SetIcon. Application icon can be changed by adding a new icon resource to your .rc file:

appicon ICON "myapp.ico"
#include "wx/msw/wx.rc"

Note that this icon must be the first icon in your .rc file and it must be the first one when you sort your icons alphabetically. This is because MS developers weren't able to make their mind on how to determine app's icon: it is the first one in .rc file under Windows 9x and the alphabetically first one under NT (or vice versa).

Most people usually miss this. Hope that fixes things.

1
votes

You wrote: MYICON1 ICON "Logo.ico" in the resource.rc file, and SetIcon(wxIcon(MYICON1)); to the frame constructor

That is the approach I use.

There is an extra step you need to do. In the resource.h file you need to define MYICON1 Something like this:

#define MYICON1  101

You have to ensure that the icon file contain ALL the required resolutions - I always ensure it has 16by16, 32by32 AND 256by256 The more the merrier!

It is a good idea if the application icon is the FIRST icon in the resource file.

I recommend upgrading to wxWidgets v2.9.4 - lots of things start working better.

1
votes

Use the string name of the icon, not the numeric identifier. Look at any wxWidgets sample for an example.

0
votes

A quick and dirty, non-portable, only-Windows solution (worked for me in Windows 7, wxWidgets 3.0.4, vc110):

#ifdef __WXMSW__
#include "wx/msw/private.h" //for wxGetInstance()
#endif
...

//in Frame's constructor:
HWND hWnd = this->GetHandle();
HINSTANCE hInstance = wxGetInstance();

HICON hIcon = ExtractIcon(hInstance, L"someicon.ico", 0);
SetClassLongPtr(hWnd, GCLP_HICONSM, (LONG_PTR)hIcon);

Could be useful for doing some other tricks on the window?