Page 1 of 1

wxWidgets Part II: Hello, wxWidgets! Creating a Window Rate Topic: ***** 1 Votes

#1 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Posted 09 October 2008 - 02:47 PM

wxWidgets: Part II
Hello, wxFrame!

So, hopefully you've read part one, and got a development environment set up. In this tutorial, we're actually going to start writing some code! Hooray! OMG, code FTW, right? Right!

The end result isn't going to be the most impressive thing in the world, but it's an important one~ wxWidgets is actually quite short and to the point, but the program structure is very different compared to your typical C++ program. The code may look a little confusing at first, but for those of you who have made a window using the Win32 API, you'll know what I mean when I say this:
It's short. You can create a window in just 15 lines, with plenty of spacing to see what's going on.

NOTE: My example is 15 lines long with spacing. Many examples out on the web actually use about 26, because they design a class which is not necessary at this stage. I'm making this as simple as possible to demostrate the pure power of the wxWidgets toolkit.

This tutorial is going to be brief, but it's important that you understand the structure of the program itself before we get in-depth. When writing a wxWidgets application, you don't even have to write a main() function! :o

Like I said before, wxWidgets is short and to the point. It also heavily focuses on object oriented program. If you don't have any experience with objects, I strongly recommend that you read up on them before getting into this code.

Before we start, what do we need to include?
We actually only need to include one header in our application. It's the main wxWidgets header file:
#include <wx/wx.h>

Writing the MainApp class
This is going to be our main application, as the name suggests. We're going to create a class, and inherit wxApp. All that we need to do is override one virtual function: bool OnInit();

This is basically the function that will be called when our class is initialised. So, without further ado, let's get started!
class MainApp : public wxApp {
    public:
        virtual bool OnInit();
};

How simple is that?! And it doesn't get too much more difficult, either.

Let's now define this OnInit() function. So, we want to create a wxFrame, and show it. Simple as that! The constructor for a wxFrame takes quite a few parameters, which I'll cover soon:
bool MainApp::OnInit() {
    wxFrame *content = new wxFrame(NULL, wxID_ANY, wxT("Hello cliche! Hello wxWidgets!"), wxDefaultPosition, wxSize(350, 150));
    content->Show(true);

    return true;
}

Can you believe that there's only one line left after this? :o How great is this?

What are all these things in the wxFrame constructor, then?
The first parameter is the parent of the wxFrame being created. We're passing NULL~ this is the main wxFrame, and it isn't going to have a parent.

Secondly, we have the window ID. wxID_ANY is basically just saying it can be any ID, it doesn't matter. You can pass -1 as the ID, which is the default ID.

The third parameter is the title of the window. We have something else that's completely new here: wxT("text");
Well, wxWidgets has pretty much got it's own everything. Including its own string class, called... yep, you guessed it: wxString. wxT() is basically a quick way to format a string.

The fourth parameter is the location of the window. We're passing wxDefaultPosition, to give our wxFrame a default start position. Makes sense, doesn't it? :)

Lastly, we have wxSize(int width, int height) which basically defines the size of our window.

So after breaking it up, it should make a lot more sense. Lastly, we've got this content->Show(true); which is simply saying that we want to show our wxFrame on the screen.

And finally, that last line! No main here!
All we have to do now is implement our application. (Our wxApp class, which we called MainApp)
We can do this in one line:
IMPLEMENT_APP(MainApp)

NOTE THAT THERE IS NO SEMI-COLON
This probably seems weird to you, but like I said before, wxWidgets pretty much just follows its own rules. You'll see a lot of stuff like this in the coming tutorials.

Here's the complete code:
#include <wx/wx.h>

class MainApp : public wxApp {
    public:
        virtual bool OnInit();
};

bool MainApp::OnInit() {
    wxFrame *content = new wxFrame(NULL, wxID_ANY, wxT("Hello cliche! Hello wxWidgets!"), wxDefaultPosition, wxSize(350, 150));
    content->Show(true);

    return true;
}

IMPLEMENT_APP(MainApp)

(Like I said, it's very concise)

Until next time~

Happy coding! :)

Is This A Good Question/Topic? 0
  • +

Replies To: wxWidgets Part II: Hello, wxWidgets!

#2 dananzoff  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-December 09

Posted 11 December 2009 - 06:11 PM

Hello,

I've read your Tutorial,

You did very well.
But I have some questions:
1. Why do you not use destructor after you use constructor?
2. Why do you not use "delete" after you use "new"

Thank you,
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1