OVERVIEW
Adding an application icon is fairly simple. However the behaviour may differ based on the underlying OS. The standard behaviour of an application icon is that it shows in the titlebar of the main application window as well as the application window in the OS taskbar. In some cases, the icon may appear in the titlebar but not in the taskbar or vice versa.
The default format which wxWidgets uses for icons is the xpm format, which is quite uncommon. However standard ico files can be also be used. As long as the icon size is square eg.16×16 , 32×32 etc. they should work.
If your application uses as lot of static resources like fonts, images etc, then the application icon would come from a resource file. But at this time, we are keeping it simple and it takes one line of code to show an application icon.
SAMPLE CODE
appicon.h
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
class AppIcon: public wxFrame {
public:
AppIcon(const wxString& title);
};
appicon.cpp
#include "appicon.h"
AppIcon::AppIcon(const wxString& title):
wxFrame(NULL, -1, title, wxDefaultPosition, wxSize(500,200)) {
SetIcon(wxIcon(wxT("app.xpm")));
}
main.h
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
class Main: public wxApp {
public:
virtual bool OnInit();
};
main.cpp
#include "main.h"
#include "appicon.h"
IMPLEMENT_APP(Main)
bool Main::OnInit() {
AppIcon *app = new AppIcon(wxT("Application Icon"));
app->Show(true);
}
The output is shown below. In this case, Ubuntu 18.0 does not show the icon in the titlebar, but only in the task bar.
The code uses the sample xpm file. You can change it to use the ico file to see how it looks. The ico file is given below:
Leave a Reply