2009年4月20日星期一

wxwidgets的消息处理机制

wxwidgets的消息处理机制比较灵活,效率也比较高,这里只研究一下最基本的东西。

自定义一个消息以前的用法是
BEGIN_DECLARE_EVENT_TYPES()
DECLARE_EVENT_TYPE(, )
END_DECLARE_EVENT_TYPES()
现在的用法是
DEFINE_EVENT_TYPE()

消息处理也很容易理解
BEGIN_EVENT_TABLE(窗口类,基类)
EVT_BUTTON(窗口标识,处理函数)
........
END_EVENT_TABLE()
如果是自定义消息就要看处理函数的类型是怎样的。
如果处理函数是wxEventHandler类型直接使用EVT_CUSTOM来处理,如果处理函数类型是wxCommandEventHandler,就要使用EVT_CUSTOM来处理。

下面的例子在http://blog.csdn.net/kese/archive/2007/09/08/1776985.aspx例子 的基础上改造出来的

wxButtonStudio.cpp



#include <wx/wx_gch.h>

class wxButtonStudio : public wxApp
{
public:
bool OnInit();
};

class wxButtonFrame : public wxFrame
{
public:
wxButtonFrame(wxWindow* parent,const wxWindowID id,const wxString& title);
~wxButtonFrame();
wxChoice* choice;
wxButton* button;
wxButton* button2;


void OnButtonClick(wxCommandEvent& event);
void OnButton2Click(wxCommandEvent& event);
private:
DECLARE_EVENT_TABLE()
};

DEFINE_EVENT_TYPE(wxEVT_CUSTOM1)

IMPLEMENT_APP(wxButtonStudio)

enum
{
ID_BUTTON1,
ID_BUTTON2,
ID_CHOICE1
};

BEGIN_EVENT_TABLE(wxButtonFrame,wxFrame)
EVT_BUTTON(ID_BUTTON1,wxButtonFrame::OnButtonClick)
EVT_BUTTON(ID_BUTTON2,wxButtonFrame::OnButton2Click)
EVT_COMMAND(ID_BUTTON1,wxEVT_CUSTOM1,wxButtonFrame::OnButtonClick)
END_EVENT_TABLE()

bool wxButtonStudio::OnInit()
{
wxButtonFrame* frame = new wxButtonFrame((wxWindow*)NULL,wxID_ANY,_T("ButtonFrame"));
frame->Show(true);
return true;
}

wxButtonFrame::wxButtonFrame(wxWindow* parent,const wxWindowID id,const wxString& title)
: wxFrame(parent,id,title,wxDefaultPosition,wxDefaultSize,wxDEFAULT_FRAME_STYLE)
{
choice = new wxChoice(this,ID_CHOICE1);
choice->Append(_T("A"));
choice->Append(_T("B"));
choice->Append(_T("C"));
choice->Append(_T("D"));
choice->Append(_T("E"));
choice->Append(_T("F"));
choice->Append(_T("G"));
button = new wxButton(this,ID_BUTTON1,_T("A Button"),wxPoint(200,0),wxSize(100,30));
button2 = new wxButton(this,ID_BUTTON1,_T("B Button"),wxPoint(200,60),wxSize(100,30));
}

wxButtonFrame::~wxButtonFrame()
{
}

void wxButtonFrame::OnButtonClick(wxCommandEvent& event)
{
if (choice->GetCurrentSelection() < (int)choice->GetCount() - 1)
choice->Select(choice->GetCurrentSelection() + 1);
else
choice->Select(-1);
}

void wxButtonFrame::OnButton2Click(wxCommandEvent& event)
{
::SendMessageA((HWND)button->GetHWND(),wxEVT_CUSTOM1,0,0);
}

没有评论: