I've been learning wxWidgets and C++ together, and it's been super exciting. I've been consuming all the online tutorials I can find, and have purchased and am reading the book as well. I'm well aware that many of the tutorials (and the book) are outdated, so part of my learning is to bring the examples up to current practice.
I've converted the "Your First App" tutorial on the wxwidgets wiki to use dynamic Bind() instead of the event table, for instance, and have updated references to wxEVT_COMMAND_MENU_SELECTED to the newer and preferred wxEVT_MENU:
MyFrame::MyFrame(const wxString &title) : wxFrame(nullptr, wxID_ANY, title) {
MainMenu = new wxMenuBar();
wxMenu *FileMenu = new wxMenu;
MainMenu->Append(FileMenu, _T("File"));
SetMenuBar(MainMenu);
CreateStatusBar(1);
FileMenu->Append(MENU_New, _T("&New"), _T("Create a new file"));
FileMenu->Append(MENU_Open, _T("&Open"), _T("Open an existing file"));
FileMenu->Append(MENU_Close, _T("&Close"), _T("Close the current document"));
FileMenu->Append(MENU_Save, _T("&Save"), _T("Save the current document"));
FileMenu->Append(MENU_SaveAs, _T("Save &As"), _T("Save current document with new name"));
FileMenu->Append(MENU_Quit, _T("&Quit"), _T("Quit the editor"));
Bind(wxEVT_MENU, &MyFrame::NewFile, this, MENU_New);
Bind(wxEVT_MENU, &MyFrame::OpenFile, this, MENU_Open);
Bind(wxEVT_MENU, &MyFrame::CloseFile, this, MENU_Close);
Bind(wxEVT_MENU, &MyFrame::SaveFile, this, MENU_Save);
Bind(wxEVT_MENU, &MyFrame::SaveAsFile, this, MENU_SaveAs);
Bind(wxEVT_MENU, &MyFrame::Quit, this, MENU_Quit);
MainEditBox = new wxTextCtrl(this, TEXT_Main, _T("Hi!\n"), wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE | wxTE_RICH | wxNO_BORDER, wxDefaultValidator, wxTextCtrlNameStr);
}
Now, as a learning exercise, I'm interested in figuring out how to Bind to a 'resize' event. What I'd like to do, just for grins, is display the size of the current frame in it's status bar while it's resizing: something like (300 : 200), but dynamically changing as the frame is resized. But I'm having no luck figuring out how to Bind to this event.
Can anybody offer me a tantalizing tip or two on how I might accomplish this? Thank you in advance.
In the class declaration for MyFrame, add one more event to handle the size event
void OnSize( wxSizeEvent& event );
or whatever you want to call it.
Then in MyFrame::MyFrame
, bind the event handler with a call like this:
Bind( wxEVT_SIZE, &MyFrame::OnSize, this );
Finally, If you want to display the current size of the frame in the status bar, the body of the event handler might look something like this:
void MyFrame::OnSize( wxSizeEvent& event )
{
wxStatusBar* sb=this->GetStatusBar();
if (sb != NULL )
{
int j = sb->GetFieldsCount();
sb->SetStatusText(
wxString::Format("(%d:%d)",
event.GetSize().GetWidth(),
event.GetSize().GetHeight()),
j-1);
}
event.Skip();
}
Basically this just looks up how many sections there are in the status bar and changes the text of the last one to display the frame's current size. If you know the number of sections in the status bar, you could remove this step and just use the zero based index of the section you want to use to display the text in the call to SetStatusText
instead.
The event.Skip()
and the end is to allow any other default processing for the size event to happen. Generally it's always a good idea to do this if you handle the wxEVT_SIZE
event.
If you want to explore other events in addition to the size event, you can find the full list of events supported for a class in the 'Events emitted by this class' section of the relevant documentation page. Here are the pages for wxWindow, wxTopLevelWindow, and wxFrame.
I hope that gets you started.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With