Creating wxWidgets Programs with Visual Studio 2017 – Part 2

Part 1 of this post described how to set up your development environment for building wxWidgets-based Windows applications, and how to build a bare-bones application. This post continues the development of this application by modifying it to create a HelloWorld program.

The first step is to create a new main window class. In Solution Explorer, right click on the project name (MyProject if you are following along from part 1). Select Add->New Item… from the dropdown menu to open the Add New Item dialog. Select Header File (.h). Enter “HelloWindow.h ” as the name. Click the Add button; this adds the file and closes the dialog. Reopen the dialog ans select C++ File (.cpp). Enter “HelloWindow.cpp” as the file name, and click Add; the file HelloWindow.cpp is added to the project.

In HelloWindow.h, add a class definition for the HelloWindow class, with wxFrame as the parent class. Include a default constructor definition and a virtual destructor. Add the
void OnClick(wxCommandEvent& event);
method, then add a wxStaticText* member called hello, and a wxButton* member called helloButton. The file should contain the following:

#pragma once
class HelloWindow : public wxFrame
{
public:
    HelloWindow();
    virtual ~HelloWindow();
    void OnClick(wxCommandEvent& event);

private:
    wxStaticText* hello;
    wxButton* helloButton;
};

In HelloWindow.cpp, add
#include "wx/wxprec.h"
as the first line in the file, and
#include "HelloWindow.h"
as the second line in the file.

Define an enumeration to contain two widget IDs, one for a wxStaticText widget, and another for a wxButton widget.

The HelloWindow class is derived from wxFrame;  add a call to the wxFrame constructor, then add code to create a wxStaticText widget and a wxButton widget and add them to the window. Bind the OnClick method to the button click event. The HelloWindow constructor should look like this:

HelloWindow::HelloWindow() : wxFrame(nullptr, wxID_ANY, L"Hello World!")
{ 
    hello = new wxStaticText(this, ID_STATIC, L"", { 100, 50 }, { 250, 20 });
    helloButton = new wxButton(this, ID_HELLO, L"Hello", { 150, 100 });
    helloButton->Bind(wxEVT_BUTTON, &HelloWindow::OnClick, this);
}

Add the method HelloWindow::OnClick as follows:

void HelloWindow::OnClick(wxCommandEvent& event)
{
    hello->SetLabelText(L"Hello World, I want to get off.");
    helloButton->Enable(false);
}

This code sets “Hello World, I want to get off.” as the label of the wxStaticText widget and disables the button. Since this method performs the same action every time it executes and no other method sets the wxStaticText widget’s label to any other value, there is no reason to leave the button enabled.

HelloWindow.cpp should now contain:

#include "wx/wxprec.h"
#include "HelloWindow.h"

enum {
    ID_STATIC = 2001,
    ID_HELLO
};

HelloWindow::HelloWindow() : wxFrame(nullptr, wxID_ANY, L"Hello World!")
{
    hello = new wxStaticText(this, ID_STATIC, L"", { 100, 50 }, { 250, 20 });
    helloButton = new wxButton(this, ID_HELLO, L"Hello", { 150, 100 });
    helloButton->Bind(wxEVT_BUTTON, &HelloWindow::OnClick, this);
}

HelloWindow::~HelloWindow()
{

}

void HelloWindow::OnClick(wxCommandEvent& event)
{
    hello->SetLabelText(L"Hello World, I want to get off.");
    helloButton->Enable(false);
}

Add

#include "HelloWindow.h"

below the two #include directives at the top of MyProjectApp.cpp. Change the

wxFrame* mainFrame = new wxFrame( nullptr, wxID_ANY, L"MyProject");

line in MyProjectApp::OnInit()

to

HelloWindow* mainFrame = new HelloWindow();
Build and run this program. You will get the following window:

HelloWorld1

Click the Hello button:

Hello World2

As noted above, text is displayed in the wxStaticText widget, and the wxButton is disabled.

But Aren’t There Memory Leaks?

A HelloWorld object is created on the heap in MyProjectApp::OnInit()</em> and two widgets are created on the heap in the HelloWorld constructor, but these objects are not deleted anywhere in the code. Doesn’t that result in memory leaks?

Well, no. The wxWidgets library takes ownership of these objects and deletes them at the appropriate time. The widgets are deleted when HelloWindow is destroyed, and HelloWindow is deleted when the MyProjectApp terminates.

Now What?

The Tutorials section of the wxWidgets website contains more information about wxWidgets and how to use it.

Advertisements

2 thoughts on “Creating wxWidgets Programs with Visual Studio 2017 – Part 2

  1. Pingback: Creating wxWidgets Programs with Visual Studio 2017 – Part 1 | Using C++

  2. Pingback: Creating wxWidgets Programs with Visual Studio 2015 – Part 2 | Using C++

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s