星期三, 十二月 13, 2006

wxWidget Event Handler

在有繼承到 wxEvtHandler 這個類別的子類別,都可以使用 DECLARE_EVENT_TABLE() 來讓這個類別有處理訊息的能力,像是按鍵處理、視窗移動、滑鼠按鍵 … 等,都是需要我們處理的訊息,以下是讓一個 MyFrame 有處理訊息的能力:


// Declare our main frame class
class MyFrame : public wxFrame
{
public:
// Constructor
MyFrame(const wxString& title);
// Event handlers
void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
private:
// This class handles events
<span style="font-weight: bold;">DECLARE_EVENT_TABLE()</span>
};

當我們宣告 DECLARE_EVENT_TABLE() 讓 MyFrame 有辦法處理訊息之後,接下來要利用 BEGIN_EVENT_TABLE() 將訊息和 Function 作個對應表,方法如下:

// Event table for MyFrame
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(<span style="font-weight: bold;">wxID_ABOUT</span>, MyFrame::OnAbout)
EVT_MENU(<span style="font-weight: bold;">wxID_EXIT</span>, MyFrame::OnQuit)
END_EVENT_TABLE()

BEGIN_EVENT_TABLE 的第一個參數是子類別,第二個參數是父類別,中間的二個 Event 是對應功能表的關於和離開,只要有功能表發出 wxID_ABOUT 或是 wxID_EXIT 訊息,會在這個地方轉成相對應的 Function,以下是 MyFrame 的建構子,也是訊息和功能表連結的地方:

MyFrame::MyFrame(const wxString& title)
: wxFrame(NULL, wxID_ANY, title)
{
// Create a menu bar
wxMenu *fileMenu = new wxMenu;

// The “About” item should be in the help menu
wxMenu *helpMenu = new wxMenu;

helpMenu->Append(<span style="font-weight: bold;">wxID_ABOUT</span>, wxT(“&About...\tF1”),wxT(“Show about dialog”));
fileMenu->Append(<span style="font-weight: bold;">wxID_EXIT</span>, wxT(“E&xit\tAlt-X”),wxT(“Quit this program”));

// Now append the freshly created menu to the menu bar...
wxMenuBar *menuBar = new wxMenuBar();
menuBar->Append(fileMenu, wxT(“&File”));
menuBar->Append(helpMenu, wxT(“&Help”));

// ... and attach this menu bar to the frame
SetMenuBar(menuBar);
// Create a status bar just for fun
}

在建立 helpMenu 和 fileMenu 之後,可以利用 Append 的功能,來增加功能表的選項,並和訊息作連結,新增的 About 和 Exit 各自連結 wxID_ABOUT 和 wxID_EXIT ,這樣就是個簡單的訊息對應表。

以上是靜態的訊息對應表宣告,如果在程式中有需要將訊息表重新對應的時候,可以利用 wxEvtHandler 中的 Connect() 和 Disconnect() 來做連結和斷開的動作,Function 的原型如下:

wxEvtHandler::Connect

void Connect(int id, int lastId, wxEventType eventType, wxObjectEventFunction function, wxObject* userData = NULL, wxEvtHandler* eventSink = NULL)
void Connect(int id, wxEventType eventType, wxObjectEventFunction function, wxObject* userData = NULL, wxEvtHandler* eventSink = NULL)
void Connect(wxEventType eventType, wxObjectEventFunction function, wxObject* userData = NULL, wxEvtHandler* eventSink = NULL)

Example:

frame->Connect( wxID_EXIT,
wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(MyFrame::OnQuit) );


wxEvtHandler::Disconnect

bool Disconnect(wxEventType eventType = wxEVT_NULL, wxObjectEventFunction function = NULL, wxObject* userData = NULL, wxEvtHandler* eventSink = NULL)
bool Disconnect(int id = wxID_ANY, wxEventType eventType = wxEVT_NULL, wxObjectEventFunction function = NULL, wxObject* userData = NULL, wxEvtHandler* eventSink = NULL)
bool Disconnect(int id, int lastId = wxID_ANY, wxEventType eventType = wxEVT_NULL, wxObjectEventFunction function = NULL, wxObject* userData = NULL, wxEvtHandler* eventSink = NULL)



0 意見:

 
template hacks by: [ METAMUSE ]