The last time I briefly introduced WXWindows, then I wrote a simple program today to experience the powerful functionality of WXWINDOWS. Let's create a simple text editor. You will find that the WXWindows write program will be so simple. Started with nonsense, start to create a simple text editor. Create your own mainframe
/ / File: mainfrm.h
#ifndef _MAINFRM_H
#define _MAINFRM_H
// Create your own Frame class by inheriting wxframe so you can add a variety of features to your own Frame class.
Class Mainfrm: public wxframe
{
PUBLIC:
// Constructor. Used to create new mainfrm
Mainfrm (const wxchar * title, int xpos, int yPOS, int width, int.com);
// Destructor
~ Mainfrm ();
// Treat menu event
// processes menu file | Open
Void onmenufileopen (WxCommandevent & Event);
// Processes Menu File | Save
Void onmenufilesave (wxcommandevent & evening);
// processes menu file | quit
Void onmenufilequit (WxCommandevent & Event);
// Processes Menu About | Info
Void OnMenuinfoabout; WXCommandevent & Event;
protected:
// Napp Menu Processing Event Table
Declare_event_table ();
Private:
WXTextCtrl * m_ptextctrl;
WXMenuBar * m_pmenubar;
WXMENU * M_PFILEMENU;
WXMENU * m_pinfomenu;
ENUM
{
Menu_file_open,
Menu_file_save,
Menu_file_quit,
Menu_INFO_ABOUT
}
}
#ENDIF // _ mainfrm_h // File: mainfrm.cpp
/ / To support the prepaid compiler, "wx / wx.h"
#include "wx / wxprec.h"
#ifndef wx_precomp
#include "wx / wx.h"
#ENDIF
#include "mainfrm.h"
#include "wx / filedlg.h"
Mainfrm :: mainfrm (const wxchar * title, int xpos, int yPOS, int width, int hotht)
: wxframe (wxframe *) NULL, -1, TIS, WXPOINT (XPOS, YPOS), WXSIZE (Width, Height))
/ *
Constructor:
WXFrame (wxwindow * parent,
WXWindowID ID,
Const wxstring & title,
Const wxpoint & pos = wxdefaultPosition,
Const wxSIze & size = wxdefaultsize,
Long style = wxdefault_frame_style,
Const wxString & name = "frame");
Parent: It is a pointer to the parent window that uses null when the frame does not have a parent window.
ID: is the only window identification number, when you don't need it, use -1.
Title: is the title of the frame, which will appear in the title bar of the frame. POS: It is the position of the frame. WxDefaultPosition represents the default value.
SIZE: It is the size of the frame. WxDefaultsize represents the use of the default.
STYLE: It is a style of the frame.
* /
{
// Add wxtexrctrl control to process text
m_ptextctrl = new wxtextctrl (this, -1, wxstring ("Type Your Text ..."),
WxDefaultPosition, wxdefaultsize, wxte_multiline;
// Add a menu bar
m_pmenubar = new wxmenubar ();
// Add File Menu
m_pfileMenu = new wxmenu (); m_pfileMenu-> append (menu_file_open, "Open (& O)", "Open an existing file");
M_pfileMenu-> append (menu_file_save, "save (& S)", "save a file");
m_pfilemenu-> appendseparator ();
m_pfilemenu-> append (menu_file_quit, "exit (& Q)", "exit program");
m_pmenubar-> append (m_pfilemenu, "file (& f)");
A m
m_pinfomenu = new wxmenu ();
m_pinfomenu-> append (menu_info_about, "About WXFrametest", "Display Program Related Information");
M_PMenubar-> append (m_pinfomenu, "About (& A)"); setmenubar (m_pmenubar);
// Add status bar
Createstatusbar (1);
SetStatustext ("Ready", 0);
}
Mainfrm :: ~ mainfrm ()
{
}
// Treat menu event
Begin_Event_Table (Mainfrm, wxframe)
EVT_MENU (Menu_File_Open, Mainfrm :: OnMenuFileOpen)
EVT_MENU (Menu_File_save, Mainfrm :: OnMenuFileSave)
EVT_MENU (Menu_File_quit, Mainfrm :: OnMenuFilequit)
EVT_MENU (Menu_Info_about, mainfrm :: onmenuinfoabout)
END_EVENT_TABLE ()
Void Mainfrm :: OnMenufileOpen (wxcommandevent & Event)
{
// open a file
WXFiledialog * DLG = New WXFiledialog (this, "Open a text file", "", ",
"All files (*. *) | *. * | Text files (*. Txt) | * .txt",
Wxopen, wxdefaultPosition;
IF (dlg-> showmodal () == wxid_ok)
{
M_PTextCtrl-> loadingfile (dlg-> getpath ());
SetStatustext (DLG-> getFileName (), 0);
}
DLG-> Destroy ();
}
Void Mainfrm :: OnMenuFileSave (wxcommandevent & Event) {
// save document
WXFiledialog * DLG = New wxfiledialog (this, "save a text file", "", ",
"All files (*. *) | *. * | Text files (*. Txt) | * .txt",
WXSAVE, WXDEFAULTPSITION;
IF (dlg-> showmodal () == wxid_ok)
{
m_ptextctrl-> savefile (dlg-> getpath ());
SetStatustext (DLG-> getFileName (), 0);
}
DLG-> Destroy ();
}
Void Mainfrm :: OnMenufilequit (WxCommandevent & Event)
{
//exit the program
Close (false);
}
Void Mainfrm :: onMenuinfoabout (wxcommandevent & Event)
{
WXLogMessage ("CopyRight (C) 2004. Donnie ZHANG / N / N Simple Text Editor - WxFrame Case / N");
}
Show your own mainframe // file: wxframetest.h
#ifndef wxframetest_h
#define wxframetest_h
Class wxframetestapp: public wxapp
{
PUBLIC:
// Program initialization
Virtual bool oninit ();
}
Declare_app (wxframetestApp)
#ENDIF // WXFrametest_H // File: wxframetest.cpp
/ / To support the prepaid compiler, "wx / wx.h"
#include "wx / wxprec.h"
#ifndef wx_precomp
#include "wx / wx.h"
#ENDIF
#include "wxframetest.h"
#include "mainfrm.h"
IMPLEMENT_APP (WXFrameteStApp)
// Each wxwindow program must have an object inherited from wxapp and use the oninit () method to instantiate.
// and you are here to create a main window.
Bool wxframetestapp :: oninit ()
{
Mainfrm * frame = new mainfrm ("Simple Text Editor - WXFrame Cases", 100, 100, 800, 600);
Frame-> Seticon (WXICON (Mainfrmicon));
Frame-> show (true);
SettopWindow (frame);
Return True;
}
Such a simple editor is finished, and I don't need to explain more. I can understand that the code comment should be understood. Among them, menu response events are some of them need to pay attention. It uses an incident table (in
In the early versions of WXWindows, this is achieved by using a callback function or by overloading the virtual function.
After wxwindows 2.0, it is changed to use the event table. ). Each class to deal with the event needs to declare an event table. Mainfrm.h code in macro
Declare_event_table is used to complete this work. Every event must have a method that has already been implemented, each method has a parameter to contain information, and the event obtained from the menu is one
WXCommandEventEvent type data. And the event table We put in the implementation file, wxwindows completed the statement of the event table through some macro. Macro
Begin_event uses the beginning of an event table declaration because there may be more than one event table in a program to pass the class name related class name to the macro. Use a method to associate with events
EVT_MENU macro. This macro requires a menu
ID and event name. Last use of the event table
End_event_table works the end tag. This way we can handle the menu event.