This page shows a very simple wxWidgets program that can be used as a skeleton for your own code.
While it 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.
Note that this simple example creates the UI entirely from C++ code which is fine for a simple example, but more realistic examples will typically define their UI at least partially in XRC resource files.
First, you have to include wxWidgets' header files, of course. This can be done on a file by file basis (such as wx/window.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). For the platforms with support for precompiled headers, as indicated by WX_PRECOMP
, this global header is already included by wx/wxprec.h
so we only include it for the other ones:
Practically every app should define a new class derived from wxApp. By overriding wxApp's OnInit() virtual method the program can be initialized, e.g. by creating a new main window.
The main window is created by deriving a class from wxFrame and giving it a menu and a status bar in its constructor. Also, any class that wishes to respond to an "event" (such as mouse clicks, messages from the menu, or a button) must declare an event table using the macro below.
Finally, reacting to such events is done via "event handlers" which are just functions (or functors, including lambdas if you're using C++11) taking the event
parameter of the type corresponding to the event being handled, e.g. 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.
In order to be able to react to a menu command, it must be given a unique identifier which can be defined as a const variable or an enum element. The latter is often used because typically many such constants will be needed:
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. You should use these whenever possible, as they can be handled in a special way by a particular platform.
As in all programs, there must be a "main" function. Under wxWidgets, main is implemented inside the wxIMPLEMENT_APP() macro, which creates an application instance of the specified class and starts running the GUI event loop. It is used simply as:
As mentioned above, wxApp::OnInit() is called upon startup and should be used to initialize the program, maybe showing a "splash screen" and creating the main window (or several). Frames are created hidden by default, to allow the creation of child windows before displaying them. We thus need to explicitly show them. Finally, we return true from this method to indicate successful initialization:
In the constructor of the main window (or later on), we create a menu with our menu items, as well as a status bar to be shown at the bottom of the main window. Both have to be bound to the frame with respective calls.
Notice that we don't need to specify the labels for the standard menu items wxID_ABOUT
and wxID_EXIT
— they will be given standard (even correctly translated) labels and standard accelerators correct for the current platform, making our program behaviour more native. For this reason, you should prefer reusing the standard ids (see Stock Items) where possible.
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
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.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.
MyFrame::OnAbout() will display a small window with some text in it. In this case a typical "About" window with information about the program.
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:
Here is the entire program that can be copied and pasted: