strange behaviour (button fills whole window) 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.
Post Reply
PGU8
Earned a small fee
Earned a small fee
Posts: 10
Joined: Tue May 26, 2020 4:53 pm

strange behaviour (button fills whole window)

Post by PGU8 »

I highly doubt that this is a bug, but I just can't find any explanation for this weird behaviour
when there are 2 buttons, it works as I expected..

Code: Select all

class MyFrame : public wxFrame
{
public:
    MyFrame() : wxFrame(NULL, wxID_ANY, "Test")
    {
        wxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
        wxButton* one = new wxButton(this, wxID_ANY, "Hello", { 100, 50 }, { 50,50 });
        sizer->Add(one);
        wxButton* two = new wxButton(this, wxID_ANY, "Hello", { 150, 50 }, { 50,50 });
        sizer->Add(two);
        Show(true);
    }
private:
};
2 buttons result:
double.png
double.png (3.16 KiB) Viewed 495 times
but when there is a single button with defined position & size, it fills the whole screen..

Code: Select all

class MyFrame : public wxFrame
{
public:
    MyFrame() : wxFrame(NULL, wxID_ANY, "Test")
    {
        wxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
        wxButton* one = new wxButton(this, wxID_ANY, "Hello", { 100, 50 }, { 50,50 });
        sizer->Add(one);
        //wxButton* two = new wxButton(this, wxID_ANY, "Hello", { 150, 50 }, { 50,50 });
        //sizer->Add(two);
        Show(true);
    }
private:
};
single button result:
single.png
single.png (2.89 KiB) Viewed 495 times
I would like the single button to be as the size of what I defined, 50 by 50...
User avatar
doublemax
Moderator
Moderator
Posts: 19159
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: strange behaviour (button fills whole window)

Post by doublemax »

It's not a bug, it's a feature ;)

If a wxFrame has exactly one immediate child, this child will always be stretched to fill the whole client area.

Use a wxPanel as only child of the frame, then put your button(s) onto the panel. This will also get rid of the ugly dark background color.

BTW: The ugly look of the buttons makes me believe that your application does not contain a manifest, then you get the old "Windows 95" style look. If you don't know how to add a manifest to your application, search for "manifest" in the forum. There should be several threads about this.
Use the source, Luke!
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: strange behaviour (button fills whole window)

Post by PB »

doublemax wrote: Fri May 29, 2020 10:30 am It's not a bug, it's a feature ;)
It is even a documented feature for wxFrame.
wxFrame docs wrote:if the frame has exactly one child window, not counting the status and toolbar, this child is resized to take the entire frame client area. If two or more windows are present, they should be laid out explicitly either by manually handling wxEVT_SIZE or using sizers;

doublemax wrote: Fri May 29, 2020 10:30 am Use a wxPanel as only child of the frame, then put your button(s) onto the panel. This will also get rid of the ugly dark background color.
Additionally, wxPanel will handle <Tab> navigation between controls, which would not work if the controls were placed directly on the frame.
Post Reply