Rob Sears bio photo

Rob Sears

       

Rocket scientist. Computer hacker. Geek before it was cool.

BTC Donations:
1AU9qGkSubhR24r8Y4WEoV8bccZjeT2dKg

1. Download the latest version of wxWidgets Head on over to the project’s SourceForge page and grab the latest version. I grabbed wxWidgets-2.9.4.zip, but there is also a setup package executable. I’m not sure what the installer does that is so special, all we need are the header and library files.

2. Extract to your main drive Whether you use the installer or download the ZIP file, extract the contents to a location you’ll remember. I extracted everything to my C:\ so that all the wxWidget files were located at C:\wxWidgets-2.9.4. You should see a bunch of folders like “art,” “build,” “debian,” etc. The two we care about are “include” and “lib.” The include directory should have two folders: “msvc” and “wx,” each of which have header (.h) files. The lib directory should have a folder “vc_lib” which is full of object file library (lib) files.

3. Create a new Win32 Application Open up Visual Studio 2010 Express and navigate to File > New > Project. Make sure to select a “Win32 Project.” I originally made the mistake of a Win32 Console Application and it left me with a compile error: “LINK : error LNK2001: unresolved external symbol _WinMainCRTStartup.” Fortunately Google and StackExchange pointed the error out.

4. Create main.cpp Once the solution has been set up, add a CPP file to the Source folder. I did this by right-clicking on the Source folder and navigating to Add > New Item and selecting C++ File. Name it main.cpp. Paste in the following code for a simple “Hello World” program and click save:

gist:4303955

/*
 * hworld.cpp
 * http://www.wxwidgets.org/docs/tutorials/hworld.txt
 */
 
#include <wx\wx.h>
 
class MyApp: public wxApp
{
    virtual bool OnInit();
};
 
class MyFrame: public wxFrame
{
public:
 
    MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
 
    void OnQuit(wxCommandEvent& event);
    void OnAbout(wxCommandEvent& event);
 
    DECLARE_EVENT_TABLE()
};
 
enum
{
    ID_Quit = 1,
    ID_About,
};
 
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
    EVT_MENU(ID_Quit, MyFrame::OnQuit)
    EVT_MENU(ID_About, MyFrame::OnAbout)
END_EVENT_TABLE()
 
IMPLEMENT_APP(MyApp)
 
bool MyApp::OnInit()
{
    MyFrame *frame = new MyFrame( _("Hello World"), wxPoint(50, 50),
                                  wxSize(450,340) );
    frame->Show(true);
    SetTopWindow(frame);
    return true;
} 
 
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame( NULL, -1, title, pos, size )
{
    wxMenu *menuFile = new wxMenu;
 
    menuFile->Append( ID_About, _("&About...") );
    menuFile->AppendSeparator();
    menuFile->Append( ID_Quit, _("E&xit") );
 
    wxMenuBar *menuBar = new wxMenuBar;
    menuBar->Append( menuFile, _("&File") );
 
    SetMenuBar( menuBar );
 
    CreateStatusBar();
    SetStatusText( _("Welcome to wxWidgets!") );
}
 
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
    Close(TRUE);
}
 
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
    wxMessageBox( _("This is a wxWidgets Hello world sample"),
                  _("About Hello World"),
                  wxOK | wxICON_INFORMATION, this);
}

6. Edit the project properties Navigate to Project > “<yourprojectname> Properties”. Navigate to Configuration Properties > C/C++ > General. Add the following locations to the “Additional Include Directories” field:

  • <wxWidgets Install directory>\include
  • <wxWidgets Install directory>\include\msvc

Then go to Linker > General. Edit the “Additional Library Directories” field to include <wxWidgets Install directory>\lib\vc_lib

7. Compile the solution You should just be able to click Debug > Build Solution and generate the .exe file.

9. Run helloworld.exe Open up your fancy new wxWidgets Hello World program and see it in action.