1、按鈕框架
在接下來的這個例子中,咱們在框架上建立了一個按鈕。咱們將會看到,如何創建一個簡單的事件處理程序。在這個實例中,爲按鈕綁定處理事件,使用的是靜態時間表的方法
函數
1 BEGIN_EVENT_TABLE(MyFrame, wxFrame) 2 EVT_MENU(wxID_ABOUT, MyFrame::OnAbout) 3 EVT_MENU(wxID_EXIT, MyFrame::OnQuit) 4 EVT_BUTTON(wxID_EXIT, MyFrame::OnQuit) 5 END_EVENT_TABLE()
main.hui
1 #include <wx/wx.h> 2 //定義主窗口類 3 class MyFrame : public wxFrame 4 { 5 public: 6 MyFrame(const wxString& title); 7 8 //定義事件處理函數 9 void OnQuit(wxCommandEvent& event); 10 void OnAbout(wxCommandEvent& event); 11 private: 12 //聲明事件表 13 DECLARE_EVENT_TABLE() 14 15 }; 16 //定義應用程序類 17 class MyApp : public wxApp 18 { 19 public: 20 virtual bool OnInit(); 21 };
main.cppthis
1 #include "main.h" 2 #include "icon.xpm" 3 4 MyFrame::MyFrame(const wxString& title) 5 : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(250, 150)) 6 { 7 //定義菜單 8 wxMenu *menuFile = new wxMenu; 9 menuFile->Append(wxID_EXIT, wxT("Exit ... \tAlt+X"), wxT("Quit this program")); 10 11 wxMenu *menuHelp = new wxMenu; 12 menuHelp->Append(wxID_ABOUT, wxT("&About ... \tF1"), wxT("Show about frame")); 13 14 //定義菜單欄 15 wxMenuBar *menuBar = new wxMenuBar; 16 17 //向菜單欄添加菜單 18 menuBar->Append(menuFile, wxT("&File")); 19 menuBar->Append(menuHelp, wxT("&Help")); 20 21 //將菜單欄添加到wxFrame中 22 SetMenuBar(menuBar); 23 24 //添加狀態欄 25 CreateStatusBar(); 26 //將狀態欄分爲兩欄 27 //CreateStatusBar(2); 28 //添加狀態欄顯示內容 29 SetStatusText(wxT("Welcome to wxWidgets!")); 30 31 //設置應用顯示圖標 32 SetIcon(wxIcon(icon_xpm)); 33 34 //在wxFrame組件中定義了一個Panel容器,用於放置Button按鈕 35 wxPanel * panel = new wxPanel(this, wxID_ANY); 36 //添加一個按鈕 37 wxButton * button = new wxButton(panel, wxID_EXIT, wxT("Quit"), wxPoint(20, 20)); 38 button->SetFocus();//按鈕自動獲取焦點 39 40 41 Centre(); 42 } 43 //聲明應用程序 44 IMPLEMENT_APP(MyApp) 45 46 //初始化應用程序 47 bool MyApp::OnInit() 48 { 49 MyFrame *myframe = new MyFrame(wxT("MyFrame")); 50 myframe->Show(true); 51 52 return true; 53 } 54 55 //定義事件表,完成事件和處理函數的映射 56 BEGIN_EVENT_TABLE(MyFrame, wxFrame) 57 EVT_MENU(wxID_ABOUT, MyFrame::OnAbout) 58 EVT_MENU(wxID_EXIT, MyFrame::OnQuit) 59 EVT_BUTTON(wxID_EXIT, MyFrame::OnQuit) 60 END_EVENT_TABLE() 61 62 //事件處理函數的實現 63 void MyFrame::OnAbout(wxCommandEvent& event) 64 { 65 wxString msg; 66 //設置msg的內容 67 msg.Printf(wxT("About hello wxWidgets"), wxVERSION_STRING); 68 //定義彈出框的內容和標題 69 wxMessageBox(msg, wxT("About wxWidgets"), wxOK | wxICON_INFORMATION, this); 70 } 71 72 void MyFrame::OnQuit(wxCommandEvent& event) 73 { 74 Close(true); 75 }
窗口界面以下:spa
2、組件之間通訊指針
下面是一個組件之間相互通訊的例子,在這個實例中,因爲每一個按鈕須要屢次點擊,所以,爲按鈕綁定事件處理函數,使用的動態關聯時間處理函數的方法。code
靜態與動態事件處理函數關聯方法的區別,咱們會在後面講事件處理機制中具體講到。orm
main.hblog
1 #include <wx/wx.h> 2 #include <wx/panel.h> 3 #include <wx/wxprec.h> 4 #include <wx/stattext.h> 5 6 const int ID_PLUS = 101; 7 const int ID_MINUS = 102; 8 9 //定義左側面板 10 class LeftPanel : public wxPanel 11 { 12 public: 13 LeftPanel(wxPanel * parent); 14 15 void OnPlus(wxCommandEvent & event); 16 void OnMinus(wxCommandEvent & event); 17 18 wxButton * m_plus; 19 wxButton * m_minus; 20 wxPanel * m_parent; 21 int count; 22 }; 23 //定義右側面板 24 class RightPanel : public wxPanel 25 { 26 public: 27 RightPanel(wxPanel * parent); 28 29 void OnSetText(wxCommandEvent & event); 30 //靜態文本控件 31 wxStaticText * m_text; 32 }; 33 34 //定義主窗口類 35 class MyFrame : public wxFrame 36 { 37 public: 38 MyFrame(const wxString& title); 39 //定義成員變量 40 LeftPanel * m_lp; 41 RightPanel * m_rp; 42 43 wxPanel * m_parent; 44 45 //定義事件處理函數 46 void OnQuit(wxCommandEvent& event); 47 void OnAbout(wxCommandEvent& event); 48 private: 49 //聲明靜態事件表 50 DECLARE_EVENT_TABLE() 51 52 }; 53 //定義應用程序類 54 class MyApp : public wxApp 55 { 56 public: 57 virtual bool OnInit(); 58 };
main.cpp事件
1 #include "main.h" 2 #include "icon.xpm" 3 4 LeftPanel::LeftPanel(wxPanel * parent) 5 : wxPanel(parent, wxID_ANY, wxPoint(-1, -1), wxSize(-1, -1), wxBORDER_SUNKEN) 6 { 7 //定義一個計數變量 8 count = 0; 9 //定義父panel 10 m_parent = parent; 11 //定義兩個按鈕 12 m_plus = new wxButton(this, ID_PLUS, _T("+"), wxPoint(10, 10)); 13 m_minus = new wxButton(this, ID_MINUS, _T("-"), wxPoint(10, 60)); 14 15 //定義動態關聯事件處理函數 16 Connect(ID_PLUS, wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(LeftPanel::OnPlus)); 17 Connect(ID_MINUS, wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(LeftPanel::OnMinus)); 18 19 } 20 21 void LeftPanel::OnPlus(wxCommandEvent & WXUNUSED(event)) 22 { 23 //更新計數變量 24 count++; 25 //獲取父panel指針 26 MyFrame * comm = (MyFrame *)(m_parent->GetParent()); 27 //設置靜態文本框的值 28 comm->m_rp->m_text->SetLabel(wxString::Format(_T("%d"), count)); 29 } 30 31 void LeftPanel::OnMinus(wxCommandEvent & WXUNUSED(event)) 32 { 33 //更新計數變量 34 count--; 35 //獲取父panel指針 36 MyFrame * comm = (MyFrame *)(m_parent->GetParent()); 37 //設置靜態文本框的值 38 comm->m_rp->m_text->SetLabel(wxString::Format(_T("%d"), count)); 39 } 40 41 //定義右側panel 42 RightPanel::RightPanel(wxPanel * parent) 43 : wxPanel(parent, wxID_ANY, wxDefaultPosition, wxSize(270, 150), wxBORDER_SUNKEN) 44 { 45 //定義一個靜態文本框 46 m_text = new wxStaticText(this, -1, _T("0"), wxPoint(40, 60)); 47 } 48 49 //實現主窗口類 50 MyFrame::MyFrame(const wxString& title) 51 : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(250, 150)) 52 { 53 //定義菜單 54 wxMenu *menuFile = new wxMenu; 55 menuFile->Append(wxID_EXIT, wxT("Exit ... \tAlt+X"), wxT("Quit this program")); 56 57 wxMenu *menuHelp = new wxMenu; 58 menuHelp->Append(wxID_ABOUT, wxT("&About ... \tF1"), wxT("Show about frame")); 59 60 //定義菜單欄 61 wxMenuBar *menuBar = new wxMenuBar; 62 63 //向菜單欄添加菜單 64 menuBar->Append(menuFile, wxT("&File")); 65 menuBar->Append(menuHelp, wxT("&Help")); 66 67 //將菜單欄添加到wxFrame中 68 SetMenuBar(menuBar); 69 70 //添加狀態欄 71 CreateStatusBar(); 72 //將狀態欄分爲兩欄 73 //CreateStatusBar(2); 74 //添加狀態欄顯示內容 75 SetStatusText(wxT("Welcome to wxWidgets!")); 76 77 //設置應用顯示圖標 78 SetIcon(wxIcon(icon_xpm)); 79 80 //定義父panel 81 m_parent = new wxPanel(this, wxID_ANY); 82 //定義一個wxBoxSizer 83 wxBoxSizer * hbox = new wxBoxSizer(wxHORIZONTAL); 84 //定義左右panel 85 m_lp = new LeftPanel(m_parent); 86 m_rp = new RightPanel(m_parent); 87 //將左右panel加入到wxBoxSizer 88 hbox->Add(m_lp, 1, wxEXPAND | wxALL, 5); 89 hbox->Add(m_rp, 1, wxEXPAND | wxALL, 5); 90 //將wxBoxSizer加載到父panel 91 m_parent->SetSizer(hbox); 92 93 //使整個wxFrame框架位於屏幕中間 94 Centre(); 95 } 96 97 //聲明應用程序 98 IMPLEMENT_APP(MyApp) 99 100 //初始化應用程序 101 bool MyApp::OnInit() 102 { 103 MyFrame *myframe = new MyFrame(wxT("MyFrame")); 104 myframe->Show(true); 105 106 return true; 107 } 108 109 //定義靜態事件表,完成事件和處理函數的映射 110 BEGIN_EVENT_TABLE(MyFrame, wxFrame) 111 EVT_MENU(wxID_ABOUT, MyFrame::OnAbout) 112 EVT_MENU(wxID_EXIT, MyFrame::OnQuit) 113 END_EVENT_TABLE() 114 115 //事件處理函數的實現 116 void MyFrame::OnAbout(wxCommandEvent& event) 117 { 118 wxString msg; 119 //設置msg的內容 120 msg.Printf(wxT("About hello wxWidgets"), wxVERSION_STRING); 121 //定義彈出框的內容和標題 122 wxMessageBox(msg, wxT("About wxWidgets"), wxOK | wxICON_INFORMATION, this); 123 } 124 125 void MyFrame::OnQuit(wxCommandEvent& event) 126 { 127 Close(); 128 }
窗口界面以下:
點擊左側的加減按鈕,右側的數據會同步更新。