#includeclass MyFrame : public wxFrame { public: MyFrame(const wxString& title) : wxFrame(nullptr, wxID_ANY, title) { // Create the menu and its items wxMenu* fileMenu = new wxMenu; fileMenu->Append(wxID_OPEN, wxT("&Open")); fileMenu->Append(wxID_SAVE, wxT("&Save")); fileMenu->Append(wxID_EXIT, wxT("&Quit")); // Attach the update UI event handler for the "Save" item Connect(wxID_SAVE, wxEVT_UPDATE_UI, wxUpdateUIEventHandler(MyFrame::OnUpdateSave)); // Create the menu bar and add the file menu to it wxMenuBar* menuBar = new wxMenuBar; menuBar->Append(fileMenu, wxT("&File")); SetMenuBar(menuBar); } private: void OnUpdateSave(wxUpdateUIEvent& event) { // Check if the file is currently open and enable/disable the "Save" menu item bool isOpen = false; // TODO: Implement check for open file event.Enable(isOpen); } }; class MyApp : public wxApp { public: virtual bool OnInit() { MyFrame* frame = new MyFrame(wxT("wxUpdateUIEvent Example")); frame->Show(); return true; } }; wxIMPLEMENT_APP(MyApp);
#includeIn both examples, the package library used is wxWidgets, specifically the wxcore and wxbase libraries.class MyFrame : public wxFrame { public: MyFrame(const wxString& title) : wxFrame(nullptr, wxID_ANY, title) { // Create the toolbar and its buttons wxToolBar* toolbar = CreateToolBar(); toolbar->AddTool(wxID_NEW, wxT("New"), wxNullBitmap); toolbar->AddTool(wxID_OPEN, wxT("Open"), wxNullBitmap); toolbar->AddSeparator(); m_toggleButton = toolbar->AddCheckTool(wxID_ANY, wxT("Toggle"), wxNullBitmap); // Attach the update UI event handler for the "New" item Connect(wxID_NEW, wxEVT_UPDATE_UI, wxUpdateUIEventHandler(MyFrame::OnUpdateNew)); // Bind the toggle button state change event to the event handler Bind(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, &MyFrame::OnToggleButton, this); // Finish initializing the frame and show it SetInitialSize(wxSize(400, 300)); SetSize(wxSize(400, 300)); Centre(); } private: void OnUpdateNew(wxUpdateUIEvent& event) { // Always enable the "New" button event.Enable(true); } void OnToggleButton(wxCommandEvent& event) { // Update the toolbar button state based on the checkbox state bool isChecked = m_toggleButton->GetToolState(m_toggleButton->GetId()); m_toggleButton->Enable(isChecked); } wxToolBarToolBase* m_toggleButton; }; class MyApp : public wxApp { public: virtual bool OnInit() { MyFrame* frame = new MyFrame(wxT("wxUpdateUIEvent Example")); frame->Show(); return true; } }; wxIMPLEMENT_APP(MyApp);