Bind() to Resize Event? Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
User avatar
conleec
Earned a small fee
Earned a small fee
Posts: 10
Joined: Wed Aug 22, 2018 4:19 pm
Location: Los Angeles

Bind() to Resize Event?

Post by conleec »

Hello Everybody,

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:

Code: Select all

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 the status bar while the frame is 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.
User avatar
doublemax
Moderator
Moderator
Posts: 19163
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Bind() to Resize Event?

Post by doublemax »

It works similar to the code you already have.

Code: Select all

Bind(wxEVT_SIZE , &MyFrame::OnSize, this, wxID_ANY);

Code: Select all

void MyFrame::OnSize(wxSizeEvent& event)
{
  SetStatusText( wxString::Format("new size: %d %d", event.GetSize().x, event.GetSize().y) );
  event.Skip();          // needed so that other size event handlers get called, i.e. for sizer based layouts
}
Use the source, Luke!
User avatar
conleec
Earned a small fee
Earned a small fee
Posts: 10
Joined: Wed Aug 22, 2018 4:19 pm
Location: Los Angeles

Re: Bind() to Resize Event?

Post by conleec »

Awesome, thanks for sharing. I had tried something similar, but was coming up short somehow. Will try this as soon as I get home!

PS: Given that I'm just getting started, is there anything else in my code snippet that you'd suggest doing differently, or better? I'd like to get off on the right foot, using current best practices, from the get-go. :D

Thanks again.