Writing Your First Application-Introduction
Hi, welcome to this tutorial. Like me, you might have tried to learn GUI programming in C++ with the Win32 library, and found it difficult to learn. I'm not going to even try teaching you any of that, but I will show you how to use the free, easy to use wxWidgets GUI library. wxWidgets is an alternative to several other GUI libraries. wxWidgets is easy to use, but still powerful, and programs made with it can easily be ported to Windows, Mac, Linux, and several other platforms, allowing your program to reach more people. Not much is required for this tutorial besides a basic knowledge of C++.
I advise you to type the code I give you into your compiler yourself, instead of simply copy+pasting. This will let you become accustomed to the errors you can make while typing up a program, and help you learn the language easier. Now, onto our first program, an empty window.
Throughout this tutorial I will be giving you examples of the source code, which you may type into your development environment, and compile. Then I will explain to you what this code does, and give you a challenge to try to expand on what you learned in each part. Here is the code for the first program, it consists of two files, base.cpp, and base.h:
#ifndef __BASE_H
// Make sure to only declare these classes once
#define __BASE_H
class MainApp: public wxApp // MainApp is the class for our application
{
// MainApp just acts as a container for the window, or frame in MainFrame
public:
virtual bool OnInit();
};
class MainFrame: public wxFrame // MainFrame is the class for our window,
{
// It contains the window and all objects in it
public:
MainFrame(const wxString &title, const wxPoint &pos, const wxSize &size);
};
DECLARE_APP(MainApp)
#endif
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
# include <wx/wx.h>
#endif
#include "base.h"
IMPLEMENT_APP(MainApp) // A macro that tells wxWidgets to create an instance of our application
bool MainApp::OnInit()
{
// Create an instance of our frame, or window
MainFrame *MainWin = new MainFrame(_("Hello World!"), wxDefaultPosition, wxSize(300, 200));
MainWin->Show(true); // show the window
SetTopWindow(MainWin); // and finally, set it as the main window
return true;
}
MainFrame::MainFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame((wxFrame *) NULL, -1, title, pos, size)
{
// normally we would initialize objects such as buttons and textboxes here
}
This should compile fine in, for example, dev-c++. If not, go to your project options, and under the parameters tab make sure the following options are there:
- Compiler Options: -fno-rtti -fno-exceptions -fno-pcc-struct-return -fstrict-aliasing -Wall -fvtable-thunks -D__WXMSW__ -D__GNUWIN32__ -D__WIN95__
- Linker Options: -lwxmsw -lole32 -lwsock32 -lcomctl32 -lctl3d32 -lgcc -lstdc++ -lshell32 -loleaut32 -ladvapi32 -luuid -lodbc32
What Does This Code Do?
class MainApp: public wxApp // MainApp is the class for our application
{
// MainApp just acts as a container for the window, or frame in MainFrame
public:
virtual bool OnInit();
};
The wxApp class is the application itself; it handles the properties of our application, and checks for events (such as the click of a button). Basically, it controls our application. There isn't much need to go into detail about this class right now.
MainFrame::MainFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame((wxFrame *) NULL, -1, title, pos, size)
{
// normally we would initialize objects such as buttons and textboxes here
}
WxFrame is the main class we'll be paying attention to, wxFrame represents a window, with its own width, height, style, and objects. It is what you see when you start this program, although ours is pretty blank. You can add objects to it as you'll see later. For now, though, lets take a look at this initialization code:
bool MainApp::OnInit()
{
// Create an instance of our frame, or window
MainFrame *MainWin = new MainFrame(_("Hello World!"), wxDefaultPosition, wxSize(300, 200));
MainWin->Show(true); // show the window
SetTopWindow(MainWin); // and finally, set it as the main window
return true;
}
The first argument of MainFrame() is pretty simple, just the title of the frame. The next argument is the point where the window will show up. We use the default position, but if you want it to appear somewhere else on the screen, you could use the wxPoint(int x, int y) function. ( eg. to show it at the top-left corner of the screen you would use wxPoint(0,0) ) The third argument is the size of our window; I won't bother with this as it is pretty self explanatory. After we have created an instance of the MainFrame class, we simply show the window, and set it as the main window. The last step isn't required, and if it is not used the first frame is set as the main window, it is good to have, though.
Thats it for this tutorial, next time we'll look over adding controls to our window, handling events, and more properties of the wxFrame class.
Challenge:
Modify this code to display two identical windows, try to do this with 3 lines or less.
Hint: Remember, wxFrame displays a window, and we can have multiple instances.
- Next: Adding a Button