The wxWidgets programming tutorial (2. First programs in wxWidgets)

Author: Jan Bodnar. Link to original: http://zetcode.com/tutorials/wxwidgetstutorial/firstprograms/ (English).
A more in-depth tutorial covering menus, toolbars, layout, events, standard and custom dialogs, common and custom controls, drag & drop, and drawing with device contexts, written by Jan Bodnar.

Translations of this material:

into Russian: Руководство по программированию с wxWidgets (2. Первые программы в wx Widgets).. Translation complete.
Submitted for translation by ber113 14.03.2010 Published 2 years, 2 months ago.

Text

First programs in wxWidgets

In this chapter, we will cover the basics needed to create wxWidgets applications. We will create our first simple example, show how to display an icon. Next we will create a simple example demonstrating usage of an event. Finally, we will see, how widgets communicate in wxWidgets applications.

A simple application

First we create the very basic wxWidgets program.

simple.h

#include <wx/wx.h>

class Simple : public wxFrame

{

public:

Simple(const wxString& title);

};

simple.cpp

#include "simple.h"

Simple::Simple(const wxString& title)

: wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(250, 150))

{

Centre();

}

main.h

#include <wx/wx.h>

class MyApp : public wxApp

{

public:

virtual bool OnInit();

};

main.cpp

#include "main.h"

#include "simple.h"

IMPLEMENT_APP(MyApp)

bool MyApp::OnInit()

{

Simple *simple = new Simple(wxT("Simple"));

simple->Show(true);

return true;

}

This very basic example shows a small window on the screen. The window is centered.

Centre();

This method centers the window on the screen. Both horizontally and vertically.

IMPLEMENT_APP(MyApp)

The code that implements the application is hidden behind this macro. This is copy and paste code, we usually don't have to care about.

To compile the example, run the following command. (On Unix).

g++ main.cpp main.h simple.cpp simple.h `wx-config --cxxflags --libs` -o simple

Simple

Figure: Simple

Showing an application icon

In this example, we provide an icon for our application. It became a standard to display a small icon in the upper left corner of the window. The icon is a graphical identity of the program.

icon.h

#include <wx/wx.h>

class Icon : public wxFrame

{

public:

Icon(const wxString& title);

};

icon.cpp

#include "icon.h"

Icon::Icon(const wxString& title)

: wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(250, 150))

{

SetIcon(wxIcon(wxT("web.xpm")));

Centre();

}

main.h

#include <wx/wx.h>

class MyApp : public wxApp

{

public:

virtual bool OnInit();

};

main.cpp

#include "main.h"

#include "icon.h"

IMPLEMENT_APP(MyApp)

bool MyApp::OnInit()

{

Icon *icon = new Icon(wxT("Icon"));

icon->Show(true);

return true;

}

In our example we show a small web icon.

SetIcon(wxIcon(wxT("web.xpm")));

To display an application icon is a matter of one code line. XPM (X PixMap) is an ASCII image format.

Icon

Figure: Icon

A simple button

In the following example, we create a button on the frame widget. We will show, how to create a simple event handler.

button.h

#include <wx/wx.h>

class Button : public wxFrame

{

public:

Button(const wxString& title);

void OnQuit(wxCommandEvent & event);

};

button.cpp

#include "button.h"

Button::Button(const wxString& title)

: wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(270, 150))

{

wxPanel *panel = new wxPanel(this, wxID_ANY);

wxButton *button = new wxButton(panel, wxID_EXIT, wxT("Quit"),

Pages: ← previous Ctrl next
1 2 3