Help to create a button Very simple. 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
Nick
Earned some good credits
Earned some good credits
Posts: 107
Joined: Thu Apr 11, 2019 12:50 am

Help to create a button Very simple.

Post by Nick »

I am studying wxWidgets and wanting to understand how it works because I like to know what I am writing and why! That's why so many of my questions may be silly. But it helps me understand how it works.

In my example, trying to make the code as simple and as small as possible, and of course preferably correct.
I created a Button. But my code does not respect the position of the button. It only respects the position if I put another button.
I wish it had only 1 button. And for him to stay in the position I informed

Code: Select all

#include <wx/app.h>
#include <wx/frame.h>
#include <wx/button.h>

class FrmPrincipal: public wxFrame {
public:
   FrmPrincipal(): wxFrame(NULL, wxID_ANY, "2019") {
      wxButton *MyButton  = new wxButton(this, wxID_ANY, ("MyButton" ), wxPoint(10, 20), wxSize(80,30));
      wxButton *MyButton2 = new wxButton(this, wxID_ANY, ("MyButton2"), wxPoint(10, 60), wxSize(80,30)); // I didn't want this button
   }

};

class MyProgram: public wxApp {
   bool OnInit() {
      (new FrmPrincipal)->Show();
      return true;
   }
};

wxIMPLEMENT_APP(MyProgram);
With only 1 button disrespecting the position and size I informed
Image

With 2 buttons
Image
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Help to create a button Very simple.

Post by doublemax »

There's a "hidden" feature of a wxFrame: If it has exactly one child, then this child will be resized to fill the whole client area. Usually you create a wxPanel as the only child of the frame, then add all other controls as children of the wxPanel.
Use the source, Luke!
Kvaz1r
Super wx Problem Solver
Super wx Problem Solver
Posts: 357
Joined: Tue Jun 07, 2016 1:07 pm

Re: Help to create a button Very simple.

Post by Kvaz1r »

I don't know why it happens, but usually one can use sizers for widget layouting.
So you can do it this way:

Code: Select all

#include <wx/wx.h>

class FrmPrincipal : public wxFrame {
public:
	FrmPrincipal() : wxFrame(NULL, wxID_ANY, "2019") {
		wxBoxSizer* topSizer = new wxBoxSizer(wxVERTICAL);
		wxButton* MyButton = new wxButton(this, wxID_ANY, ("MyButton"), wxPoint(10, 20), wxSize(80, 30));
		topSizer->Add(MyButton, 0, wxALL, 5);
		this->SetSizer(topSizer);
		this->Layout();
	}
};

class MyProgram : public wxApp {
	bool OnInit() {
		(new FrmPrincipal)->Show();
		return true;
	}
};

wxIMPLEMENT_APP(MyProgram);
Nick
Earned some good credits
Earned some good credits
Posts: 107
Joined: Thu Apr 11, 2019 12:50 am

Re: Help to create a button Very simple.

Post by Nick »

doublemax wrote: Sat Aug 17, 2019 6:44 am There's a "hidden" feature of a wxFrame: If it has exactly one child, then this child will be resized to fill the whole client area. Usually you create a wxPanel as the only child of the frame, then add all other controls as children of the wxPanel.
Do you have any idea why they did so?
Can you tell if it is possible to disable this hidden feature?
I say this because I think having more control means more resource consumption.
Nick
Earned some good credits
Earned some good credits
Posts: 107
Joined: Thu Apr 11, 2019 12:50 am

Re: Help to create a button Very simple.

Post by Nick »

Kvaz1r wrote: Sat Aug 17, 2019 6:52 am So you can do it this way: ...
Thanks for the tip wxsizer is useful for me. But my idea is to save resources. Although I don't even know if there is any program that I will make that has only 1 control.
Nick
Earned some good credits
Earned some good credits
Posts: 107
Joined: Thu Apr 11, 2019 12:50 am

Re: Help to create a button Very simple.

Post by Nick »

doublemax wrote: Sat Aug 17, 2019 6:44 amUsually you create a wxPanel as the only child of the frame, then add all other controls as children of the wxPanel.
Assuming there is no other solution. And thinking about what is right to do in this case.
Usually any window usually has more than one control, like button, grid, labels, and texts. That way I have no problem with this resizing.
So my question and question is: Can I only use wxFrame in a window with multiple controls, or is it more advisable to always put the controls in a wxPanel?
Explaining otherwise...
wxFrame > Controls (Is it wrong to do so?)
wxFrame > wxPanel > Controls (Or should I always do like this?)

Sorry if I'm asking some bullshit.
Because I don't even know if to represent a window in my program wxFrame is the most recommended.
I understand that any window must first be a wxFrame
Nick
Earned some good credits
Earned some good credits
Posts: 107
Joined: Thu Apr 11, 2019 12:50 am

Re: Help to create a button Very simple.

Post by Nick »

Interestingly I could not display a wxWindows.
Is it possible to display a wxWindows?

Code: Select all

#include <wx/app.h>
#include <wx/window.h>

class FrmPrincipal: public wxWindow {
public:
   FrmPrincipal(): wxWindow(NULL, wxID_ANY) {
   }

};

class MyProgram: public wxApp {
   bool OnInit() {
      (new FrmPrincipal)->Show();
      return true;
   }
};

wxIMPLEMENT_APP(MyProgram);
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Help to create a button Very simple.

Post by doublemax »

wxFrame > wxPanel > Controls (Or should I always do like this?)
Yes. For various reasons.
Is it possible to display a wxWindows?
No. You need a toplevel window (wxDialog or wxFrame) as main window.
Use the source, Luke!
Post Reply