wxWidgets事件处理

 

wxWidgets是如何处理事件呢? 如果对MFC熟悉的话,相信一定会很轻松的,因为看起来,wxWidgets的事件处理与MFC很相似。

wxWidgets的事件处理分静态与动态两种,静态处理方式,可以通过以下步骤实现:

  1. 定义一个直接或间接继承自wxEvtHandler的类
  2. 定义事件处理函数
  3. 在类中使用DECLARE_EVENT_TABLE()宏,定义事件表
  4. 在实现文件中,使用类似如下的定义:

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();
}

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值