wxWidgets是如何处理事件呢? 如果对MFC熟悉的话,相信一定会很轻松的,因为看起来,wxWidgets的事件处理与MFC很相似。
wxWidgets的事件处理分静态与动态两种,静态处理方式,可以通过以下步骤实现:
- 定义一个直接或间接继承自wxEvtHandler的类
- 定义事件处理函数
- 在类中使用DECLARE_EVENT_TABLE()宏,定义事件表
- 在实现文件中,使用类似如下的定义:
BEGIN_EVENT_TABLE(当前类,父类)
//事件处理函数定义
END_EVENT_TABLE()
这样,就实现了表态的事件处理了,如下代码示例了,如何使用:
//MainFrame.h

#ifndef MAIN_FRAME_H
#define MAIN_FRAME_H
#include <wx/frame.h>
//#include <wx/wx.h>
class wxStudyEvent;
class MainFrame : public wxFrame
...{
public:
MainFrame(const wxString& strTitle);
private:
void InitCtrl();
private://event
void OnAbout(wxCommandEvent& event);
void OnQuit(wxCommandEvent& event);
//测试动态库
void OnShowMessage(wxCommandEvent& event);
void OnAppInitOK(wxStudyEvent& event);
private:
DECLARE_EVENT_TABLE()
};
#endif
//MainFrame.cpp

#include "MainFrame.h"
#include <wx/menu.h>
//#include <wx/string.h>
#include <wx/msgdlg.h>
#include <wx/dynlib.h>
#include <wx/filefn.h>
#include <wx/filename.h>
#include <wx/toolbar.h>
#include "maindef.h"
#include "sdk/sdk_events.h"
//EVENT DECLARE
BEGIN_EVENT_TABLE(MainFrame,wxFrame)
EVT_MENU(wxID_EXIT,MainFrame::OnQuit)
EVT_MENU(wxID_ABOUT,MainFrame::OnAbout)
EVT_MENU(wxID_TEST_DLL,MainFrame::OnShowMessage)
EVT_APP_ONINIT_OK(MainFrame::OnAppInitOK)
END_EVENT_TABLE()
//END EVENT DECLARE
MainFrame::MainFrame(const wxString& strTitle)
:wxFrame(NULL,wxID_ANY,strTitle)
...{
InitCtrl();
}
void MainFrame::InitCtrl()
...{
wxMenu* pMenu = new wxMenu();
pMenu->Append(wxID_EXIT,_T("Exit"));
pMenu->Append(wxID_ABOUT,_T("About"));
pMenu->Append(wxID_TEST_DLL,_T("测试动态库"));
wxMenuBar* pMenuBar = new wxMenuBar();
pMenuBar->Append(pMenu,_T("File"));

SetMenuBar(pMenuBar);

/**//*wxToolBar(wxWindow* parent, wxWindowID id,
const wxPoint& pos = wxDefaultPosition,
const wxSize& size = wxDefaultSize,
long style = wxTB_HORIZONTAL | wxNO_BORDER,
const wxString& name = wxPanelNameStr)
*/
wxToolBar* pMainToolBar = CreateToolBar( wxTB_DOCKABLE|wxTB_HORIZONTAL, 6000 );
pMainToolBar->AddTool( 6001, wxT("tool"), wxNullBitmap, wxNullBitmap, wxITEM_NORMAL, wxT(""), wxT("") );
//pMainToolBar->Realize();
//wxToolBar* pPluginBar = new wxToolBar(this,-1);
}

/**//*
* 退出程序
* @param wxCommandEvent& event
*/
void MainFrame::OnQuit(wxCommandEvent& event)
...{
Close();
}
void MainFrame::OnAbout(wxCommandEvent& event)
...{
wxString strMsg(_T("wxWidgets study."));
wxString strCaption(_T("关于"));
wxMessageBox(strMsg, strCaption, wxOK | wxICON_INFORMATION,this);

//wxMessageBox("Quit program?", "Confirm",wxYES_NO | wxCANCEL, this);
}
void MainFrame::OnShowMessage(wxCommandEvent& event)
...{
//wxString dir(wxGetWorkingDirectory());
//wxMessageBox(dir,_T("dir"),wxOK);

wxChar name[MAX_PATH] = ...{0};
GetModuleFileName(0L, name, MAX_PATH);
//wxMessageBox(name,_T("dir"),wxOK);
wxFileName fname(name);
wxString strPath(fname.GetPath(wxPATH_GET_VOLUME));
wxDynamicLibrary lib;
wxString libfile(strPath << _T("/plugins/msgplugin/msgplugin.dll"));
lib.Load(libfile);
if(!lib.IsLoaded())
...{
wxMessageBox(libfile << _T(" load error."),_T("Error"),wxOK | wxICON_ERROR);
return;
}
typedef int (*ShowMessageProc)(const wxString&);
ShowMessageProc ShowMessage = (ShowMessageProc)lib.GetSymbol(_T("ShowMessage"));
if(NULL == ShowMessage)
...{
wxMessageBox(_T("don't call the method: ShowMessag"), _T("Error"),
wxOK | wxICON_ERROR);
return;
}
ShowMessage(_T("call from ") + libfile);
}
void MainFrame::OnAppInitOK(wxStudyEvent& event)
...{
wxMessageBox(_T("在MainFrame中处理事件->") + event.GetEventMsg());
event.Skip();
}
本文介绍wxWidgets中静态事件处理的方法,通过定义事件处理类、事件处理函数及事件表实现事件响应。展示了具体的代码示例,包括菜单项点击事件及自定义事件的处理。
644

被折叠的 条评论
为什么被折叠?



