事件表和事件处理

This commit is contained in:
taynpg 2024-01-25 14:30:29 +08:00
parent 9afb2bfe4d
commit 8a1306e49e
3 changed files with 87 additions and 9 deletions

View File

@ -169,6 +169,7 @@ MyFrame::MyFrame(const wxString& title) : wxFrame(NULL, wxID_ANY, title)
menuBar->Append(helpMenu, wxT("&Help")); menuBar->Append(helpMenu, wxT("&Help"));
// ...然后将菜单条放置在主窗口上 // ...然后将菜单条放置在主窗口上
SetMenuBar(menuBar); SetMenuBar(menuBar);
// 创建一个状态条来让一切更有趣些。 // 创建一个状态条来让一切更有趣些。
CreateStatusBar(2); CreateStatusBar(2);
SetStatusText(wxT("Welcome to wxWidgets!")); SetStatusText(wxT("Welcome to wxWidgets!"));

View File

@ -1,30 +1,44 @@
#include "event_01.h" #include "event_01.h"
BEGIN_EVENT_TABLE(MyFrame, wxFrame) BEGIN_EVENT_TABLE(MainFrame, wxFrame)
EVT_MENU(wxID_ABOUT, MyFrame::OnAbout) EVT_MENU(wxID_ABOUT, MainFrame::OnAbout)
EVT_MENU(wxID_EXIT, MyFrame::OnQuit) EVT_MENU(wxID_EXIT, MainFrame::OnQuit)
// EVT_SIZE事件映射宏不需要标识符参数因为这个事件只会被产生这个事件的控件所处理。
EVT_SIZE(MainFrame::OnSize)
/*
MyFrame的子窗
wxWidgets会首先检查wxButton类是否指定了相应的处理函数
MyFrame类型的一个实例
MyFrame::OnButtonOK函数就被调用了
wxID_OK的command事件
使使
*/
EVT_BUTTON(wxID_OK, MainFrame::OnButtonOK)
END_EVENT_TABLE() END_EVENT_TABLE()
bool MyApp::OnInit() bool MyApp::OnInit()
{ {
MyFrame* frame = new MyFrame(wxT("Minimal wxWidgets App")); MainFrame* frame = new MainFrame(wxT("Minimal wxWidgets App"));
frame->Show(true); frame->Show(true);
return true; return true;
} }
void MyFrame::OnAbout(wxCommandEvent& event) void MainFrame::OnAbout(wxCommandEvent& event)
{ {
wxString msg; wxString msg;
msg.Printf(wxT("Hello and welcome to %s"), wxVERSION_STRING); msg.Printf(wxT("Hello and welcome to %s"), wxVERSION_STRING);
wxMessageBox(msg, wxT("About Minimal"), wxOK | wxICON_INFORMATION, this); wxMessageBox(msg, wxT("About Minimal"), wxOK | wxICON_INFORMATION, this);
} }
void MyFrame::OnQuit(wxCommandEvent& event) void MainFrame::OnQuit(wxCommandEvent& event)
{ {
Close(); Close();
} }
MyFrame::MyFrame(const wxString& title) : wxFrame(NULL, wxID_ANY, title) MainFrame::MainFrame(const wxString& title) : wxFrame(NULL, wxID_ANY, title)
{ {
// 设置窗口图标 // 设置窗口图标
// SetIcon(wxIcon(mondrian_xpm)); // SetIcon(wxIcon(mondrian_xpm));
@ -41,6 +55,38 @@ MyFrame::MyFrame(const wxString& title) : wxFrame(NULL, wxID_ANY, title)
menuBar->Append(helpMenu, wxT("&Help")); menuBar->Append(helpMenu, wxT("&Help"));
// ...然后将菜单条放置在主窗口上 // ...然后将菜单条放置在主窗口上
SetMenuBar(menuBar); SetMenuBar(menuBar);
wxButton* button = new wxButton(this, wxID_OK, wxT("OK"),
wxPoint(200, 200));
CreateStatusBar(2); CreateStatusBar(2);
SetStatusText(wxT("Welcome to wxWidgets!")); SetStatusText(wxT("Welcome to wxWidgets!"));
}
void MainFrame::OnSize(wxSizeEvent& event)
{
}
void MainFrame::OnButtonOK(wxCommandEvent& event)
{
/*
wxCommandEvent事件被创建
wxID_OK和事件类型 wxEVT_COMMAND_BUTTON_CLICKED
wxEvtHandler::ProcessEvent函数进行匹配wxControl
wxWindow的 wxWidgets会搜索其父亲的类事件表
EVT_BUTTON(wxID_OK,MyFrame::OnButtonOK)
Command事件(wxCommandEvent)
wxWidgets的用户经常会感到困惑的地方
wxActivate, wxCloseEvent, wxEraseEvent, wxFocusEvent, wxKeyEvent, wxIdleEvent,
wxInitDialogEvent, wxJoystickEvent, wxMenuEvent, wxMouseEvent, wxMoveEvent, wxPaintEvent,
wxQueryLayoutInfoEvent, wxSizeEvent, wxScrollWinEvent, wxSysColourChangedEvent
.
*/
} }

View File

@ -12,17 +12,48 @@
*/ */
/*
wxWidgets事件处理系统比起通常的虚方法机制来说要稍微复杂一点
wxEvtHandler的派生类frame
wxWidgets事件和事件处理过程的对应关系wxWindow的窗口类
wxEvtHandler的派生类.
()
1. wxEvtHandler的类.
2.
3. 使DECLARE_EVENT_TABLE声明事件表
4. .cpp文件中使用使用BEGIN_EVENT_TABLE和END_EVENT_TABLE实现一个事件表
5.
void
MFCMFC中消息处理函数并没有一个统一的形式
wxCommandEvent类型size事件
wxSizeEvent的类型
访
*/
class MyApp : public wxApp { class MyApp : public wxApp {
public: public:
virtual bool OnInit(); virtual bool OnInit();
}; };
class MyFrame : public wxFrame { class MainFrame : public wxFrame {
public: public:
MyFrame(const wxString& title); MainFrame(const wxString& title);
void OnQuit(wxCommandEvent& event); void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event); void OnAbout(wxCommandEvent& event);
/*
*/
void OnSize(wxSizeEvent& event);
void OnButtonOK(wxCommandEvent& event);
private: private:
DECLARE_EVENT_TABLE() DECLARE_EVENT_TABLE()
}; };