动态事件处理方法

This commit is contained in:
taynpg 2024-01-25 14:43:53 +08:00
parent fc53babbb8
commit d07cc4295e
2 changed files with 75 additions and 26 deletions

View File

@ -1,5 +1,7 @@
#include "event_demo.h" #include "event_demo.h"
#ifndef USE_DYNAMIC_CONNECT
// 静态链接
BEGIN_EVENT_TABLE(MainFrame, wxFrame) BEGIN_EVENT_TABLE(MainFrame, wxFrame)
EVT_MENU(wxID_ABOUT, MainFrame::OnAbout) EVT_MENU(wxID_ABOUT, MainFrame::OnAbout)
EVT_MENU(wxID_EXIT, MainFrame::OnQuit) EVT_MENU(wxID_EXIT, MainFrame::OnQuit)
@ -18,11 +20,28 @@ EVT_SIZE(MainFrame::OnSize)
*/ */
EVT_BUTTON(wxID_OK, MainFrame::OnButtonOK) EVT_BUTTON(wxID_OK, MainFrame::OnButtonOK)
END_EVENT_TABLE() END_EVENT_TABLE()
#endif
bool MyApp::OnInit() bool MyApp::OnInit()
{ {
MainFrame* frame = new MainFrame(wxT("Minimal wxWidgets App")); MainFrame* frame = new MainFrame(wxT("Minimal wxWidgets App"));
frame->Show(true); frame->Show(true);
#ifdef USE_DYNAMIC_CONNECT
/*
wxEVT_COMMAND_MENU_SELECTED不同于前面在静态事件表中用于表示事件映射的宏EVT_MENU,
EVT_MENU内部也使用了wxEVT_COMMAND_MENU_SELECTED.EVT_MENU其实也自动包含了用于对事
wxCommandEventHandler()
wxXYZEvent, wxXYZEventHandler宏进行强制转换.
frame->Disconnect
*/
frame->Connect(wxID_EXIT, wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(MainFrame::OnQuit));
frame->Connect(wxID_ABOUT, wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(MainFrame::OnAbout));
#endif
return true; return true;
} }
@ -33,10 +52,7 @@ void MainFrame::OnAbout(wxCommandEvent& event)
wxMessageBox(msg, wxT("About Minimal"), wxOK | wxICON_INFORMATION, this); wxMessageBox(msg, wxT("About Minimal"), wxOK | wxICON_INFORMATION, this);
} }
void MainFrame::OnQuit(wxCommandEvent& event) void MainFrame::OnQuit(wxCommandEvent& event) { Close(); }
{
Close();
}
MainFrame::MainFrame(const wxString& title) : wxFrame(NULL, wxID_ANY, title) MainFrame::MainFrame(const wxString& title) : wxFrame(NULL, wxID_ANY, title)
{ {
@ -56,37 +72,37 @@ MainFrame::MainFrame(const wxString& title) : wxFrame(NULL, wxID_ANY, title)
// ...然后将菜单条放置在主窗口上 // ...然后将菜单条放置在主窗口上
SetMenuBar(menuBar); SetMenuBar(menuBar);
wxButton* button = new wxButton(this, wxID_OK, wxT("OK"), wxButton* button =
wxPoint(200, 200)); 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::OnSize(wxSizeEvent& event) {}
{
}
void MainFrame::OnButtonOK(wxCommandEvent& event) void MainFrame::OnButtonOK(wxCommandEvent& event)
{ {
/* /*
wxCommandEvent事件被创建 wxCommandEvent事件被创建
wxID_OK和事件类型 wxEVT_COMMAND_BUTTON_CLICKED wxID_OK和事件类型
wxEvtHandler::ProcessEvent函数进行匹配wxControl wxEVT_COMMAND_BUTTON_CLICKED
wxWindow的 wxWidgets会搜索其父亲的类事件表 wxEvtHandler::ProcessEvent函数进行匹配wxControl
EVT_BUTTON(wxID_OK,MyFrame::OnButtonOK) wxWindow的
wxWidgets会搜索其父亲的类事件表
EVT_BUTTON(wxID_OK,MyFrame::OnButtonOK)
Command事件(wxCommandEvent) Command事件(wxCommandEvent)
wxWidgets的用户经常会感到困惑的地方 wxWidgets的用户经常会感到困惑的地方
wxActivate, wxCloseEvent, wxEraseEvent, wxFocusEvent, wxKeyEvent, wxIdleEvent, wxActivate, wxCloseEvent, wxEraseEvent,
wxInitDialogEvent, wxJoystickEvent, wxMenuEvent, wxMouseEvent, wxMoveEvent, wxPaintEvent, wxFocusEvent, wxKeyEvent, wxIdleEvent, wxInitDialogEvent,
wxQueryLayoutInfoEvent, wxSizeEvent, wxScrollWinEvent, wxSysColourChangedEvent wxJoystickEvent, wxMenuEvent, wxMouseEvent, wxMoveEvent, wxPaintEvent,
. wxQueryLayoutInfoEvent, wxSizeEvent, wxScrollWinEvent,
wxSysColourChangedEvent .
*/ */
} }

View File

@ -3,6 +3,8 @@
#include "wx/wx.h" #include "wx/wx.h"
#define USE_DYNAMIC_CONNECT
/* /*
@ -66,6 +68,37 @@
} }
*/ */
/*
wxWindow的类来说
wxEvtHandler的新类
使wxWindow::PushEventHandler函数将这个事件表压入到某个窗口类的事件表栈中
wxWindow::PopEventHandler函数来弹出最顶层的事件表
wxWindow:: PopEventHandler函数传递的是True的参数
使
使wxWindow::GetEventHandler方法
使wxWindow::GetEventHandler通常返回这个窗口类本身
使 PushEventHandler压入另外一个事件表使
wxWindow:: GetEventHandler函数才可以保证事件被正确的处理
PushEventHandler的方法通常用来临时的或者永久的改变图形界面的行为
使
wxEvent::Skip函数正常处理
*/
/*
使
使使(python)
使
PushEventHandler和PopEventHandler的方法只能针对
API有两个:wxEvtHandler::Connect和wxEvtHandler::Disconnect
wxEvtHandler::Disconnect函数
*/
class MyApp : public wxApp { class MyApp : public wxApp {
public: public:
virtual bool OnInit(); virtual bool OnInit();