Page 1 of 1

Help to create a button Very simple.

Posted: Sat Aug 17, 2019 12:54 am
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

Re: Help to create a button Very simple.

Posted: Sat Aug 17, 2019 6:44 am
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.

Re: Help to create a button Very simple.

Posted: Sat Aug 17, 2019 6:52 am
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);

Re: Help to create a button Very simple.

Posted: Sat Aug 17, 2019 1:22 pm
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.

Re: Help to create a button Very simple.

Posted: Sat Aug 17, 2019 1:26 pm
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.

Re: Help to create a button Very simple.

Posted: Sat Aug 17, 2019 1:48 pm
by Nick
doublemax wrote:
Sat Aug 17, 2019 6:44 am
Usually 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

Re: Help to create a button Very simple.

Posted: Sat Aug 17, 2019 2:05 pm
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);

Re: Help to create a button Very simple.

Posted: Sat Aug 17, 2019 4:38 pm
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.