如何单击 wxWidgets 处理程序 OnClose 中的按钮?
How to click a Button in wxWidgets handler OnClose?
我有一个用于对话框中关闭按钮(右上角)的处理程序和一个用于对话框内添加按钮的处理程序。我的自定义按钮有 wxID_CANCEL 作为 ID。处理程序 OnClose 应该执行处理程序 OnButtonCancel。发生了什么:它关闭了对话框和应用程序,因为对话框被覆盖了 wxApp:OnInit。但是没有执行OnButtonCancel。
void Dialog_Test::OnClose(wxCloseEvent& event) {
wxCommandEvent event_button_clicked(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
event_button_clicked.SetEventObject(this);
this->ProcessEvent(event_button_clicked);
}
void Dialog_Test::OnButtonCancel(wxCommandEvent& WXUNUSED(event)) {
wxMessageBox(_("TODO: Dialog_Test::OnButtonCancel")); // <---- not executed
EndModal(wxID_CANCEL);
}
这里发生了什么?
编辑 #1: 在 wxFrame 中,我在 OnClose 中使用 ProcessCommand(wxID_CLOSE_FRAME),但在 wxDialog 中没有 ProcessCommand。
我想我找到了错误:
// wrong: don't use ProcessEvent of wxDialog
// this->ProcessEvent(event_button_clicked);
// right: use ProcessEvent of the custom wxButton
GetButtonCancel()->GetEventHandler()->ProcessEvent(event_button_clicked);
您应该添加一个单独的函数(即 Dialog_Test::DoCancel()
)并从 Dialog_Test::OnClose()
和 Dialog_Test::OnButtonCancel()
调用它。
如果您按以下方式处理它,它可能是更简洁、更灵活的代码:
void MyFrame::btnCancelOnClick(wxCommandEvent & event)
{
wxCommandEvent CloseEvent;
CloseEvent.SetEventType(wxEVT_CLOSE_WINDOW);
CloseEvent.SetEventObject(m_btnCancel);
wxPostEvent(this, CloseEvent);
}
然后在 OnClose 事件中
void MyFrame::OnClose(wxCloseEvent & event)
{
if (event.GetEventObject() == m_btnOK) {}
else{
//Do if cancel button is clicked
}
event.Skip();
}
我有一个用于对话框中关闭按钮(右上角)的处理程序和一个用于对话框内添加按钮的处理程序。我的自定义按钮有 wxID_CANCEL 作为 ID。处理程序 OnClose 应该执行处理程序 OnButtonCancel。发生了什么:它关闭了对话框和应用程序,因为对话框被覆盖了 wxApp:OnInit。但是没有执行OnButtonCancel。
void Dialog_Test::OnClose(wxCloseEvent& event) {
wxCommandEvent event_button_clicked(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
event_button_clicked.SetEventObject(this);
this->ProcessEvent(event_button_clicked);
}
void Dialog_Test::OnButtonCancel(wxCommandEvent& WXUNUSED(event)) {
wxMessageBox(_("TODO: Dialog_Test::OnButtonCancel")); // <---- not executed
EndModal(wxID_CANCEL);
}
这里发生了什么?
编辑 #1: 在 wxFrame 中,我在 OnClose 中使用 ProcessCommand(wxID_CLOSE_FRAME),但在 wxDialog 中没有 ProcessCommand。
我想我找到了错误:
// wrong: don't use ProcessEvent of wxDialog
// this->ProcessEvent(event_button_clicked);
// right: use ProcessEvent of the custom wxButton
GetButtonCancel()->GetEventHandler()->ProcessEvent(event_button_clicked);
您应该添加一个单独的函数(即 Dialog_Test::DoCancel()
)并从 Dialog_Test::OnClose()
和 Dialog_Test::OnButtonCancel()
调用它。
如果您按以下方式处理它,它可能是更简洁、更灵活的代码:
void MyFrame::btnCancelOnClick(wxCommandEvent & event)
{
wxCommandEvent CloseEvent;
CloseEvent.SetEventType(wxEVT_CLOSE_WINDOW);
CloseEvent.SetEventObject(m_btnCancel);
wxPostEvent(this, CloseEvent);
}
然后在 OnClose 事件中
void MyFrame::OnClose(wxCloseEvent & event)
{
if (event.GetEventObject() == m_btnOK) {}
else{
//Do if cancel button is clicked
}
event.Skip();
}