wxWidgets事件处理
2007-12-24 11:22
274 查看
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的事件处理分静态与动态两种,静态处理方式,可以通过以下步骤实现:
定义一个直接或间接继承自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教程(2)-事件处理
- wxWidgets教程六:事件处理
- wxWidgets自定义事件处理
- wxWidgets-事件处理
- wxWidgets事件处理
- wxWidgets事件处理(手机播放器连载系列2)
- wxWidgets事件处理(手机播放器连载系列2)
- wxWidgets事件处理
- wxWidgets中事件处理过程
- wxWidgets中事件处理过程
- wxWidgets中的事件处理
- [ZETCODE]wxWidgets教程六:事件处理
- C# 自定义事件处理
- 添加控件并处理事件(纯手写)
- 使用IDA对MFC寻找按钮处理事件
- jQuery 的事件处理
- 基于回调的事件处理——回调机制与监听机制
- Android 的事件分发、传递、处理机制
- Android 广播事件处理Broadcast Receiver
- CListCtrl控件主要事件及LVN_ITEMCHANGED消息和鼠标双击列表项事件的处理