WXWindows 2
Write cross-platform programs with C
Chinese version
This tutorial is translated by GXL117 and will continue to maintain. This is the first draft of this tutorial. If you find an error, please contact me (email: gxl117@yahoo.com.cn) Let me fix it in time. After that, this tutorial will be more expanded, and the majority of netizens will be welcome to give advice.
This tutorial is published in the GPL protocol.
This license authorizes you to make and publish a copy of this tutorial, but keep this copyright declaration and license statement on all copies.
If you are ready to publish this document, please let the translator to make sure you get the latest version of this document.
The scope of application of this document is not guaranteed, it is only provided as a free resource. Therefore, the author and the maintainer of the information provided herein cannot make this information, certainly correct guarantees.
Franky Braem
Copyright © 2001-2002 Franky Braem
You are allowed to copy and to print this book as long as you do not exploit the information commercially. The author of this book makes no warranty of any kind, expressed or implied, with regard to the programs or the documentation contained in this book .
table of Contents
1.
Introduction Introduction
Why use wxwindows?
WxWindows
History
Hello World
2. wxframe
usage of
Create a FRAME
Constructor constructor
Add a control Adding a control
Add menu Adding a menubar
Add status bar adding a statusbar
Processing processing menu events
3.
Event processing EVENT HANDLING
Introduction Introduction
How does it work How IT Works
Event's jump EVENT SKIPPING
Prohibited an event vetoing an Event
Blocked an event handle Plug an Event Handler
4.
General dialog Common Dialogs
WXFiledialog
Constructor Constructor
Method Methods
Example EXAMPLE
WXFileSelector
WXCOLOURDIALOG
Construction constructor
Method Methods
WxcolourData
Example EXAMPLE
WxgetCOLOURFROMUSER
WXFONTDIALOGOG
Constructor Constructor
Method Methods
WXFONTDATA
Example EXAMPLE
WXPrintDialog
Constructor Constructor
Method Methods
Example EXAMPLE
WXDIRDIALOG
Constructor Constructor
Method Methods
Example EXAMPLE
WXDIRSELECTOR
WXTEXTENTRYDIALOGOGOG
Constructor Constructor
Method Methods
Example EXAMPLE
WxgetTextFromuser
WxGetPasswordFromuser
WxMessageDialog
Constructor Constructor
Method Methods
Example EXAMPLE
WxMessageBox
WXSinglechoiceDialog
Constructor Constructor
Method Methods
Example EXAMPLE
Wxgetsinglechoice
WxgetsinglechoiceIndex
WxgetsinglechoiceData
5.
Dialog box Dialogs
WxDialog
Constructor Constructor
Dialog Box Programming Dialogssizers
SIZERS AND Windows
SIZERS AND Other SIZERS
SIZERS AND SPACERS
WxBoxSizer
Wxgridsizer
WXFlexGridsizer
WxStaticBoxSizer
WxnoteBooksizer
Nesting sizers
List of flights
5.1. The About Dialog
5.2. The About Dialog Using A WXBOXSIZER
5.3. Using wxflexgridsizer
5.4. Nesting Sizers
List of tables
2. WXFrame Styles
4.1. Wxfiledialog styles
4.2. WxtextentryDialog Styles
4.3. WxMessageDialog Styles
4.4. WxsinglechoiceDialog Styles
5.1. Wxdialog styles
5.2. Wxsizer border flags
5.3. WXSIZER Behaviour Flags
5.4. Wxboxsizer orientation
List of example
1.1. A Resource File
1.2. A Makefile for MingW
1.3. Adding Additional Libraries to a makefile
1.4. HelloWorldapp.h - The HelloWorldApp Definition
1.5. HelloWorldapp.cpp - The Implementation of HelloWorldApp
1.6. Using the wxgetapp () Method
2.1. Step 1 - The textframe definition
2.2. Step 1 - The TextFrame IMPLEMENTATION
2.3. TextEditorapp.h - The TextEditorapp Definition
2.4. TextEditorapp.cpp - The TextEditorapp IMPLEMENTATION
2.5. Step 2 - The TextFrame Definition
2.6. Step 2 - The TextFrame IMPLEMENTATION
2.7. Step 3 - The textframe definition
2.8. Step 3 - The TextFrame IMPLEMENTATION
2.9. CREANG A STATUSBAR
2.10. Menus with a description Text
2.11. TextFrame.h - The Full Definition
2.12. TextFrame.cpp - The Full Implementation
3.1. An Event Table
3.2. NumTextCtrl.h - The NumTextCtrl Definition
3.3. NumTextCtrl.cpp - The NumTextCtrl Implementation
3.4. Vetoing an event
3.5. LOGEVENTHANDLER.H - The Definition of The Logeventhandler.
3.6. LOGEVENTHANDAL.CPP - The IMPLEMENTATION OF THE LOGEVENTHANDLER.
4.1. Using wxfiledialog
4.2. Using wxfileselector
4.3. Using wxcolourdialog
4.4. Using wxgetcolourfromuser4.5. Using wxfontdialog
4.6. Using wxdirdialog
4.7. Using WXDIRSELECTOR
4.8. Using wxtextentryDialog
4.9. Using wxgettextFromuser
4.10. Using WxGetPasswordFromuser
4.11. Using wxsinglechoicedialog
5.1. A Simple Resource File
5.2. Aboutdialog.h - The definition of the about-dialog
5.3. AboutDialog.cpp - The Implement of the About-Dialog
5.4. Showing a dialog
5.5. Using wxboxsizer
5.6. Using wxgridsizer
5.7. Using wxflexgridsizer
5.8. Nesting Sizers
Chapter 1. Introduction
Table of contents
Why use wxwindows?
WxWindows
History
Hello World
Why use wxwindows?
Do you want to use C to write on Windows, Linux or UNIX? This is almost impossible when each platform has their own Framework, appearance, behavior, or SDK. Of course, you will definitely don't want to write your procedures for every platform, which will be extremely difficult.
WXWindows is a solution. WxWindows hides all platform-related code. It is a Framework that is not related to the platform, it has the following characteristics:
It is very comprehensive and has a lot of practical classes. IT is Very Complete. There Are Many Utility Classes. It is still in fast development. IT is Still Heavily Developed. Support for many compilers and platforms: Windows, Linux, Mac, UNIX. Has a lot of documentation. The A Lot of Documentation. Individual and commercial organizations can use it freely. IT's Free for Personal and Commercial Uses. As long as WXWindows uses local SDK. This means that if a program is compiled under Windows, it will have the appearance and behavior of Linux programs when it compiles under Linux.
WXWindows history
Julian Smart has developed wxwindows in the Artificial Intelligent Project Academy of EDINBURGH University in 1992. In 1995, Markus Holzem completed the XT version of transplant. In 1997, Windows and GTK transplantation were integrated and put in the CVS archive.
Hello World
This first example is a well-known Hello World program. This program is a window that displays "Hello World" in the status bar.
Every wxwindow program has an object inherited from wxapp. Each program is instantiated with an onInit () method, you can create a main window here. Example 1.4 is the definition of HelloWorldApp:
Example 1.4. HelloWorldapp.h - The HelloWorldApp Definition
#ifndef incruded_helloworldapp_h
#define incruded_helloworldApp_h
/ ** * HelloWorldApp class * This class shows a status bar contains text "Hello World" window * / Class HelloWorldApp: Public WxApp
{
PUBLIC: Virtual Bool OnInit ();
}
Declare_APP (HelloWorldApp)
#ENDIF // INCLUDED_HELLOWORLDAPP_H
We use the wxframe class for the main window. This class provides a window that adjusts the size and location. It has a thick border with a title bar. In addition, you can make it have a menu bar, toolbar, status bar. Example 1.5 is the implementation of HelloWorldApp.
Example 1.5. HelloWorldApp.cpp - HelloWorldApp's Implementation
/ / To support the prepaid compiler, "wx / wx.h"
#include "wx / wxprec.h"
#ifndef wx_precomp
#include "wx / wx.h"
#ENDIF
#include "helloworldapp.h"
IMPLEMENT_APP (HelloWorldApp)
/ * Program starts from here, similar to main () * / in non-WXWINDOWS programs
Bool helloworldapp :: oninit ()
{
WXFrame * frame = new wxframe ((wxframe *) NULL, -1, _T ("Hello World"));
Frame-> CreateStatusbar ();
frame-> setstatustext (_t ("Hello World"));
Frame-> show (true);
SettopWindow (frame);
Return True;
}
When your compiler supports the pre-processor, you can use the wxprec header file. When it does not support, you should include wx.h, which contains all the necessary WXWindows header files. You can also include a corresponding header file for each control. (Translator Note: Under Linux uses wxframe constructor to use wxcha to perform explicit conversion, support for multi-byte characters can be solved using macro wxt (String)
Macro Declare_APP and IMPLEMENT_APP Take us: When the platform needs, it creates a main () or WinMain () method. It creates a global method wxgetApp (). You can use this function to get a reference to a program object:
Example 1.6. WxgetApp () Usage method HelloWorldApp & App = (HelloWorldApp &) WXGetApp ();
You may be strange why there is no code of the Frame variable anywhere in the program. Since the frame is set to the top window of the program, the program will automatically remove the frame when exiting.
Some bad compilers do not allow NULL implicit conversion to wxframe *, so why we want to make it, just out of steady consideration (but if you are really using such a compiler, I I suggest you still update it). At the same time, this is also more conducive to the transplantation of the program.
After Frame is created, use CreateStatusbar to create a status bar. The text content of the status bar is set to "Hello World". Call show () to display Frame. Show () is a way to inherit from wxwindow class. When OnNit returns false, the program will stop immediately. If something happens during the initialization of the program, you can use it to stop the program.
Chapter 2 uses wxframe
table of Contents
create
Constructor
Add a control
Add a menu bar
Add status bar
Handling menu event
The WXFrame class provides us a frame window. The frame window is a window that can change the size, it has a thick border with a title bar. In addition, you can make it have a menu bar, toolbar, status bar. The frame can be used as a window device for other controls, but does not contain another window or dialog. WXFrame is derived from the WXWindow class.
In this chapter we will create a small text editor.
Create a FRAME
Usually you can create your own class by inheriting wxframe. This allows you to add functions to your own Frame class. Example 2.1 is this, which is implemented in Example 2.2.
Example 2.1 - Define TextFrame
#ifndef _textframe_h
#define_textframe_h
Class textFrame: Public wxframe: PUBLIC WXFRAME
{
PUBLIC:
/ **
* Constructor. Used to create new TextFrame
* /
TextFrame (const wxchar * title, int xpos, int yPOS, int width, int hotht);
/ **
* Destructor
* /
~ TextFrame ();
}
#endif // _ textframe_h
TextFrame constructor inherits the constructor from wxframe.
Example 2.2. TEXTFRAME implementation
/ / To support the prepaid compiler, "wx / wx.h"
#include "wx / wxprec.h"
#ifndef wx_precomp
#include "wx / wx.h"
#ENDIF
#include "textframe.h"
TextFrame :: TextFrame (const wxchar * title, int Xpos, int yPOS, int width, int hotht)
: wxframe (wxframe *) NULL, -1, TIS, WXPOINT (XPOS, YPOS), WXSIZE (Width, Height))
{
}
TextFrame :: ~ textFrame ()
{
}
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 is a pointer to the parent window that uses NULL when the frame does not have a parent window. The 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 is the location of the frame. WxDefaultPosition represents the default value. SIZE is the size of the frame. WxDefaultsize represents the use of the default. Style is the style of the frame. Table 2.1. Wxfram style
WXDEFAULT_FRAME_STYLE WXMINIMIZE_BOX, WXMAXIMIZE_BOX, WXRESIZE_BOX, WXSYSTEM_MENU, and WXCAPTION combination. WXICONIZE Displays a window (for Windows only) WXCAPTION Title WXMINIMIZE and WXCONIZE WXMINIMIZE_BOX Framework will have a minimize button WXMAXIMize framework to maximize the way (for Windows) wxmaximize_box framework will have a maximum button WXSTAY_ON_TOP The frame will have a system menu with the upper layer (for Windows) wxsystem_menu frames located in other windows. wxSIMPLE_BORDER a display without a frame border (only GTK with Windows) wxRESIZE_BORDER display a resizable border (only with Unix) wxFRAME_FLOAT_ON_PARENT The frame will be above the parent window in the z-order and is not shown in the task Bar. (Windows and gtk only) wxframe_tool_window Displays a small title bar (for Windows only) Name is the name of the window. This parameter is used to associate an entry with a name. This allows the user to set the MOTIF resource value for each window.
Example 2.3 and Example 2.4 are the implementation of the program
2.3. TEXTEDITORAPP.H - TEXTEDITORAPP definition
#ifndef textedITORAPP_H
#define textedITORAPP_H
Class TextEditorapp: Public WxApp
{
PUBLIC:
/ **
* Program initialization
* /
Virtual bool oninit ();
}
DECLARE_APP (TextEditorapp)
#ENDIF // TextEditorapp_h
Example 2.4. TEXTEDITORAPP.CPP - TEXTEDITORAPP implementation
/ / To support the prepaid compiler, "wx / wx.h"
#include "wx / wxprec.h"
#ifndef wx_precomp
#include "wx / wx.h"
#ENDIF
#include "texteditorapp.h"
#include "textframe.h"
IMPLEMENT_APP (TextEditorapp)
Bool texteditorapp :: oninit ()
{
TextFrame * frame = new textframe ("Simple Text Editor", 100, 100, 400, 300);
Frame-> show (true);
SettopWindow (frame);
Return True;
}
Add control
A window frame is now created, you need to add some controls to process text. Class WXTextCtrl will be what we need. The framework class already contains this control.
Example 2.5 is a definition of a new TEXTFrame class. Only one thing is changing, it adds a member of a wxtextCtrl type. This member is initialized in the constructor.
Example 2.5. TEXTFRAME redefine
#ifndef _textframe_h
#define_textframe_h
Class textFrame: Public wxframe: PUBLIC WXFRAME
{
PUBLIC:
/ **
* Constructor. Used to create new TextFrame
* /
TextFrame (const wxchar * title, int xpos, int yPOS, int width, int hotht);
/ **
* Destructor
* /
~ TextFrame ();
Private:
WXTextCtrl * m_ptextctrl;
}
#endif // _ textframe_h
Example 2.6 is a constructor of the updated TextFraem class. TextFrame is the parent class of WXTextCtrl members. So you should pass the THIS pointer. String "Type Some Text ..." will be used as the default text. Note that the constructor of WXTextCtrl looks like WXFrame. This is because each class is inherited from wxwindow, so there is the same constructor mode.
Also, the code of the WXTextCtrl pointer is not deleted here. It doesn't matter here (actually not allowed), because all subclasses of it will automatically delete when the parent class textFrame is undo.
WXTE_MULTILINE is a dedicated window style for text controls. This style indicates that WXTextCtrl allows you.
Example 2.6.TextFrame Implementation
/ / To support the prepaid compiler, "wx / wx.h"
#include "wx / wxprec.h"
#ifndef wx_precomp
#include "wx / wx.h"
#ENDIF
#include "textframe.h"
TextFrame :: TextFrame (const wxchar * title, int Xpos, int yPOS, int width, int hotht)
: wxframe (wxframe *) NULL, -1, TIS, WXPOINT (XPOS, YPOS), WXSIZE (Width, Height))
{
M_PTextCtrl = new wxtextctrl (this, -1, wxstring ("Type Some Text ..."),
WxDefaultPosition, wxdefaultsize, wxte_multiline;
}
TextFrame :: ~ textFrame ()
{
}
When you build this project, you will have a window that you can enter some text, you can try to cut in this window, paste the text, you will have these short code has made a lot of things for you.
Add a menu bar
Typically, we always add a menu bar to help the user manager, wxWindows provides the following classes: WXMenubar and WxMenu.
Each menu requires a separate ID. This is done by an enumerated type. Can't use the constant defined by #define (such as: #define menu_file_meu 1) because this cannot guarantee that you have a unique ID. It is very easy to miss out and it will become more difficult to maintain when you want to insert a new ID.
Please refer to the wxwindows manual, wxwin predefined some IDs you use when using document / view Framework.
Example 2.7. Definition of TextFrame
#ifndef _textframe_h
#define_textframe_h
Class textFrame: Public wxframe: PUBLIC WXFRAME
{
PUBLIC:
/ **
* Constructor. Creates A New TextFrame
* /
TextFrame (const wxchar * title, int xpos, int yPOS, int width, int hotht);
/ **
* DESTRUCTOR
* /
~ TextFrame ();
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 // _ textframe_h
The menu bar is created in the TextFrame constructor. A menu item is added to the menu using the Append method of wxmenu. Pay attention to how the number indicates which character is shortcut. When you are created, you should add it to the menu bar using wxmenubar.
Example 2.8. TEXTFRAME
// for Compilers That Supports Precompilation, Includes "WX / WX.H"
#include "wx / wxprec.h"
#ifndef wx_precomp
#include "wx / wx.h"
#ENDIF
#include "textframe.h"
TextFrame :: TextFrame (const wxchar * title, int Xpos, int yPOS, int width, int hotht)
: wxframe (wxframe *) NULL, -1, TIS, WXPOINT (XPOS, YPOS), WXSIZE (Width, Height))
{
M_PTextCtrl = new wxtextctrl (this, -1, wxstring ("Type Some Text ..."),
WxDefaultPosition, wxdefaultsize, wxte_multiline;
m_pmenubar = new wxmenubar ();
// File Menu
M_PfileMenu = new wxmenu ();
m_pfileMenu-> append (menu_file_open, "& open");
m_pfilemenu-> append (menu_file_save, "& savior);
m_pfilemenu-> appendseparator ();
m_pfileMenu-> append (menu_file_quit, "& quit");
m_pmenubar-> append (m_pfilemenu, "& file");
// About menu
m_pinfomenu = new wxmenu ();
m_pinfomenu-> append (menu_info_about, "& about");
m_pmenubar-> append (m_pinfomenu, "& info");
SetMenubar (M_PMenubar);
}
TextFrame :: ~ textFrame ()
{
}
Add status bar
It is easy to add a status bar for the frame. Using the CreateStatusBar method in the wxframe class to create a status bar, it accepts a shaping parameter as the number of zones in the status bar, using the SetStatusText method to change the text content in each domain.
EXAMPLE 2.9. Creation of the status bar
CreateStatusbar (3);
SetStatustext ("Ready", 0);
EXAMPLE 2.9 Create a status bar with three domains. The first domain contains text "ready".
The status bar can be used to display a description of a menu item. Example 2.10 changes to Example 2.8 for adding menu items to the menu bar in the previous section to enable the menu item to associate with the status bar. When a menu item is selected, the relevant description is displayed in the first domain in the status bar.
Example 2.10 The menu item containing the function
// File menu
M_PfileMenu = new wxmenu ();
m_pfilemenu-> append (menu_file_open, "& open", "opens an existing file"); m_pfileMenu-> append (menu_file_save, "& savior", "save the content");
m_pfilemenu-> appendseparator ();
m_pfileMenu-> append (menu_file_quit, "& quit", "quit the application");
m_pmenubar-> append (m_pfilemenu, "& file");
/ / About menu
m_pinfomenu = new wxmenu ();
m_pinfomenu-> append (menu_info_about, "offer", "shows information about the application);
m_pmenubar-> append (m_pinfomenu, "& info");
Handling menu event
Now there is already a menu on the frame, when the user chooses a menu, we have to use a method to implement its corresponding action. Each time you choose a menu, you will generate an event. How to make a movement associated with this event? A trick is to associate a class method with an event. In the early versions of WXWindows, this is achieved by using a callback function or by overloading virtual functions. Change from WXWindows 2.0 to use the event table. In this section we only explain the events generated by the menu. The detailed process of incident processing We will explain it in Chapter 3.
Each class to deal with the event needs to declare an event table. 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. The event obtained from the menu is a wxcommandevent type data. Example 2.11 is a complete definition of class TextFrame.
Example 2.11. Complete Definition of TextFrame.h
#ifndef _textframe_h
#define_textframe_h
Class textFrame: Public wxframe: PUBLIC WXFRAME
{
PUBLIC:
/ **
* Constructor. Creates A New TextFrame
* /
TextFrame (Const WxChar Title, Int Xpos, int yPOS, int width, int.com);
/ **
* DESTRUCTOR
* /
~ TextFrame ();
/ **
* 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:
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 // _ textframe_h
The event table is placed 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 an EVT_MENU macro to associate a method with an event. This macro requires a menu ID and an event name. At the end of the event table, the end tag is ended with the macro end_event_table. Example 2.12 shows a complete implementation file.
Example 2.12. TextFrame.cpp - The Full Implementation
// for Compilers That Supports Precompilation, Includes "WX / WX.H"
#include "wx / wxprec.h"
#ifndef wx_precomp
#include "wx / wx.h"
#ENDIF
#include "textframe.h"
TextFrame :: TextFrame (const wxchar * title, int Xpos, int yPOS, int width, int hotht)
: wxframe (wxframe *) NULL, -1, TIS, WXPOINT (XPOS, YPOS), WXSIZE (Width, Height))
{
M_PTextCtrl = new wxtextctrl (this, -1, wxstring ("Type Some Text ..."),
WxDefaultPosition, wxdefaultsize, wxte_multiline;
CreateStatusbar ();
m_pmenubar = new wxmenubar ();
// File Menu
M_PfileMenu = new wxmenu ();
m_pfileMenu-> append (menu_file_open, "& open", "opens an existing file");
m_pfileMenu-> append (menu_file_save, "& savle", "saves the file");
m_pfilemenu-> appendseparator ();
m_pfileMenu-> append (menu_file_quit, "& quit," close the application ");
m_pmenubar-> append (m_pfilemenu, "& file");
// About menu
m_pinfomenu = new wxmenu ();
m_pinfomenu-> append (menu_info_about, "& qu)," Shows Info About the Application ");
m_pmenubar-> append (m_pinfomenu, "& info");
SetMenubar (M_PMenubar);
}
TextFrame :: ~ textFrame ()
{
}
Begin_Event_Table (TextFrame, wxframe)
EVT_MENU (Menu_File_Open, TextFrame :: OnMenuFileOpen)
EVT_MENU (Menu_File_Save, TextFrame :: OnMenuFileSave)
EVT_MENU (menu_file_quit, textframe :: onmenufilequit) evt_menu (menu_info_about, textframe :: onMenuinfoabout)
END_EVENT_TABLE
Void textframe :: OnMenuFileOpen (wxcommandevent & evenet)
{
WXLogMessage ("File Open Menu SELECTED);
}
Void textframe :: OnMenufilesave (wxcommandevent & evenet)
{
WXLogMessage ("File Save Menu SELECTED";
}
Void textframe :: onmenufilequit (wxcommandevent & evenet)
{
Close (false);
}
Void TextFrame :: onMenuinfoabout (wxcommandevent & evenet)
{
WXLogMessage ("File About Menu SELECTED);
}
Chapter III Event Processing
table of Contents
Introduction
How is it working?
Event jump
Incident
Handle
In the previous chapter, I have learned how to deal with menu events. This chapter will explain how the event is working, while explaining how to deal with other events.
Introduction Introduction
The event is something that appears inside or outside the program. An event may be triggered by a user or other programs, an operating system, or the like. At this time, a mechanism is required to let the program respond to the expected event.
In earlier versions of WXWindows, the program's event processing is completely implemented through a callback function or an overloaded virtual function. Use the incident table from WXWindows 2.0 to use the event table. As in Example 3, the event table tells wxwindows to map events to member functions. The event table is declared in the implementation of the file (CPP).
Example 3.1. Event Table
Begin_Event_Table (MyFrame, wxframe)
EVT_MENU (wxid_exit, myframe :: onexit)
EVT_SIZE (MyFrame :: Onsize)
EVT_BUTTON (Button1, Myframe :: Onbutton1)
END_EVENT_TABLE ()
The above incident table tells wxwindows call myFrame's member function onSize when it receives a WM_SIZE event. Macro Begin_Event_Table declares that wxframe and its subclass MyFrame have this incident table.
The members of the handling event cannot be virtual. In fact, the event processor will ignore the virtual statement. The event handler has a similar form: return types of VOID and accepts an event parameter. The type of this parameter is related to the specific event. For Size events, the type used is wxcommandevent. When the control is more complicated, they use their own event classes.
In the class definition, there must be a declare_event_table macro. See Example 2.11.
All of these macros hide complex event processing systems.
How did it work?
When an event is received, WXWindows first invokes the procssEventLler of WXEventHandler on the object on the window. WXWindow (and other window classes) inherit from WXEventHander. ProcessEvent Finds every event in the event table and calls zero or multiple event processor functions. Below is the step of processing an event:
When the object is turned off (including the setvthandle) function of WXEVTHANDLER, you jump to the sixth step. If the object is a wxwindow object, Processevent is handed over on the wxvalidator of the window. If the return function is exited. SearcheventTable is a function of the event processor call. When it fails, starting at the base class until there is no more event table or finding a suitable function. This function exits. The found function begins. The lookup process is applied to the entire event processor chain, and when the application is successful (indicating the event processing) function exits. When the object is a wxwindow object, and the event is the WXCommandEvent type, ProcessEvent is recursively used to the event processor applied to the parent window. When it returns true, the function exits. This allows a button's father to handle the click of the button instead of letting the button hand it yourself. Finally, Processevent is called on the WXAPP object. Event jump EVENT SKENT SKIPPING
Processevent exits after discovering a function that can handle events. This means that when your class reacts to an event, the next class will not get this event. Sometimes we don't want this. This problem can be solved with the Skip method of the WXEvent class according to the basic type of the base class, so that the event processor's lookup continues.
The following example shows the use of event jumps. Example 3.2 is a definition of a text control that only accepts numeric characters.
Example 3.2. NumTextCtrl.h
Class NumTextCtrl: public wxtextCtrl {public: NumTextCtrl (wxwindow * parent); void onchar (wxkeyevent & evenet); protected: declare_event_table ()};
When NumericTextCtrl receives a keyboard event, KEYCODE check is performed. If you entered a number, base class wxtextCtrl can handle this event. This is why we want to use jump on this event. You must call the SKIP method here, otherwise the base class does not process any keys.
Example 3.3. NumTextCtrl.cpp
// for Compilers That Supports Precompilation, Includes "WX / WX.H"
#include "wx / wxprec.h"
#ifndef wx_precomp
#include "wx / wx.h"
#ENDIF
#include "numtextctrl.h"
#include
NumTextCtrl :: NumTextCtrl (wxwindow * parent): wxtextctrl (parent, -1)
{
}
Void NumTextCtrl :: onchar (wxkeyEvent & Event)
{
IF (Isdigit (Event.getKeyCode ())))
{
// Numeric Characters Are ALOWED, SO The Base Class WxtextCtrl
..................
Event.skip ();
}
Else
{
// Character is not allows.
wxbell ();
}
}
Begin_Event_Table (NumTextCtrl, wxtextctrl)
EVT_CHAR (NUMTEXTCTRL :: OnCha)
END_EVENT_TABLE ()
Incident
Some events are disabled. This event will not be further processed when you prohibit an event. Example 3.4 Demo, how to disable this simple text editor after the text changes in a text control. This means that this window cannot be turned off when the user has not saved the changed text content. Example 3.4. Prohibited event
Void textframe :: OnClose (wxcloseEvent & Event)
{
Bool destroy = True;
IF (Event.canveto ())
{
IF (m_ptextctrl-> ismodified ())
{
WxMessageDialog * DLG =
New wxMessageDialog (this, "text is change! / Nare you suwany want to exit?",
"Text Changed !!!", wxyes_no | wxno_default);
Int results = dlg-> showModal ();
IF (Result == wxid_no)
{
Event.veto ();
DESTROY = FALSE;
}
}
}
IF (Destroy)
{
DESTROY ();
}
What is CANVETO returns a FALSE? You will not ban this event, your program will exit.
Blocking event processor Plug An Event Handler
Consider the following problem: Each menu command must be recorded. A solution is to create a function called in each command event handler. The problem brought about this method is to make maintenance very difficult. This menu command will not be recorded when adding a new menu and does not call this function.
Solving this problem is to create a new event processor and add to a wxwindow class. To complete it needs to create a new class derived from WXEvthandler. Processing in the new class is the same as a normal window.
Example 3.5. LogeventHandler.h -LogeventHandler definition
#ifndef _logeventhandler_h
#define _logeventhandler_h
Class Logeventhandler: Public WXEVTHANDALEER: PUBLIC
{
PUBLIC:
LOGEVENTHANDLER (): wxevthandler ()
{
}
Virtual ~ LOGEVENTHANDLER ()
{
}
protected:
Declare_event_table ()
Void Onmeu (WXMENUEVENT & Event);
Private:
}
#ENDIF / / _LOGEVENTHANDLER_H
Example 3.6. LOGEVENTHANDLER.CPP - LOGEVENTHANDLER
// for Compilers That Supports Precompilation, Includes "WX / WX.H"
#include "wx / wxprec.h"
#ifndef wx_precomp
#include "wx / wx.h"
#ENDIF
#include "LOGEVENTHANDLER.H"
Void LOGEVENTHANDLER :: OnMenu (WXMENUEVENT & Event)
{
WXLogMessage ("Someone SELECTED A Menu Item);
Event.skip ();
}
Begin_Event_Table (LOGEVENTHANDLER, WXEVTHANDAL)
EVT_MENU_RANGE (-1, -1, LOGEVENTHANDLER :: Onmenu)
END_EVENT_TABLE ()
In the macro evt_menu_range, all menu events can be processed. The first two parameters are used to specify the ID range of the menu to be processed. -1 represents all menu items. Don't forget to call Skip on the event, otherwise there will be no events pass to the event processor of class wxwindow. The next step is to press the new event processor into the processor stack. This is done using WXWindow's PUSHEVENTHANDLER. To remove the event processor from the stack Using PopeventHandler.
PusheventHandler (New LogeventHandler ());
PopeventHandler has a Boolean parameter that is true when this parameter is true, WXWindows deletes event processors. Note When there is no event processor being taken out, WXWndows will try to delete the event processor when the window will undo. This occurs an access error when accessing an event processor created on the stack. This issue can be avoided before a class revoked PopeVentHandler can avoid this problem.
Chapter 4. General dialog
Table of contents
WXFiledialog
Constructor
Methods
EXAMPLE
WXFileSelector
WXCOLOURDIALOG
Constructor
Methods
WxcolourData
EXAMPLE
WxgetCOLOURFROMUSER
WXFONTDIALOGOG
Constructor
Methods
WXFONTDATA
EXAMPLE
WXPrintDialog
Constructor
Methods
EXAMPLE
WXDIRDIALOG
Constructor
Methods
EXAMPLE
WXDIRSELECTOR
WXTEXTENTRYDIALOGOGOG
Constructor
Methods
EXAMPLE
WxgetTextFromuser
WxGetPasswordFromuser
WxMessageDialog
Constructor
Methods
EXAMPLE
WxMessageBox
WXSinglechoiceDialog
Constructor
Methods
EXAMPLE
Wxgetsinglechoice
WxgetsinglechoiceIndex
WxgetsinglechoiceData
In the example of the second chapter, there is a menu command to open and store a text file. At this point, you should use a general dialog wxfiledialog to allow users to select or enter a file name. When using a general dialog, the user gets a unified interface. This allows users to see similar dialogs regardless of what kind of programs.
WXWindows offers the following general dialogs.
WXFiledialog is a dialog box that stores or opens a file. WXCOLOURDIALOG a selection color dialog. WXFONTDIALOG Select the dialog box. WXPrintDialog is used to print a dialog box for setting up printers. WXDIRDIALOG selection directory dialog box. WXTEXTENTRYDIALOG requests a user into a dialog box for a line. WxMessageDialog is used to display a row or multi-line information, which can include OK, Yes, NO, with the Cancel button. WXSINGLECHOICEDIALOG displays a dialog box containing a string list and allows users to select one.
WXWindows is the Framework for cross platforms. WXWindow will display a general dialog when a general dialog box cannot be used on a particular platform.
note
In the example of each dialog box below, the Destroy method will be used instead of the method of deleting pointers to undo the window. This is for a stable purpose. WXWindows controls should always be created on the heap and use the Destroy method to be safely removed. Because wxwindows sometimes deletes until all events are processed. This way WXWindows avoids sending events to a window that does not exist. WXFiledialog
WXFiledialog provides users with a dialog box that can select or enter a file name when a user is turned on or stored. A global method WXFileSelector can also be used to allow users to choose a file. Please see the section "WXFileSelector" for details.
Constructor
#include
wxFileDialog (wxWindow * parent, const wxString & message = "Choose a file", const wxString & defaultDir = "", const wxString & defaultFile = "", const wxString & wildcard = "", long style = 0, const wxPoint & pos = wxDefaultPosition);
PARENT has a window of this dialog. The headline of the message dialog. DefaultDir default directory. Defaultfile default file name. Wildcard is some strings like "*. *" * .txt ". This is used to filter files. Its syntax is description | suffix. Example "All Files (*. *) | *. * | Text files (*. Txt) | * .txt | Bitmap files (*. Bmp) | * .bmp". Style dialog. Table 4.1. Wxfiledialog style
WXChange_dir change the current directory wxfile_must_exist Enter file must exist that WXHIDE_READOONLY hide read-only file wxmultiple is just an open dialog, allowing multiple files to be selected. WxOpen is just an open dialog. WxOverWrite_Prompt is determined when a file will overwrite. WXSave a save dialog. POS is not used.
method
Public void setDirectory (const wxstring & dir); public wxstring getdirectory ();
Set with your current directory
Public void setFileName (const wxstring & name); public wxstring getFileName ();
Set with get the current file name
Public const void getFilenames (wxaRrayString & Files);
Use the selected file name to add a file name array. This function is only used for WXMultiPle dialogs. Other use GetFileName.
Public void setfilterindex (int filterindex); public int getFilterIndex ();
Set the index with the list of filters given. The filter is first used when the dialog is displayed before the dialog box is displayed. This index is selected by the user after display.
Public Void SetMessage (Const WxString & Message); Public wxString getMessage ();
Set the title to the new dialog box.
Public void setpath; public wxstring getpath (); settings and get the current path (directory and file name)
Public void setStyle (long style); public long getStyle ();
Set the style to get the dialog box, see "constructor"
Public void getPaths (wxaRrayString & Paths);
Use the selected file absolute path name to populate the path array. This function is only used for WXMultiPle dialogs. Other use getPath.
Public void setwildcard; public wxstring getWildcard ();
Set with getting Wildcard See "Constructor".
Public int showModal ();
The dialog box is displayed and returns WXID_OK when the user presses OK. Other situations return wxid_cancel.
example
In the text editor example of the second chapter, use ShowModal to display a dialog. "MODAL" means that other windows of the program before the dialog is closed, you cannot get the focus. When using the ShowModal display dialog, return wxid_ok when the user presses OK. Other situations return wxid_cancel. GetFileName is the file name used to get the selected file. GetPath will return the full path (directory and file name) of the selected file. Loading and saving files is very simple when using WXTextCtrl controls. Method LoadFile and Savefile are used to load and save files, respectively.
Example 4.1. WXFiledIalog
Void textframe :: OnMenuFileOpen (wxcommandevent & evenet)
{
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-> getFilename ());
SetStatustext (DLG-> getFileName (), 0);
}
DLG-> Destroy ();
}
Void textframe :: OnMenufilesave (wxcommandevent & evenet)
{
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 ();
}
WXFileSelector
WXFileSelector pops up a file selection box. Returns an empty string when the user ignores it.
wxString wxFileSelector (const wxString & message, const wxString & defaultPath = "", const wxString & defaultFile = "", const wxString & defaultExtension = "", const wxString & wildcard = "*. *", int flags = 0, wxWindow * parent = NULL, int X = -1, int y = -1); the title of the Message file selector. DefaultPath Default Path I DefaultFile Default File Default The default suffix. This suffix will be automatically added to the file when the user omits the suffix. WXWindows automatically adds a filter when not specified by Wildcard. Wildcard (see wxfiledialog constructor). Flags is the style of the dialog. See WXFiledIalog constructor. The X-axis position of the WXMultiPle style Parent parent window X selector is not supported. The Y-axis position of the Y selector.
example
Example 4.2 Requires Users Select a JavaScript file.
Example 4.2. Use of WXFileSelector.
WXString Selection = WXFileSelector ("Open A JavaScript file", "", "", "js",
"All files (*. *) | *. * | Javascript files (*. Js) | * .js | text files (*. Txt) | * .txt",
WXOpen, this);
WXCOLOURDIALOG
WXCOLOURDIALOG implements a window for users to choose color. WXCOLOURDIALOG is used in common with WXCOLOURDATA. WXCOLOURDATA is used to set the current color in the dialog and to get selected colors.
Constructor
#include
WXCOLOURDIALOG (WXWindow * Parent, wxcolourdata * data = null);
Parent i is the parent window of our dialog. DATA contains color information. It includes default colors, custom colors, and under Windows you can tell it to display a population dialog that contains a custom color selection control.
method
Public wxcolourdata & getColourData ();
Get reference to the Coloudata associated with this dialog.
Public void settitle (const wxstring & title); public wxstring gettitle ();
Set or get the title of the dialog
Public int showModal ();
The dialog box is displayed and returns WXID_OK when the user presses OK. Other situations return wxid_cancel.
WxcolourData
This class contains information related to WXCOLOURDIALOG.
Constructor
WXCOLOURDATA ();
Default constructor
WXCOLOURDATA (Const WxcolourData & Data);
Copy constructor method
Public void setchoosefull (bool flag); public bool getchoosefull ();
On Windows, this method displays a dialog that contains a custom color selection control, the default value is TRUE.
Public void setcolour (wxcolour & color); public wxcolour & getColour ();
Set or get the selected color.
Public Void SetCustomColour (INT I, WXCOLOUR & COLOUR); Public WxColour GetCustomColour (INT I);
Set or get a custom color of a given location. I must be between 0 and 15.
example
Example 4.3 Add a menu item to the program to allow users to select other background colors. The current background colors are given to colourData. If the program is running under Windows, a population dialog will appear.
Example 4.3. WXCOLOURDIALOG
Void TextFrame :: OnMenuOptionBackgroundColor (WxCommandevent & Event)
{
WXCOLOURDATA ColourData;
Wxcolour color = m_ptextctrl-> getBackgroundColour ();
ColourData.SetColour (Colour);
ColourData.Setchoosefull (TRUE);
WXCOLOURDIALOG * DLG = New WxColourDialog (this, & colourData);
IF (dlg-> showmodal () == wxid_ok)
{
ColourData = DLG-> getColourData ();
M_PTextCtrl-> setBackgroundColour (ColourData.GetColour ());
m_ptextctrl-> refresh ();
}
DLG-> Destroy ();
}
WxgetCOLOURFROMUSER
WxGetCOLOURFROMUSER Displays the color selection dialog and returns the color selected by the user or returns an invalid color when the user cancels the dialog. Test the color is valid using WXCOLOUR OK method.
WXCOLOUR WXGETCOLOURFROMUSER (wxwindow * parent = null, const wxcolour & colinit = wxnullcolour);
The parent window of the PARENT selector. Colinit initial colors.
example
Example 4.4 To show a user to choose a color
Example 4.4. WXGETCOLOURFROMUSER
Wxcolour colour = wxgetCOLOURFROMUSER ();
IF (colour.ok ())
{
// User selected color
}
WXFONTDIALOGOG
WXFontDialog is a dialog for users to select fonts. This dialog is used with wxfontdata, wxfont, and wxcolour. WXFontData is used to set the current font within the dialog and get the font.
Constructor
#include
WXCOLOURDIALOG (WXWindow * Parent, wxfontdata * data = null);
The parent window DATA of the Parent dialog box contains font. It contains information such as default fonts, current font colors.
method
Public wxfontdata & getfontdata ();
Get reference to the FontData associated with the dialog.
Public int showModal (); Display dialog box, returns WXID_OK when the user presses OK. Other situations return wxid_cancel.
WXFONTDATA
WXFONTDATA ();
Default constructor
Public void enableeffects (Bool flag); public bool getenableeffects;
On Windows, it is pointed out whether the font effect is used, such as underline, italic, and the like.
Public void setallowsymbols (bool flag); Public Bool getAllowsymbols ();
On Windows, indicating whether the symbol font can be selected.
Public void setColour; Public wxcolour & getcolour ();
Set or get the font color.
Public void setshowhelp (bool flag); public bool getshowhelp ();
ON on Windows, pointing to whether the Help button is displayed.
Public void setInitialFont (const wxfont & font); public wxfont getinitialfont ();
Get or set the original font used by the dialog.
Public wxfont getChosenfont ();
Get the selected font.
Public Void SetRange (int minrange, int maxRANGE);
On Windows, pointing out the maximum and minimum of the font size. By default, 0 to infinite.
example
Example Example 4.5 implements a new menu item to change the font within the editor in the text editor of the second chapter. First, fontdata fills with the current font and color, when the program is running on Windows, the user will see the help button. When the user selects the font, FontData will be assigned to the foreground color of the new font and the text control.
Example 4.5. WXFONTDIALOG
Void textframe :: onMenuoptionFont (wxcommandevent & eve)
{
WXFONTDATA FONTDATA;
WXFONT FONT;
WXCOLOUR COLOUR;
Font = m_ptextctrl-> getFont ();
Fontdata.setInitialFont (font);
Colour = m_ptextctrl-> getForeGroundColour ();
Fontdata.SetColour (Colour);
Fontdata.SetShowhelp (TRUE);
WXFONTDIALOG * DLG = New wxfontdialog (this, & fontdata);
IF (dlg-> showmodal () == wxid_ok)
{
FONTDATA = DLG-> getFontdata ();
Font = fontdata.getchosenfont ();
m_ptextctrl-> setfont (font);
M_PTextCtrl-> setForeGroundColour (fontdata.getcolour ());
m_ptextctrl-> refresh ();
}
DLG-> Destroy ();
}
WXPrintDialog
Todo.
Constructor
Methods
EXAMPLE
WXDIRDIALOG
WXDIRDIALOG is used to select a directory
Constructor
#include
wxDirDialog (wxWindow * parent, const wxString & message = "Choose a directory", const wxString & defaultPath = "", long style = 0, const wxPoint & pos = wxDefaultPosition); parent parent window pointer message header DefaultPath default path style does not use pos not used
method
Public Void SetMessage (Const WxString & Message); Public wxString getMessage ();
Get the title of setting dialog
Void setPath (Const WxString & Path); Public wxString getPath ();
Get with setting the current path
Public int showModal ();
The dialog box is displayed and returns WXID_OK when the user presses OK. Other situations return wxid_cancel.
example
Example 4.6 shows the implementation of the menu movement to set the new work directory. WxGetCWD is used to get the current working directory. When the user selects a directory you can use the getPath method to get the directory name. WXSetworkingDirectory is used to set up a new work directory.
Example 4.6. WXDIRDIALOG.
Void TextFrame :: OnMenuOptionDirectory (wxcommandevent & eve)
{
WXDIRDIALOG * DLG = New WxDirDialog (this, "SELECT A New Working Directory", WXGetCWD ());
IF (dlg-> showmodal () == wxid_ok)
{
WXSetworkingDirectory (DLG-> getPath ());
}
DLG-> Destroy ();
}
WXDIRSELECTOR
WXDIRSELECTOR pops up a directory selection box.
WXSTRING WXDIRSELECTOR (Const WxString & Message, Const WxString & DEFAULTPATH = "", long style = 0, const wxpoint & pos = wxdefaultPosition, wxwindow * parent = null);
Message Title The DefaultPath default path i style dialog, see the WXDIRDIALOGIS POS dialog position. Parent parent window.
example
Example 4.7 requires a user to select a folder.
Example 4.7. Using WXDIRSELECTOR
WXSTRING Selection = WXDIRSELECTOR ("SELECT A Folder");
IF (! Selection.empty ())
{
// The user selected a folder.
}
WXTEXTENTRYDIALOGOGOG
WXTEXTENTRYDIALOG requests a user into a dialog box for a line.
Constructor
#include
wxTextEntryDialog (wxWindow * parent, const wxString & message, const wxString & caption = "Please enter text", const wxString & defaultValue = "", long style = wxOK | wxCANCEL | wxCENTRE, const wxPoint & pos = wxDefaultPosition); parent parent window. The information displayed in the Message dialog box. DefaultValue Default Text. The Caption Title Style dialog. You can use WXTextCtrl style. Table Table 4.2. WXTEXTENTRYDIALOG style styles
WXCancel Displays the Cancel Button Wxcentre Central Dialog WXOK Displays the location of the OK button POS dialog
method
Void SetValue (Const WxString & Val); Public WXString getValue ();
Get the value of the set text area. Get / set the value of the text field.
Public int showModal ();
The dialog box is displayed and returns WXID_OK when the user presses OK. Other situations return wxid_cancel.
example
Example 4.8 Display a dialog prompting the user to enter a password.
Example 4.8. WXTEXTENTRYDIALOG
WXTEXTENTRYDIALOG * DLG = New WXTEXTENTRYDIALOG (this, "Enter Your Password", "Enter Your Password", "", wxok | wxcancel | wxcentre | wxte_password;
IF (dlg-> showmodal () == wxid_ok)
{
// check the password
}
Else
{
// stop the application
}
DLG-> Destroy ();
WxgetTextFromuser
WxgetTextFromuser pops up a dialog that allows users to enter some text.
wxString wxGetTextFromUser (const wxString & message, const wxString & caption = "Input text", const wxString & defaultValue = "", wxWindow * parent = NULL, int x = -1, int y = -1, bool centre = true);
The information displayed in the Message dialog box. In the information, you can include a wrap Caption title DEFAULTVALUE default text. Parent parent window. The X-axis position Y spindle position of the X dialog box. Center: At the center of the true time dialog, other situations are left.
example
Example 4.9 requires a user to enter some text. a
Example 4.9. WXGetTextFromuser uses wxString text = wxgetTextFromuser ("please, enter some text");
WxGetPasswordFromuser
WxGetPasswordFromuser pops up a dialog box for entering a password.
WXString WxGetPasswordFromuser (const wxstring & copy = "input text", const wxstring & defaultValue = ", wxwindow * parent = null);
The information displayed in the Message dialog box. In the information, you can include a wrap Caption title DEFAULTVALUE default text Parent parent window
example
EXAMPLE 4.10 Require User Enter Password
Example 4.10. WXGETPASSWORDFROMUSER
WXSTRING PWD = WXGetPasswordFromuser ("please, enter you password");
WxMessageDialog
WxMessageDialog can be used to display a row or multi-line information, which can contain OK, Yes, NO or Cancel buttons.
Constructor constructor
#include
WxMessageDialog (wxwindow * parent, const wxstring & copy = "message box", long style = wxok | wxcancel | wxcentre, const wxpoint & pos = wxdefaultPosition;
Parent parent window. The information displayed in the Message dialog can be multi-line text. Caption title. Style dialog in style. Table 4.3. WxMessageDialog style. Styles
WXOK displays OK button. wxcancel displays Cancel Button. wxyes_no Displays YES and NO buttons. WXYES_DEFALT uses wxyes_no to let Yes buttons as the default button. This is the default behavior. WXNO_DEFAULT USED with WXYES_NO, let the NO button are default. WXCENTRE Let the information are located in the center, not the window located in the center. WXICON_EXCLAMATION displays an exclamation mark icon. WXICON_HAND displays an error icon. WXICON_ERROR is the same. WXICON_QUESTION shows the question mark icon. WXICON_INFORMATION Display Information (i) icon. The position of the POS dialog box.
Methods method
Public int showModal ();
The dialog box is displayed and returns WXID_OK when the user presses OK. Other situations return wxid_cancel.
example
Example 3.4 Displays WXMessageDialog asks if the user really exits the program.
WxMessageBox
WxMessageBox displays a message. The return value is one of wxyes, wxno, wxcancel, wxok.
WxMessageBox (const wxstring & copy = "message wxstring & caption =" message box ", int style, wxock | wxcent, wxwindow * parent, int x = -1, int y = -1); message displayed. Caption Title Style style. See the WXMessageDialog Parent Parent window. X x axis position. Y Y axis position.
Note Note
Local MessageBox functions on Windows are not using wxcentre style. In the general function, it is used. This is because local functions cannot be centered in text. The symbol is not displayed when the generic function is used.
WXSinglechoiceDialog
Constructor constructor
#include
wxSingleChoiceDialog (wxWindow * parent, const wxString & message, const wxString & caption, int n, const wxString * choices, char ** clientData = (char **) NULL, long style = wxCHOICEDLG_STYLE, const wxPoint & pos = wxDefaultPosition);
Parent parent window. The information displayed in the Message dialog box. Caption title. n entry number. Choices is a string array. ClientData style dialog style. Table 4.4. WXSinglechoiceDialog style
WXOK displays ok button. wxcancel displays Cancel Button. Wxcentre hits the information in non-Windows. WXCHOICEDLG_STYLE WXOK | WXCANCEL | WXCENTRE POS dialog box.
Methods method
Public void setSelection (int SEL);
Set option
Public int getselection ();
Returns the option of the option.
Public wxstring getstringselection ();
Returns the option.
Public int showModal ();
The dialog box is displayed and returns WXID_OK when the user presses OK. Other situations return wxid_cancel.
example
An example shows a dialog to let users choose their country.
Example 4.11. WXSINGLECHOICEDIALOG is used.
WXSTRING Countries [] = {"Belgium", "United Kingdom", "U.S.A.", "France"};
WXSINGLECHOICEDIALOG * DLG = New wxsinglechoiceDialog (NULL, "Where do you live?",
"SELECT a country",
4, Countries);
IF (dlg-> showmodal () == wxid_ok)
{
WxMessageBox ("you live in" DLG-> getStringSelection ());
DLG-> Destroy ();
Wxgetsinglechoice
WXGetSinglechoice displays wxsinglechoiceDialog and returns the selected string.
wxString wxGetSingleChoice (const wxString & message, const wxString & caption, const wxArrayString & choices, wxWindow * parent = (wxWindow *) NULL, int x = -1, int y = -1, bool centre = TRUE, int width = wxCHOICE_WIDTH, int height = wxchoice_height);
wxString wxGetSingleChoice (const wxString & message, const wxString & caption, int n, const wxString * choices, wxWindow * parent = (wxWindow *) NULL, int x = -1, int y = -1, bool centre = TRUE, int width = wxCHOICE_WIDTH , Int Height = wxchoice_HEIGHT);
WxgetsinglechoiceIndex
WxgetSinglechoiceIndex displays wxsinglechoiceDialog and returns the selected index. Returns -1 when no option is selected.
int wxGetSingleChoiceIndex (const wxString & message, const wxString & caption, const wxArrayString & choices, wxWindow * parent = (wxWindow *) NULL, int x = -1, int y = -1, bool centre = TRUE, int width = wxCHOICE_WIDTH, int height = wxCHOICE_HEIGHT); int wxGetSingleChoiceIndex (const wxString & message, const wxString & caption, int n, const wxString * choices, wxWindow * parent = (wxWindow *) NULL, int x = -1, int y = -1, bool centre = TRUE, int Width = wxchoice_WIDTH, INT Height = wxchoice_HEIGHT);
WxgetsinglechoiceData
WXGetSinglechoiceData displays wxsinglechoiceDialog and returns the data associated with the selected item.
void * wxGetSingleChoiceData (const wxString & message, const wxString & caption, const wxArrayString & choices, void ** client_data, wxWindow * parent = (wxWindow *) NULL, int x = -1, int y = -1, bool centre = TRUE, int width = Wxchoice_WIDTH, INT Height = wxchoice_HEIGHT);
void * wxGetSingleChoiceData (const wxString & message, const wxString & caption, int n, const wxString * choices, void ** client_data, wxWindow * parent = (wxWindow *) NULL, int x = -1, int y = -1, bool centre = True, int width = wxchoice_WIDTH, INT Height = wxchoice_HEIGHT); note
The definition of wxchoice_width is 200, and the definition of wxchoice_HEIGHT is 150.
Chapter 5 dialog
Directory Table of Contents
WxDialog
Constructor
Programming Dialogs
SIZERS
SIZERS AND Windows
SIZERS AND Other SIZERS
SIZERS AND SPACERS
WxBoxSizer
Wxgridsizer
WXFlexGridsizer
WxStaticBoxSizer
WxnoteBooksizer
Nesting sizers
The dialog is a window that contains the title bar. This window can be moved, which can contain controls and other windows. The dialog can be modal or non-modular. The modal dialog box is locked in other windows in the program, until the dialog exits to unlock.
WxDialog
WxDialog is the base class of all dialogs. You can customize a dialog inherited from wxdialog.
Constructor constructor
wxDialog (wxWindow * parent, wxWindowId id, const wxString & title, const wxPoint & pos = wxDefaultPosition, const wxSize & size = wxDefaultSize, long style = wxDEFAULT_DIALOG_STYLE, const wxString & name = "dialogBox");
The pointer of the Parent parent window, uses a Null ID independent window ID when the dialog does not have a parent window. If it doesn't need to use -1. Title shows the title in the title bar. The position of the POS dialog box. The default location when using wxdefaultposition. Size window size. The default size is wxdefaultsize. Style dialog in style. Table 5.1. WxDialog style.
WxDefault_dialog_style wxsystem_menu combines WXCAPTION. WXSYSTEM_MENU is not used on the UNIX system. WXCAPTION shows the title. WXMinimize can minimize the same wxminimize_box as wxiconize. WXMaximize to maximize display. (Only for Windows) The wxmaximize_box can maximize. The wxStay_on_top dialog will appear above other windows. (Windows only) WXSystem_Menu displays a system menu. WXSIMPLE_BORDER Bless border (GTK and Windows only) wxResize_border can drag the zoom border. (The name only for UNIX) Name window. This parameter is associated with an option. This allows the user to set the MOTIF resource value for a separate window. Dialog programming
WXWindows requires a resource file when you want to use the dialog box in the program. When you don't need an additional icon, its use is as simple as you see in Example 5.1.
Example 5.1. A simple resource file.
#include "wx / msw / wx.rc"