l xmlns="http://www.w3.org/1999/l">
The Event HandlersAs you may have noticed, the event handler functions in MyFrame are not virtual and should not be virtual. How, then, are they called? The answer lies in the event table, as follows. // Event table for MyFrame BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_MENU(wxID_ABOUT, MyFrame::OnAbout) EVT_MENU(wxID_EXIT, MyFrame::OnQuit) END_EVENT_TABLE() An event table, placed in a class's implementation file, tells wxWidgets how events coming from the user or from other sources are routed to member functions. With the event table shown previously, mouse clicks on menu items with the identifiers wxID_EXIT and wxID_ABOUT are routed to the functions MyFrame::OnQuit and MyFrame::OnAbout, respectively. EVT_MENU is just one of many event table macros you can use to tell wxWidgets what kind of event should be routed to what function. The identifiers used here are predefined by wxWidgets, but you will often define your own identifiers, using enums, consts, or preprocessor defines. This kind of event table is a static way of routing events, and cannot be changed at runtime. In the next chapter, we'll describe how to set up dynamic event handlers. While we're dealing with event tables, let's see the two functions we're using as event handlers. void MyFrame::OnAbout(wxCommandEvent& event) { wxString msg; msg.Printf(wxT("Hello and welcome to %s"), wxVERSION_STRING); wxMessageBox(msg, wxT("About Minimal"), wxOK | wxICON_INFORMATION, this); } void MyFrame::OnQuit(wxCommandEvent& event) { // Destroy the frame Close(); } MyFrame::OnAbout shows a message box when the user clicks on the About menu item. wxMessageBox takes a message, a caption, a combination of styles, and a parent window.Chapter 4, "Window Basics." This sample doesn't need it, but most applications should provide an OnExit function in its application class to clean up data structures before quitting. Note that this function is only called if OnInit returns true. |