"Hello World" based on WXWINDOWS

zhaozj2021-02-16  49

"Hello World" based on WXWINDOWS

Many people need a simple routine to get a quick entry of WXWindows to understand the organizational forms and rules of their programs.

Like many other libraries (such as STL, Boost), we have to use WXWindows components to include wxwindows related header files.

In general, as long as our compiler supports pre-compiled features, it can contain wxPrec.h (#inlcude "wx / wxprec.h"), if not supported

Can contain wx.h,

This header file contains the WXWindows standard library we can use (#include "wx / wx.h"). This makes we use wxwindows interface

It is basically not available to do #include.

The following routine can generate a single document program framework similar to the MFC, because the document wxdocument is not added as much as possible for the routine,

Just use the application class wxapp and framework wxframe to implement a basic feature of the simplest Windows window and pop up an About ... dialog box with a menu.

/

// file name: hworld.cpp

//

// purpose: wxwindows "Hello World"

//

// for Compilers That Support Precompilation, INCLUDES "WX / WX.H".

#include "wx / wxprec.h"

///

#ifdef __borlandc__

#pragma HDRSTOP

#ENDIF

///

#ifndef wx_precomp

#include "wx / wx.h"

#ENDIF

In this code, the first is to include the header file wxprec.h, if our compiler does not support pre-compiled features, including the header file wx.h, as mentioned earlier,

WXWindows standard libraries are included in this file, which is not used in our relatively simple routines.

Class myapp: public wxapp

{

Virtual bool oninit ();

}

WXWINDOWS-based applications should have a class (wxapp) constructing this application object, so we inherit WXApp, generate application class MyApp for our routines.

Overloaded for initialization operations Oninit (), here, this method is used to create a main window.

Of course, just like MFC, the application class can only build an application instance, and the main framework of this application also requires framework wxframe to construct.

Class myframe: public wxframe

{

PUBLIC:

MyFrame (Const WxString & Title, Const WXPoint & Pos, Const WXSIZE & SIZE);

Void ONQUIT (WXCommandevent & Event);

Void Onabout (WxCommandevent & Event);

Private:

Declare_event_table ()

}

Here we also inherit your own framework type MyFrame from wxframe, and we can construct the main framework of the routine, and give the status bar and menu on the main frame by its constructor.

WXFrame is defined in frame.h. In the above code, we also see that there are two functions of ONQUIT () and OnAbout (), which is the response function for the main frame menu. From then on, we can see that in need to respond to events that occur on the main frame, you must define the response function here, and in subsequent so-called Event Table (event list),

Give each event response function a uniquely determined ID, which is like a handle pointing to them. To distinguish between different response functions, this ID must be constant and constant is unique, usually defined by const (normal) or ENUM (enumeration).

ENUM

{

ID_QUIT = 1,

ID_ABOUT,

}

The event example is as follows:

Begin_Event_Table (MyFrame, wxframe)

EVT_MENU (ID_quit, myframe :: ONQUIT)

EVT_MENU (id_about, myframe :: onain)

END_EVENT_TABLE ()

Looking out in the macro declare_event_table, the unique identified ID (id_quit, id_about) is linked to the corresponding response operation,

Also link all the events with the function of responding to the response, and if the value of our given ID is -1, it means that the corresponding function responds to all events of a type.

According to this setting, you can set a function portal for a set of events only Event Table, such as a menu event for this routine, set a response function entry.

Declare_event_table is defined in Event.h.

The function on Quit () and OnAbout () have the only parameter wxcommandevent class object reference. Where wxcommandevent is more unfamiliar, it is a derived class of WXEvent, which is also defined in Event.h, saving the information of a specific event in such object, and is called by the response function.

The next is the definition of the two class of the two classes described above, first is the oninit () function of the initialization application.

BOOL MyApp :: OnNit ()

{

MyFrame * frame = new myframe ("Hello World", WXPoint (50, 50), WXSIZE (450, 340));

Frame-> show (true);

SettopWindow (frame);

Return True;

}

In the initialization code, you first create a dynamic object of the MyFrame Framework class. The main frame window is displayed by its operation show (), settopWindow () set the window, and finally returns TURE, indicating that the initialization is successful.

The following is the definition of the MyFrame framework class constructor:

Myframe :: MyFrame (Const WxString & Title, Const WxPoint & Pos, Const WXSIZE & SIZE)

: wxframe (wxframe *) NULL, -1, TIS, POS, SIZE

{

WXMENU * MENUFILE = New WxMenu;

Menufile-> append (id_about, "& quide ...");

Menufile-> appendseparetor ();

Menufile-> append (id_quit, "e & xit"); wxmenubar * menubar = new wxmenubar;

MenuBar-> append (menufile, "& file");

Setmenubar;

CreateStatusbar ();

SetStatustext ("Welcome to wxWindows!");

}

In the MyFrame framework class constructor, first initialize the title, location, size of the frame window.

Then create a WXMENU menu class dynamic object, pointed by the pointer menufile, and add two menu items by the operation append (), and appendseparator () sets the split line between the menu item.

Create a dynamic object of WXMenubar, the main menu "file", add the menu menufile to the main menu with Append. Use the MYFRAME to set the main menu to the main frame window with MyFrame.

Finally, two CreateStatusbar () creation status bar, setStatustext ("Welcome to wxWindows!"); Set the status bar to display the string. WXMENU is defined in Menu.h.

The last two menu item response function definition:

Void myframe :: ONQUIT (wxcommandevent & wxunused (event))

{

Close (True);

}

Void myframe :: onain (wxcommandevent & wxunused (event))

{

WxMessageBox ("this is a wxwindows's hello world sample",

"About Hello World", WXOK | WXICON_INFORMATION

}

Onquit () function only has a statement, close (true); that is, ending the life of the main frame object.

OnAbout () function also has only one statement, wxMessageBox (); pop-up message box. wxMessageBox () is defined in msgdlg.h.

The last last one has a most important statement. Of course, this is not a statement, this is a macro, this macro is hidden in the entrance main (),

With it, our procedure will do correctly, we will use our application class object as a macro parameter. This macro is defined in App.h.

Implement_app (myApp)

Header function: app.h, event.h, frame.h, menu.h, msgdlg.h

This routine I use DEV-C 4.9.8.0, editing, compiling, and is fully correct. This routine can't be said to be very simple, but my last arrival of the EXE file is up to 2,122,922byte, which is a major weakness of wxwindows, and I hope that the future version will improve.

2004-02-16

Sixsavage@yahoo.com

转载请注明原文地址:https://www.9cbs.com/read-27566.html

New Post(0)