簡體   English   中英

通過選擇“退出”菜單項關閉應用程序-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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM