First WxWidgets program __wxwidgets

Source: Internet
Author: User
Tags wxwidgets
setting up the development environment

Before you create your application, you need to set up related variables for the Visual Studio development environment and wxwidgets, including: Include Directories: include wxwidgets header files. In the project properties → configuration properties →c++ directory → include directories item, add C:\wxWidgets-3.0.2\include\msvc and c:\ wxwidgets-3.0.2\include. Library directory: contains wxwidgets library files. In the project properties → configuration properties →c++ directory → library directory item, add C:\wxWidgets-3.0.2\lib\vc_dll. System environment variable: Sets the wxwidgets dynamic link library file directory. In the Windows Control Panel system → advanced system settings → advanced → environment variables → system variables, add C:\wxWidgets-3.0.2\lib\vc_dllto the contents of the PATH variable. Preprocessor definition: Set up wxwidgets macros related to Windows platform, mainly including _wxmsw_,wxusingdll , etc. Include the header files required by the program in your program.

Where the preprocessor defines and contains the header files required by the program two steps can be centrally resolved by creating a header file WxInc.h, which is only included in the user code.

WxInc.h

#ifndef _wx_inc_h_
#define _WX_INC_H_

#define __WXMSW__   ///use Windows platform
#define Wxusingdll  ///Use the dynamic link library

#ifdef _DEBUG
#define __WXDEBUG__  ///use wxwidgets debug
#endif//_debug

#include "wx/config.h"

#include "wx/wx.h"       ///use Wxwidgets Universal features and Classes
#include "wx/aui/aui.h"/// Use the AUI
#include "wx/artprov.h"  ///use a predefined icon resource

#endif///_wx_inc_h_
Simple Window Program

The simplest wxwidgets window program requires 2 classes:
(1) The Wxapp class, which represents the application and is responsible for the message loop.
(2) Wxframe, representing the window, handling all kinds of window events.

The following creates a simple window program. The program works as shown in the following illustration.

Simple window program creation steps are as follows:
(1) Derive user class MyApp from Wxapp.
Wxapp uses an MFC-like macro definition and processing mechanism to declare and define program global variables (that is, entry points for programs) through Declare_app () and Implement_app () two macros.

MyApp.h

#include "wxInc.h"

class Myapp:public Wxapp
{public
:
    virtual bool OnInit ();
};

Declare_app (MYAPP)

If Declare_app () and implement () are not declared, the error is compiled as follows:
MSVCRTD.lib (crtexew.obj): Error LNK2019: unresolved external symbol _winmain@16, which is referenced in function ___tmaincrtstartup

(2) overload the Wxapp::oninit function to create and display the main window within the function.

MyApp.cpp

#include "MyApp.h"

Implement_app (MYAPP)

bool Myapp::oninit ()
{
    ///1. Create a window
    Wxframe * Mainwnd = new Wxframe (NULL, Wxid_any, Wxt ("main Window"));                    

    2. Set the window to Program main window
    Settopwindow (mainwnd); 

    3. Display the window.
    Mainwnd->show (true);   

    return true;
}

The Wxpython version of the above procedure is as follows (simple and straightforward):

myapp1.py

Import wx

if __name__ = = "__main__":
    app = wx. APP ()
    Mainwnd = wx. Frame (None,-1, "main Window")
    app. Settopwindow (Mainwnd)
    mainwnd.show ()

    app. Mainloop ()
add menus, toolbars, and status bars

The previous program shows a "Whiteboard" window with no practical meaning. As an application, you must provide the user with the content they need by interacting with the user. In an application, the simplest way to interact with a user is through menus, toolbars, status bars, and so on.

Next, we'll create an application interface with menus, toolbars, and status bars. The program works as shown in the following illustration.

The program's main interface elements include: 2 menu items, each menu item also contains 2 subkeys 1 toolbars, the toolbar has 3 buttons 1 status bar, the status bar is divided into 3 display area

The specific steps are as follows:
(1) Customize the window class to create menus, toolbars, and status bars in the window class.

MyFrame.h

#pragma once

#include "wxInc.h"

class Myframe:public Wxframe
{public
:
    MyFrame ();
};
MyFrame.cpp #include "MyFrame.h" enum {id_item_1 = wxid_highest + 1, id_item_2, Id_item_3, id_item_

