Skip to content

WxWidgets:EventHandling

Dynamic Event Handling

void MyFrameHandler::OnFrameExit( wxCommandEvent & )
{
    // Do something useful.
}
MyFrameHandler myFrameHandler;
MyFrame::MyFrame()
{
      Bind( wxEVT_COMMAND_MENU_SELECTED, &MyFrameHandler::OnFrameExit,
              &myFrameHandler, wxID_EXIT );
}

또는 아래와 같은 방법은 전부 유효하다.

  • control_panel->play_button->Bind(wxEVT_BUTTON, &MainFrame::onPlayButton, this, ID_MEDIA_CONTROL_PANEL__PLAY_BUTTON);
  • control_panel->play_button->Bind(wxEVT_BUTTON, &MainFrame::onPlayButton, this);
  • control_panel->Bind(wxEVT_BUTTON, &MainFrame::onPlayButton, this, ID_MEDIA_CONTROL_PANEL__PLAY_BUTTON);

Using Existing Event Classes

If you just want to use a wxCommandEvent with a new event type, use one of the generic event table macros listed below, without having to define a new event class yourself.

Example:

// this is typically in a header: it just declares MY_EVENT event type
wxDECLARE_EVENT(MY_EVENT, wxCommandEvent);
// this is a definition so can't be in a header
wxDEFINE_EVENT(MY_EVENT, wxCommandEvent);
// example of code handling the event with event tables
wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
    EVT_MENU    (wxID_EXIT, MyFrame::OnExit)
    ...
    EVT_COMMAND (ID_MY_WINDOW, MY_EVENT, MyFrame::OnMyEvent)
wxEND_EVENT_TABLE()
void MyFrame::OnMyEvent(wxCommandEvent& event)
{
    // do something
    wxString text = event.GetString();
}
// example of code handling the event with Bind<>():
MyFrame::MyFrame()
{
    Bind(MY_EVENT, &MyFrame::OnMyEvent, this, ID_MY_WINDOW);
}
// example of code generating the event
void MyWindow::SendEvent()
{
    wxCommandEvent event(MY_EVENT, GetId());
    event.SetEventObject(this);
    // Give it some contents
    event.SetString("Hello");
    // Do send it
    ProcessWindowEvent(event);
}

Favorite site