If you're new here, you may want to subscribe to our RSS feed as well as to our NewsLetter (on Top Right hand menu) to get highly informative articles weekly...and NO, we never spam!如果你是新这里,你可以订阅我们的rss以及我们简讯 (顶右手菜单) ,以获得翔实周刊文章… …不,我们从来垃圾!
wxWidgets is an open source cross-platform C++ programming toolkit that includes many features. 1.2.4是一个开放源代码的跨平台三+编程工具,其中包括许多特点。 It is most often used for developing applications with a GUI, but also provides features for networking, file access, and more.它是最常用的开发应用与贵,而且提供的功能联网,档案存取,以及更多。 With versions available for Windows, Linux, Mac, and even some embedded devices, cross platform programming becomes much easier.同版本的windows , linux系统,陆委会,甚至一些嵌入式设备,跨平台的编程变得容易得多。
I am myself very new to wxWidgets but still have penned down a Beginners Tutorial on wxWidgets -我自己很新的1.2.4 ,但仍然有圈养了初学者对补习1.2.4 -
During this tutorial, a very simple text editor will be constructed that can load and save text files.在本教程中,一个很简单的文本编辑器将兴建能负荷并节省文本文件。 The tutorial will focus on the application object, the main window, and the event table.该教程将集中适用对象,主要窗口,以及事件表。
The Application 应用
All wxWidgets applications are derived from wxApp , and simply need to override a single member, wxApp::OnInit , and create a window. 1.2.4所有申请均来自wxapp ,只需凌驾单一会员, wxapp : : oninit ,创造一个窗口。 As long as the window is open, the application is as well.只要窗口打开,应用等。 The following code illustrates the code needed.以下代码说明代码。
#include “wx/wx.h”class MyApplication : public wxApp #包括"蜡质 / wx.h "级myapplication :公共wxapp { ( public : 市民 : bool OnInit ( ) ;</code> bool oninit ( ) ; < /代码> } ;bool MyApplication:: OnInit ( ) { ) ; bool myapplication : : oninit ( ) ( // TODO: Show window / /待办事项:橱窗 return false ; 返回假 ; } IMPLEMENT_APP ( MyApplication ) ; ) implement_app ( myapplication ) ;
The first line includes most of the header files needed for a wxWidgets application.一线包括了大部分的头文件,需要一个应用1.2.4 。 Then a new class is derived from wxApp called MyApplication, and the MyApplication::OnInit() function is created.当时新一类是来自wxapp myapplication呼吁,并myapplication : : oninit ( )函数创建。 Finally, IMPLEMENT_APP(MyApplication), is needed to cause the application to run.最后, implement_app ( myapplication ) ,是需要引起应用运行。 This macro expands to the correct code for any given platform to actually start the application.这一宏观扩展到正确的代码为任何特定的平台,真正开始应用。
The Frame in wxWidgets 框1.2.4
After creating the application, the window frame needs to be created and controls need to be created on it.经过创建申请,窗框需要创造和控制必须创造它。 The frame object is derived from wxFrame and, as shown below, includes several functions and the text control.帧对象是来自wxframe ,所示,其中包括若干职能和文字控制。
class MyFrame : public wxFrame myframe 类 : 公共 wxframe { ( public : MyFrame ( ) ; void OnNew ( wxCommandEvent& evt ) ; 市民 : myframe ( ) ; 无效 onnew ( wxcommandevent & evt ) ; void OnOpen ( wxCommandEvent& evt ) ; 无效 onopen ( wxcommandevent & evt ) ; void OnSave ( wxCommandEvent& evt ) ;private: 无效 onsave ( wxcommandevent & evt ) ;私人: void CreateWidgets ( ) ; 无效 createwidgets ( ) ; wxTextCtrl* m_text; wxtextctrl * m_text ; } ; ) ; MyFrame:: MyFrame ( ) : myframe : : myframe ( ) : wxFrame ( NULL , wxID_ANY, wxT ( "Sample Program" ) ) wxframe ( 无效 wxid_any , wxt ( " 样本程序" ) ) { ( CreateWidgets ( ) ; createwidgets ( ) ; } ) void MyFrame:: OnNew ( wxCommandEvent& WXUNUSED ( evt ) ) 无效 myframe : : onnew ( wxcommandevent & wxunused ( evt ) ) { ( m_text->Clear ( ) ; m_text - >明确( ) ; } ) void MyFrame:: OnOpen ( wxCommandEvent& WXUNUSED ( evt ) ) 无效 myframe : : onopen ( wxcommandevent & wxunused ( evt ) ) { ( wxString file = :: wxFileSelector ( wxT ( "Open file" ) ) ; wxstring 档案 = : : wxfileselector ( wxt ( "打开文件" ) ) ; if ( file . IsEmpty ( ) == false ) 如果 ( 档案 。 isempty ( ) == 虚假 ) m_text->LoadFile ( file ) ; m_text - > loadfile ( 档案 ) ; } ) void MyFrame:: OnSave ( wxCommandEvent& WXUNUSED ( evt ) ) 无效 myframe : : onsave ( wxcommandevent & wxunused ( evt ) ) { ( wxString file = :: wxFileSelector ( wxT ( "Save file" ) ) ; wxstring 档案 = : : wxfileselector ( wxt ( "拯救档案" ) ) ; if ( file . IsEmpty ( ) == false ) 如果 ( 档案 。 isempty ( ) == 虚假 ) m_text->SaveFile ( file ) ; m_text - > savefile ( 档案 ) ; } ) void MyFrame:: CreateWidgets ( ) 无效 myframe : : createwidgets ( ) { ( // Create a text control / /创建一个文本控制 m_text = new wxTextCtrl ( this , wxID_ANY, wxEmptyString, m_text = 新 wxtextctrl ( 本 , wxid_any , wxemptystring , wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_TAB | wxdefaultposition , wxdefaultsize , wxte_process_tab | wxTE_PROCESS_ENTER | wxTE_MULTILINE ) ; wxte_process_enter | wxte_multiline ) ; // Create file menu / /创建文件菜单 wxMenu* fileMenu = new wxMenu ( ) ; wxmenu * filemenu = 新 wxmenu ( ) ; fileMenu->Append ( wxID_NEW, wxT ( "New File" ) ) ; filemenu - >后缀( wxid_new , wxt ( "新文件" ) ) ; fileMenu->Append ( wxID_OPEN, wxT ( "Open File" ) ) ; filemenu - >后缀( wxid_open , wxt ( "打开文件" ) ) ; fileMenu->Append ( wxID_SAVE, wxT ( "Save File" ) ) ; filemenu - >后缀( wxid_save , wxt ( "拯救档案" ) ) ; // Create menu bar / /创建菜单栏 wxMenuBar* menuBar = new wxMenuBar ( ) ; wxmenubar *掏金= 新 wxmenubar ( ) ; menuBar->Append ( fileMenu, wxT ( "File" ) ) ;傅说- >后缀( filemenu , wxt ( "文件" ) ) ; // Set menu / /设定菜单 SetMenuBar ( menuBar ) ; setmenubar (傅说) ; } )
All of that code creates the window.所有这些代码创建窗口。 The constructor MyFrame::MyFrame calls the parent constructor wxFrame::wxFrame to create the frame and give it a name, and then it calls CreateWidgets, to create the contents of the frame.构造myframe : : myframe呼吁家长构造wxframe : : wxframe创造框架和给它一个名字,然后它要求createwidgets ,创造的内容框架。 The MyFrame::CreateWidgets function creates a text control and a menu bar.该myframe : : createwidgets函数创建一个文本控制和一个菜单栏。 In addition, function MyFrame::OnNew , MyFrame::OnOpen , and MyFrame::OnSave are implemented to clear, open, or save the text to a file.此外,在功能myframe : : onnew , myframe : : onopen , myframe : : onsave实施明确的,公开的,或保存文字档案。 Also notice that text constants are enclosed in wxT(...).公告还指出,文字常量密封在wxt (...). Since wxWidgets can compile in Unicode as well, wrapping text constants like this ensure that they are declare correctly.自1.2.4可以编译unicode的,以及包文常量这样确保它们是正确申报。
The event Table in wxWidgets 事件表1.2.4
The event table is used to route events from controls, menus, and anything else to the correct functions to handle the events.事件表是用来活动路线从控制,菜单以及鼓捣正确功能,以处理这些事件。 Without them, this application would not be able to load or save the text.没有他们,这个应用将无法负荷或保存文本。 The following changes are needed to implement the event table.以下有需要改变,以落实事件表。
class MyFrame : public wxFrame myframe 类 : 公共 wxframe { ( ... … … ... DECLARE_EVENT_TABLE ( ) ; ... declare_event_table ( ) ; } ; ) ; BEGIN_EVENT_TABLE ( MyFrame, wxFrame ) begin_event_table ( myframe , wxframe ) EVT_MENU ( wxID_NEW, MyFrame:: OnNew ) evt_menu ( wxid_new , myframe : : onnew ) EVT_MENU ( wxID_OPEN, MyFrame:: OnOpen ) evt_menu ( wxid_open , myframe : : onopen ) EVT_MENU ( wxID_SAVE, MyFrame:: OnSave ) evt_menu ( wxid_save , myframe : : onsave ) END_EVENT_TABLE ( ) end_event_table ( )
First, the event table for the frame is declared.首先,如果表框是申报。 Then the table is created, and the event ids wxID_NEW, wxID_OPEN, and wxID_SAVE are used to call the functions MyFrame::OnNew, MyFrame::OnOpen, and MyFrame::OnSave.那么表创建和入侵事件wxid_new , wxid_open , wxid_save使用电话功能myframe : : onnew , myframe : : onopen , myframe : : onsave 。 The events generated by the menus will get processed by the correct functions.事件所产生的选单将会得到处理正确职能。
Showing the frame 显示框
Now that the frame is complete, a small change is needed to the application.现在框架是完整的,一小变,是需要申请。 It needs to create the window frame, show it, and set it as the application top window.它需要创造窗框,显示它,并把它作为应用天窗。
bool MyApplication:: OnInit ( ) bool myapplication : : oninit ( ) { ( // Create and show the frame / /创建和显示框 MyFrame* frame = new MyFrame ( ) ; myframe *帧= 新 myframe ( ) ; frame->Show ( ) ; SetTopWindow ( frame ) ;帧>展( ) ; settopwindow (架) ; return true ; 回到真实 ; } )
The complete code 完整代码
#include "wx/wx.h"// Main window frame #包括"蜡质/ wx.h " / /主窗框 class MyFrame : public wxFrame myframe 类 : 公共 wxframe { ( public : 市民 : MyFrame ( ) ; myframe ( ) ; void OnNew ( wxCommandEvent& evt ) ; 无效 onnew ( wxcommandevent & evt ) ; void OnOpen ( wxCommandEvent& evt ) ; 无效 onopen ( wxcommandevent & evt ) ; void OnSave ( wxCommandEvent& evt ) ; 无效 onsave ( wxcommandevent & evt ) ; private : 私人 : void CreateWidgets ( ) ; 无效 createwidgets ( ) ; wxTextCtrl* m_text; wxtextctrl * m_text ; DECLARE_EVENT_TABLE ( ) ; declare_event_table ( ) ; } ; ) ; MyFrame:: MyFrame ( ) : myframe : : myframe ( ) : wxFrame ( NULL , wxID_ANY, wxT ( "Sample Program" ) ) wxframe ( 无效 wxid_any , wxt ( " 样本程序" ) ) { ( CreateWidgets ( ) ; createwidgets ( ) ; } ) void MyFrame:: OnNew ( wxCommandEvent& WXUNUSED ( evt ) ) 无效 myframe : : onnew ( wxcommandevent & wxunused ( evt ) ) { ( m_text->Clear ( ) ; m_text - >明确( ) ; } ) void MyFrame:: OnOpen ( wxCommandEvent& WXUNUSED ( evt ) ) 无效 myframe : : onopen ( wxcommandevent & wxunused ( evt ) ) { ( wxString file = :: wxFileSelector ( wxT ( "Open file" ) ) ; wxstring 档案 = : : wxfileselector ( wxt ( "打开文件" ) ) ; if ( file . IsEmpty ( ) == false ) 如果 ( 档案 。 isempty ( ) == 虚假 ) m_text->LoadFile ( file ) ; m_text - > loadfile ( 档案 ) ; } ) void MyFrame:: OnSave ( wxCommandEvent& WXUNUSED ( evt ) ) 无效 myframe : : onsave ( wxcommandevent & wxunused ( evt ) ) { ( wxString file = :: wxFileSelector ( wxT ( "Save file" ) ) ; wxstring 档案 = : : wxfileselector ( wxt ( "拯救档案" ) ) ; if ( file . IsEmpty ( ) == false ) 如果 ( 档案 。 isempty ( ) == 虚假 ) m_text->SaveFile ( file ) ; m_text - > savefile ( 档案 ) ; } ) void MyFrame:: CreateWidgets ( ) 无效 myframe : : createwidgets ( ) { ( // Create a text control / /创建一个文本控制 m_text = new wxTextCtrl ( this , ID_TEXTCTRL, wxEmptyString, m_text = 新 wxtextctrl ( 本 , id_textctrl , wxemptystring , wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_TAB | wxdefaultposition , wxdefaultsize , wxte_process_tab | wxTE_PROCESS_ENTER | wxTE_MULTILINE ) ; wxte_process_enter | wxte_multiline ) ; // Create file menu / /创建文件菜单 wxMenu* fileMenu = new wxMenu ( ) ; wxmenu * filemenu = 新 wxmenu ( ) ; fileMenu->Append ( wxID_NEW, wxT ( "New File" ) ) ; filemenu - >后缀( wxid_new , wxt ( "新文件" ) ) ; fileMenu->Append ( wxID_OPEN, wxT ( "Open File" ) ) ; filemenu - >后缀( wxid_open , wxt ( "打开文件" ) ) ; fileMenu->Append ( wxID_SAVE, wxT ( "Save File" ) ) ; filemenu - >后缀( wxid_save , wxt ( "拯救档案" ) ) ; // Create menu bar / /创建菜单栏 wxMenuBar* menuBar = new wxMenuBar ( ) ; wxmenubar *掏金= 新 wxmenubar ( ) ; menuBar->Append ( fileMenu, wxT ( "File" ) ) ;傅说- >后缀( filemenu , wxt ( "文件" ) ) ; // Set menu / /设定菜单 SetMenuBar ( menuBar ) ; setmenubar (傅说) ; } ) // Event table to route menu events to functions / /事件表干线事件菜单功能 BEGIN_EVENT_TABLE ( MyFrame, wxFrame ) begin_event_table ( myframe , wxframe ) EVT_MENU ( wxID_NEW, MyFrame:: OnNew ) evt_menu ( wxid_new , myframe : : onnew ) EVT_MENU ( wxID_OPEN, MyFrame:: OnOpen ) evt_menu ( wxid_open , myframe : : onopen ) EVT_MENU ( wxID_SAVE, MyFrame:: OnSave ) evt_menu ( wxid_save , myframe : : onsave ) END_EVENT_TABLE ( ) end_event_table ( ) // Application / /应用 class MyApplication : public wxApp myapplication 类 : 公共 wxapp { ( public : 市民 : bool OnInit ( ) ; bool oninit ( ) ; } ; ) ;