4, Id_item_5, Id_item_6, Id_item_7,}; Myframe::myframe (): Wxframe (NULL, Wxid_any, Wxt ("main window") {///1. Create menu bar Wxmenubar * MenuBar = new Wxmenuba

    R ();
    Wxmenu * menu1 = new Wxmenu ();
    Menu1->append (Id_item_1, Wxt ("Subitem 1-1"));
    Menu1->append (Id_item_2, Wxt ("Subitem 1-2"));

    Menubar->append (menu1, WXT ("Item 1"));
    Wxmenu * menu2 = new Wxmenu ();
    Menu2->append (Id_item_3, Wxt ("Subitem 2-1"));
    Menu2->append (Id_item_4, Wxt ("Subitem 2-2"));

    Menubar->append (MENU2, Wxt ("Item 2"));

    Setmenubar (MenuBar); 2.
    Create Toolbar Wxtoolbar * ToolBar = new Wxtoolbar (this, wxid_any);
    Toolbar->addtool (Id_item_5, Wxt ("Button1"), Wxartprovider::getbitmap (Wxart_go_back, Wxart_toolbar));
    Toolbar->addtool (Id_item_6, Wxt ("Button2"), Wxartprovider::getbitmap (Wxart_tip, Wxart_toolbar)); ToolbaR->addseparator ();
    Toolbar->addtool (id_item_7, Wxt ("Button3"), Wxartprovider::getbitmap (Wxart_undo, Wxart_toolbar));  Toolbar->realize ();    

    This function must be called after the button is added Settoolbar (ToolBar); 3.
    Create status bar Wxstatusbar * StatusBar = Createstatusbar (3);
    Statusbar->setstatustext (WXT ("This is pane 0"), 0);
    Statusbar->setstatustext (WXT ("This is Pane 1"), 1);
Statusbar->setstatustext (WXT ("This is Pane 2"), 2); }

(2) The instance of the window class as the main window

MyApp.cpp

#include "MyApp.h"

Implement_app (MYAPP)

bool Myapp::oninit ()
{
    ///1. Create a window
    Wxframe * Mainwnd = new Wxframe (NULL, Wxid_any, Wxt ("main Window"));                    

    2. Set the window to Program main window
    Settopwindow (mainwnd); 

    3. Display the window.
    Mainwnd->show (true);   

    return true;
}

At this point, the code is complete and the expected interface can be obtained after the compile execution. However, this program simply shows the interface elements and does not perform message processing. The message Processing Section will also be added on this basis later.

The following points should be noted in the above procedures:
(1) The AddTool function of Wxtoolbar, after version 3.0.2, should use the proposed function signature to avoid compiler alarms.
(2) Wxtoolbar after calling the AddTool Add button, you must call the realize () function to make the toolbar update.
(3) The Wxartprovider can be used to obtain the predefined image resources.

The Wxpython version of the above program is as follows (and not complicated):

myapp1.py Import WX class MyFrame (WX. Frame): def __init__ (self): WX. Frame.__init__ (self, None,-1, "main Window") #1. Add menu bar MenuBar = wx. MenuBar () menu1 = WX. Menu () menu1. Append ( -1, "subitem 1-1") menu1. Append ( -1, "subitem 1-2") Menubar.append (Menu1, "Item 1") Menu2 = wx. Menu () menu2. Append ( -1, "subitem 2-1") menu2. Append ( -1, "subitem 2-2") Menubar.append (MENU2, "Item 2") self. Setmenubar (MenuBar) #2. Add Toolbar ToolBar = wx. ToolBar (self) toolbar.addtool ( -1, WX. Artprovider.getbitmap (WX. Art_go_back, WX. Art_toolbar)) Toolbar.addtool ( -1, WX. Artprovider.getbitmap (WX. Art_tip, WX. Art_toolbar)) Toolbar.addseparator () Toolbar.addtool ( -1, WX. Artprovider.getbitmap (WX. Art_undo, WX. Art_toolbar)) toolbar.realize () self. Settoolbar (ToolBar) #3. Add status bar StatusBar = self. Createstatusbar (3) Statusbar.setstatustexT ("This are pane 0", 0) Statusbar.setstatustext ("This is Pane 1", 1) statusbar.setstatustext ("This is pane 2 ", 2" if __name__ = = "__main__": App = wx. App () Mainwnd = MyFrame () app. Settopwindow (Mainwnd) mainwnd.show () app. Mainloop () #启动消息循环
Add Message Processing

There are two main modes of wxwidgets message processing:
(1) Static binding message handler function
One is a predefined pattern similar to MFC. Maps messages to specified functions through macros such as declare_event_table,begin_event_table, andend_event_table .
Here is an example to illustrate the definition and use of event-related macros.

MyFrame.h

class Myframe:public wxframe
{
    declare_event_table () public

:
    myframe ();

Public:
    void OnItem1 (Wxcommandevent & evt);

MyFrame.cpp

begin_event_table (MyFrame, Wxframe)
    Evt_menu (id_item_1, OnItem1)
end_event_table () ...

void Myframe::onitem1 (Wxcommandevent & evt)
{
    Wxmessagebox (WXT ("Item 1"));
}

In the example above, bind the menu event with ID id_item_1 to the myframe OnItem1 function. When the menu item is selected, the MyFrame OnItem1 function is called.

(2) dynamic binding message processing functions
Binds the message to the specified handler function (the function object) by calling the bind function.
Here's an example of how bind can be used:

MyFrame.cpp

myframe::myframe (...)
{
    Bind (wxevt_command_menu_selected, &myframe::onexit, this, wxid_exit);
}

In the example above, bind the menu event with ID wxid_exit to the myframe onexit function. When the menu item is selected, the MyFrame onexit function is called.

Below, on the basis of the previous window program, using both static and dynamic binding methods to increase message processing, to form a simple interaction program. When a program selects a different menu item, the corresponding dialog box pops up. The operation effect is as follows:

Next, modify the MyFrame.h and MyFrame.cpp programs separately, the code is as follows:

MyFrame.h

#pragma once

#include "wxInc.h"

class Myframe:public wxframe
{
    Declare_event_ TABLE () public

:
    myframe ();

Public:
    void OnItem1 (Wxcommandevent & evt);
    void OnItem2 (Wxcommandevent & evt);
MyFrame.cpp #include "MyFrame.h" enum {id_item_1 = wxid_highest + 1, id_item_2, Id_item_3, id_item_

4, Id_item_5, Id_item_6, Id_item_7,};  static binding message processing begin_event_table (MyFrame, Wxframe) evt_menu (id_item_1, OnItem1) end_event_table () Myframe::myframe ()

    : Wxframe (NULL, Wxid_any, Wxt ("main window") {///1. Create menu bar Wxmenubar * MenuBar = new Wxmenubar ();
    Wxmenu * menu1 = new Wxmenu ();
    Menu1->append (Id_item_1, Wxt ("Subitem 1-1"));
    Menu1->append (Id_item_2, Wxt ("Subitem 1-2"));

    Menubar->append (menu1, WXT ("Item 1"));
    Wxmenu * menu2 = new Wxmenu ();
    Menu2->append (Id_item_3, Wxt ("Subitem 2-1"));
    Menu2->append (Id_item_4, Wxt ("Subitem 2-2"));

    Menubar->append (MENU2, Wxt ("Item 2"));

    Setmenubar (MenuBar); 2.
    Create Toolbar Wxtoolbar * ToolBar = new Wxtoolbar (this, wxid_any);
    Toolbar->addtool (Id_item_5, Wxt ("Button1"), Wxartprovider::getbitmap (Wxart_go_back, Wxart_toolbar)); ToolbaR->addtool (Id_item_6, Wxt ("Button2"), Wxartprovider::getbitmap (Wxart_tip, Wxart_toolbar));    
    Toolbar->addseparator ();
    Toolbar->addtool (id_item_7, Wxt ("Button3"), Wxartprovider::getbitmap (Wxart_undo, Wxart_toolbar));  Toolbar->realize ();    

    This function must be called after the button is added Settoolbar (ToolBar); 3.
    Create status bar Wxstatusbar * StatusBar = Createstatusbar (3);
    Statusbar->setstatustext (WXT ("This is pane 0"), 0);
    Statusbar->setstatustext (WXT ("This is Pane 1"), 1);

    Statusbar->setstatustext (WXT ("This is Pane 2"), 2); 4.
Dynamic bind message bind (wxevt_command_menu_selected, &myframe::onitem2, this, id_item_2);

} void Myframe::onitem1 (Wxcommandevent & evt) {Wxmessagebox (WXT ("Item 1")} void Myframe::onitem2 (Wxcommandevent & evt) {Wxmessagebox (WXT ("Item 2"));}

Because Python does not have wxwidgets macro-definition processing, it needs to be handled dynamically by binding messages as follows:

myframe.py Import WX class MyFrame (WX.        

        Frame): def __init__ (self): id_item1 = wx.id_highest + 1 id_item2 = wx.id_highest + 2 Wx. Frame.__init__ (self, None,-1, "main Window") #1. Add menu bar MenuBar = wx. MenuBar () menu1 = WX. Menu () menu1. Append (Id_item1, "subitem 1-1") menu1. Append (id_item2, "subitem 1-2") Menubar.append (Menu1, "Item 1") Menu2 = wx. Menu () menu2. Append ( -1, "subitem 2-1") menu2. Append ( -1, "subitem 2-2") Menubar.append (MENU2, "Item 2") self. Setmenubar (MenuBar) #2. Add Toolbar ToolBar = wx. ToolBar (self) toolbar.addtool ( -1, WX. Artprovider.getbitmap (WX. Art_go_back, WX. Art_toolbar)) Toolbar.addtool ( -1, WX. Artprovider.getbitmap (WX. Art_tip, WX. Art_toolbar)) Toolbar.addseparator () Toolbar.addtool ( -1, WX. Artprovider.getbitmap (WX. Art_undo, WX. Art_toolbar)) toolbar.realize () self. Settoolbar (ToolBAR) #3. Increase the status bar StatusBar = self. Createstatusbar (3) Statusbar.setstatustext ("This are pane 0", 0) Statusbar.setstatustext ("This is pane 1", 1) Statusbar.setstatustext ("This is Pane 2", 2) #4. Add Message processing self. Bind (WX. Evt_menu, self. ONITEM1, id=id_item1) self. Bind (WX. Evt_menu, self. ONITEM2, ID=ID_ITEM2) def OnItem1 (Self, event): WX. MessageBox ("Item 1") Pass Def OnItem2 (self, event): WX. MessageBox ("Item 2") Pass if __name__ = = "__main__": App = wx. App () Mainwnd = MyFrame () app. Settopwindow (Mainwnd) mainwnd.show () app. Mainloop () #启动消息循环

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.