简体   繁体   English

通过选择“退出”菜单项关闭应用程序-wxWidgets 3.0

[英]Closing the application by selecting 'Quit' menu item - wxWidgets 3.0

So far I have written some simple code for a wxWidgets application, like creating a menu, frame and a few buttons. 到目前为止,我已经为wxWidgets应用程序编写了一些简单的代码,例如创建菜单,框架和一些按钮。 To follow the process of exiting, I have this function that shows a message box : 为了遵循退出的过程,我有一个显示消息框的函数:

int OnExit( )
  {
  wxMessageBox( "Closing the application", wxOK | wxICON_INFORMATION )
  return 0;
  }

Closing the application by clicking close ( X ) button shows the message box and then exits. 单击关闭(X)按钮关闭应用程序,显示消息框,然后退出。 But closing it by clicking the "Quit" menu item doesn't work for me. 但是,通过单击“退出”菜单项关闭它对我不起作用。 I have tried copying some code from an old example and from CodeBlocks basic sample code that comes with wxWidgets project, with no luck. 我尝试从一个旧示例以及wxWidgets项目随附的CodeBlocks基本示例代码中复制一些代码,但是没有运气。 Please show me a method of closing the application from the menu item. 请告诉我一种从菜单项关闭应用程序的方法。

Try searching the web for "wxwidgets close window menu": 尝试在网上搜索“ wxwidgets关闭窗口菜单”:
wxWidgets Hello World Example wxWidgets Hello World示例

In your OnExit function you need to call the Close method as in the example. 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM