James C
James C

Reputation: 919

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. 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. 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. Please show me a method of closing the application from the menu item.

Upvotes: 0

Views: 3741

Answers (2)

Cody
Cody

Reputation: 639

// 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);

Upvotes: 1

Thomas Matthews
Thomas Matthews

Reputation: 57688

Try searching the web for "wxwidgets close window menu":
wxWidgets Hello World Example

In your OnExit function you need to call the Close method as in the example.

Upvotes: 2

Related Questions