Creating wxWidgets Programs with Visual Studio 2015 – 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->Class... from the dropdown menu to open the Add Class dialog. Double-click on C++ Class       Visual C++ to start the Generic C++ Class Wizard. Enter “HelloWindow” as the class name. Enter “wxFrame” as the base class, then select the Virtual destructor checkbox. Click the Finish button. Two new files, HelloWindow.h and HelloWindow.cpp are created and opened.

In HelloWindow.h, delete the #include directive. Add the
void OnClick(wxCommandEvent& event);
method, then add a wxStaticText* member called hello, and a wxButton* member called helloButton.


#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 a enumeration to contain two widget IDs, one for a wxStaticText widget, and another for a wxButton widget.

Add the following code to HelloWindow::HelloWindow(). This creates a wxStaticText widget and a wxButton widget and adds them to the window. The code also binds the OnClick method to the button click event.
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);

and 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 look like this:

#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 = newwxFrame( 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() 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 2015 – Part 2

  1. Pingback: Visual Studio, wxWidgets, and OpenGL | Using C++

  2. Pingback: HelloTriangle | 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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s