![](/img/trans.png)
[英]Creating a handler function with Bind to handle clicked menu item - wxWidgets 3.0
[英]Closing the application by selecting 'Quit' menu item - wxWidgets 3.0
到目前为止,我已经为wxWidgets应用程序编写了一些简单的代码,例如创建菜单,框架和一些按钮。 为了遵循退出的过程,我有一个显示消息框的函数:
int OnExit( )
{
wxMessageBox( "Closing the application", wxOK | wxICON_INFORMATION )
return 0;
}
单击关闭(X)按钮关闭应用程序,显示消息框,然后退出。 但是,通过单击“退出”菜单项关闭它对我不起作用。 我尝试从一个旧示例以及wxWidgets项目随附的CodeBlocks基本示例代码中复制一些代码,但是没有运气。 请告诉我一种从菜单项关闭应用程序的方法。
尝试在网上搜索“ wxwidgets关闭窗口菜单”:
wxWidgets Hello World示例
在OnExit
函数中,您需要像示例中一样调用Close
方法。
// Build: g++ this.cpp -std=gnu++11 $(wx-config --cxxflags --libs core,base)
#include <wx/wx.h>
class CApp : public wxApp
{
public:
bool OnInit() {
// Create the main frame.
wxFrame * frame = new wxFrame(NULL, wxID_ANY, wxT("demo"));
// Add the menubar
wxMenu * menus[] = {new wxMenu, new wxMenu};
wxString labels[] = {wxT("&File"), wxT("&Help")};
frame->wxFrame::SetMenuBar(new wxMenuBar(2, menus, labels));
menus[0]->Append(wxID_EXIT);
// Bind an event handling method for menu item wxID_EXIT.
this->Bind(wxEVT_MENU, [frame](wxCommandEvent &)->void{
frame->Close();
/* 1. method wxWindow::Close
* 2. event type wxEVT_CLOSE_WINDOW
* 3. method wxTopLevelWindow::OnCloseWindow
* 4. method wxTopLevelWindow::Destroy (overriding wxWindow::Destroy)
* 5. op delete
*/
}, wxID_EXIT);
// Enter the message loop.
frame->Centre(wxBOTH);
frame->Show(true);
return true;
}
int OnExit() {
wxMessageBox("Closing the application", wxEmptyString, wxOK | wxICON_INFORMATION);
return this->wxApp::OnExit();
}
};
wxDECLARE_APP(CApp);
wxIMPLEMENT_APP(CApp);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.