Search code examples
wxwidgets

Frame without borders and default controls. Sizers doesn't work


I try to write small GUI programm for windows using wx and wxFrame. I have this code in ctor:

MainFrame::MainFrame(wxPoint pos)
    : wxFrame(NULL, wxID_ANY, wxT("Sample text"), pos,
              wxDefaultSize, wxBORDER_NONE | wxFRAME_NOTASKBAR)
{
    SetSize(wxSize(250, 100));
    CreateLayout();
}

And CreateLayout function:

void MainFrame::CreateLayout()
{
    wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);

    sizer->Add(new wxStaticText(this, -1, "1000.56"), 0, wxLEFT|wxALIGN_CENTER_VERTICAL, 5);

    SetSizer(sizer);
}

As you can see, I want to align text to vertical center of the frame. But with this set of frame style flags sizers doesn't work! They work only if i set wxDEFAULT_FRAME_STYLE! But i need to have frame without borders and capture and close|minimize|maximize buttons, how can I get this work?


Solution

  • In the MainFrame constructor, move the call to SetSize() after the call to CreateLayout(). That's it.


    Normally, the call to Show() after creating the frame should make the above unnecessary, but it looks like in this case it doesn't (see the details below). Anyway, I think it makes sense to first set up all the sizers and controls inside the window, and then change the size to trigger a Layout(), just to be on the safe side.

    This also means an alternative solution is to leave the call to SetSize() where it is, and force a layout by adding a call to Layout() at the very end of your CreateLayout(). But... I like being efficient (read: lazy); I'd go with the first solution.


    EDIT: I've found some information regarding why this happens.

    For a wxFrame created with wxDEFAULT_FRAME_STYLE, the call to Show() ends up calling ::ShowWindow(), which triggers a WM_SIZE, which eventually triggers a Layout().

    For a wxFrame created with wxBORDER_NONE, ::ShowWindow() doesn't seem to trigger a WM_SIZE, which is why the solutions above for forcing the call to Layout() are necessary.