Hello World Example | wxWidgets
Version: 3.3.3
Documentation
Programming Guides
Hello World Example
This page shows a very simple wxWidgets program that can be used as a skeleton for your own code.
Note
Creating the UI entirely from C++ code is fine for a simple example, but more realistic programs can find it more convenient to define their UI at least partially in
XRC resource files
While this program does nothing very useful, it introduces a couple of important concepts and explains how to write a working wxWidgets application. Trying building and running this application is also a good way of checking that wxWidgets is correctly installed on your system. And if you haven't installed wxWidgets yet, please
do it first
Without further ado, let's write our first application.
First, you have to include wxWidgets' header files, of course. This can be done on a file by file basis (such as
wx/frame.h
) or using one global include (
wx/wx.h
) which includes most of the commonly needed headers (although not all of them as there are simply too many wxWidgets headers to pull in all of them).
// Start of wxWidgets "Hello World" Program
#include
Execution of a "classic" C++ program starts in its
main()
function (putting aside the constructors of the global objects), but GUI applications must use a different "entry function" on some platforms (such as
WinMain()
used under Microsoft Windows). wxWidgets provides a convenient
wxIMPLEMENT_APP()
macro, which allows to define the appropriate entry function on all platforms. Please note that you can avoid using macros if you're so inclined by defining your own entry function and using
wxInitializer
class for the library initialization, but this example won't do it for brevity and simplicity.
This macro takes a single parameter which is the name of the application class, that must be defined in the program. This class must derive from
wxApp
and, at the very least, override
wxApp::OnInit()
virtual function, as it will be called by wxWidgets to initialize the application. Let's do this:
class
MyApp :
public
wxApp
public
bool
OnInit
()
override
};
// This defines the equivalent of main() for the current platform.
wxIMPLEMENT_APP
(MyApp);
wxAppConsole::OnInit
virtual bool OnInit()
This must be provided by the application, and will usually create the application's main window,...
wxApp
The wxApp class represents the application itself when wxUSE_GUI=1.
Definition:
app.h:983
wxIMPLEMENT_APP
#define wxIMPLEMENT_APP(className)
This macro defines the application entry point and tells wxWidgets which application class should be ...
Definition:
app.h:1540
The main window of a typical application is a
wxFrame
object. While it's possible to just use this class directly, it's usually more convenient to derive a custom class from it, as this allows to store additional data and handle events (such as mouse clicks, messages from the menu, or a button) in the methods of this class — allowing them to access this data easily.
So, even if we don't have any data in this toy example, let's define such a custom class with a few "event handlers", i.e. functions taking the
event
parameter of the type corresponding to the event being handled, which is
wxCommandEvent
for the events from simple controls such as buttons, text fields and also menu items. In our example, we react to three menu items: our custom "Hello", and the "Exit" and "About" items (any program should normally implement the latter two). Notice that these handlers don't need to be virtual or public.
class
MyFrame :
public
wxFrame
public
MyFrame();
private
void
OnHello(
wxCommandEvent
& event);
void
OnExit(
wxCommandEvent
& event);
void
OnAbout(
wxCommandEvent
& event);
};
wxCommandEvent
This event class contains information about command events, which originate from a variety of simple ...
Definition:
event.h:2132
wxFrame
A frame is a window whose size and position can (usually) be changed by the user.
Definition:
frame.h:167
Now that we have this class, we can implement
OnInit()
which, as you remember, is called upon application startup and simply create the main window, represented by this class, in it:
bool
MyApp::OnInit()
MyFrame *frame =
new
MyFrame();
frame->Show();
return
true
Please note the following points:
Contrary to appearances, there is no memory leak in this example: wxWidgets takes ownership of all the window objects and destroys them automatically when the corresponding on-screen window is destroyed. For the frames (i.e. top-level windows) this happens when the user closes it. For the other windows, it happens when their parent is destroyed. But, generally speaking, all windows are owned by the framework and must
not
be deleted by the application.
Frames, unlike all the other windows, are created hidden by default in order to allow filling them with their contents before showing everything at once. Please don't forget to call
Show()
to make this happen.
Returning
true
from
OnInit()
allows the application to start running. By default, it will exit once all top-level windows have been closed (but see
wxApp::SetExitOnFrameDelete()
), so this function should normally create at least one window (but can, of course, create more than one). Otherwise it can just return
false
to exit immediately instead.
In the constructor of the main window, we create a menu with our menu items, as well as a status bar to be shown at the bottom of the main window.
In order to be able to react to a menu command, it must be given a unique identifier which can be defined as a constant or an enum element. The latter is often used because typically many such constants will be needed:
enum
ID_Hello = 1
};
Notice that you don't need to define identifiers for "About" and "Exit", as wxWidgets already predefines standard values such as
wxID_ABOUT
and
wxID_EXIT
(see
Stock Items
for the full list). You should use these whenever possible, as they can be handled in a special way by a particular platform and also have predefined labels associated with them, which allows us to omit them when appending the corresponding menu items. For our custom item, we specify the label, also containing an accelerator, separated by
Tab
from the label itself, allowing to invoke this command from keyboard:
MyFrame::MyFrame()
wxFrame
(nullptr,
wxID_ANY
"Hello World"
wxMenu
*menuFile =
new
wxMenu
menuFile->
Append
(ID_Hello,
"&Hello...\tCtrl+H"
"Help string shown in status bar for this menu item"
);
menuFile->
AppendSeparator
();
menuFile->
Append
wxID_EXIT
);
wxMenu
*menuHelp =
new
wxMenu
menuHelp->
Append
wxID_ABOUT
);
wxMenuBar
*menuBar =
new
wxMenuBar
menuBar->
Append
(menuFile,
"&File"
);
menuBar->
Append
(menuHelp,
"&Help"
);
SetMenuBar(menuBar);
CreateStatusBar();
SetStatusText(
"Welcome to wxWidgets!"
);
... continued below ...
wxMenuBar
A menu bar is a series of menus accessible from the top of a frame.
Definition:
menu.h:32
wxMenuBar::Append
virtual bool Append(wxMenu *menu, const wxString &title)
Adds the item to the end of the menu bar.
wxMenu
A menu is a popup (or pull down) list of items, one of which may be selected before the menu goes awa...
Definition:
menu.h:507
wxMenu::Append
wxMenuItem * Append(int id, const wxString &item=wxEmptyString, const wxString &helpString=wxEmptyString, wxItemKind kind=wxITEM_NORMAL)
Adds a menu item.
wxMenu::AppendSeparator
wxMenuItem * AppendSeparator()
Adds a separator to the end of the menu.
wxID_ANY
@ wxID_ANY
Any id: means that we don't care about the id, whether when installing an event handler or when creat...
Definition:
defs.h:591
wxID_EXIT
@ wxID_EXIT
Definition:
defs.h:607
wxID_ABOUT
@ wxID_ABOUT
Definition:
defs.h:615
Note that, as with the windows, menu pointers don't need to (and, in fact, must not) be destroyed because they are owned by the menu bar, which is itself owned by the frame, which is owned, i.e. will be destroyed, by wxWidgets.
We also have to connect our event handlers to the events we want to handle in them. We do this by calling Bind() to send all the menu events (identified by wxEVT_MENU event type) with the specified ID to the given function. The parameters we pass to Bind() are
The event type, e.g. wxEVT_MENU, wxEVT_BUTTON, wxEVT_SIZE, or one of many other events used by wxWidgets.
A pointer to the method to call, and the object to call it on. In this case, we just call our own function, and pass the
this
pointer for the object itself. We could instead call the method of another object, or a non-member function — in fact, any object that can be called with a
wxCommandEvent
, can be used here.
An optional identifier, allowing us to select just some events of wxEVT_MENU type, namely those from the menu item with the given ID, instead of handling all of them in the provided handler. This is mainly useful with menu items and rarely with other kinds of events.
... continued from above ...
Bind(
wxEVT_MENU
, &MyFrame::OnHello,
this
, ID_Hello);
Bind(
wxEVT_MENU
, &MyFrame::OnAbout,
this
wxID_ABOUT
);
Bind(
wxEVT_MENU
, &MyFrame::OnExit,
this
wxID_EXIT
);
wxEVT_MENU
wxEventType wxEVT_MENU
Definition:
event.h:5482
Here are the standard event handlers implementations. MyFrame::OnExit() closes the main window by calling Close(). The parameter
true
indicates that other windows have no veto power such as after asking "Do you really want to close?". If there is no other main window left, the application will quit.
void
MyFrame::OnExit(
wxCommandEvent
& event)
Close(
true
);
MyFrame::OnAbout() will display a small window with some text in it. In this case a typical "About" window with information about the program.
void
MyFrame::OnAbout(
wxCommandEvent
& event)
wxMessageBox
"This is a wxWidgets Hello World example"
"About Hello World"
wxOK
wxICON_INFORMATION
);
wxICON_INFORMATION
#define wxICON_INFORMATION
Definition:
defs.h:462
wxOK
#define wxOK
Definition:
defs.h:445
wxMessageBox
int wxMessageBox(const wxString &message, const wxString &caption=wxMessageBoxCaptionStr, int style=wxOK|wxCENTRE, wxWindow *parent=nullptr, int x=wxDefaultCoord, int y=wxDefaultCoord)
Show a general purpose message dialog.
The implementation of custom menu command handler may perform whatever task your program needs to do, in this case we will simply show a message from it as befits a Hello World example:
void
MyFrame::OnHello(
wxCommandEvent
& event)
wxLogMessage
"Hello world from wxWidgets!"
);
wxLogMessage
void wxLogMessage(const char *formatString,...)
For all normal, informational messages.
Note
It can be convenient to use unnamed lambdas instead of functions for event handlers, especially for such short functions. Here, for example, we could replace the handler above with just
Bind(
wxEVT_MENU
, [=](
wxCommandEvent
&) {
wxLogMessage
"Hello from a lambda!"
); }, ID_Hello);
Here is the entire program that can be copied and pasted:
// Start of wxWidgets "Hello World" Program
#include
class
MyApp :
public
wxApp
public
bool
OnInit
()
override
};
wxIMPLEMENT_APP
(MyApp);
class
MyFrame :
public
wxFrame
public
MyFrame();
private
void
OnHello(
wxCommandEvent
& event);
void
OnExit(
wxCommandEvent
& event);
void
OnAbout(
wxCommandEvent
& event);
};
enum
ID_Hello = 1
};
bool
MyApp::OnInit()
MyFrame *frame =
new
MyFrame();
frame->Show(
true
);
return
true
MyFrame::MyFrame()
wxFrame
(nullptr,
wxID_ANY
"Hello World"
wxMenu
*menuFile =
new
wxMenu
menuFile->
Append
(ID_Hello,
"&Hello...\tCtrl-H"
"Help string shown in status bar for this menu item"
);
menuFile->
AppendSeparator
();
menuFile->
Append
wxID_EXIT
);
wxMenu
*menuHelp =
new
wxMenu
menuHelp->
Append
wxID_ABOUT
);
wxMenuBar
*menuBar =
new
wxMenuBar
menuBar->
Append
(menuFile,
"&File"
);
menuBar->
Append
(menuHelp,
"&Help"
);
SetMenuBar( menuBar );
CreateStatusBar();
SetStatusText(
"Welcome to wxWidgets!"
);
Bind(
wxEVT_MENU
, &MyFrame::OnHello,
this
, ID_Hello);
Bind(
wxEVT_MENU
, &MyFrame::OnAbout,
this
wxID_ABOUT
);
Bind(
wxEVT_MENU
, &MyFrame::OnExit,
this
wxID_EXIT
);
void
MyFrame::OnExit(
wxCommandEvent
& event)
Close(
true
);
void
MyFrame::OnAbout(
wxCommandEvent
& event)
wxMessageBox
"This is a wxWidgets Hello World example"
"About Hello World"
wxOK
wxICON_INFORMATION
);
void
MyFrame::OnHello(
wxCommandEvent
& event)
wxLogMessage
"Hello world from wxWidgets!"
);
Generated on Sat Apr 18 2026 22:28:10 for wxWidgets by
Doxygen
1.9.1