void MyFrame::OnButtonFocus(wxFocusEvent& event) { if (event.GetEventType() == wxEVT_SET_FOCUS) { // Button has gained focus // Perform some action } else if (event.GetEventType() == wxEVT_KILL_FOCUS) { // Button has lost focus // Perform some action } } wxButton* button = new wxButton(this, wxID_ANY, "My Button"); button->Bind(wxEVT_SET_FOCUS, &MyFrame::OnButtonFocus, this); button->Bind(wxEVT_KILL_FOCUS, &MyFrame::OnButtonFocus, this);
void MyFrame::OnFocusEvent(wxFocusEvent& event) { wxWindow* focusedWindow = event.GetWindow(); if (focusedWindow->IsKindOf(CLASSINFO(wxTextCtrl))) { // A text control has gained or lost focus // Perform some action } } wxWindow* panel = new wxPanel(this, wxID_ANY); panel->Bind(wxEVT_SET_FOCUS, &MyFrame::OnFocusEvent, this); panel->Bind(wxEVT_KILL_FOCUS, &MyFrame::OnFocusEvent, this);In both the above examples, the wxFocusEvent is used to perform a certain action when a certain widget has gained or lost focus. The library package used in these examples is wxWidgets.