Search code examples
c++wxwidgets

wxButton covering entire client area C++


I have made an application using wxWidgets 3.1.5 in C++ and everything is working fine except a test button that I have on my main window.
Here's a pic:
enter image description here

The menubar, menus and their functions work perfectly but the button covers the entire client area.

Here's the code:

main.h

#pragma once
#include <wx\wx.h>
#include "mainFrame.h"

class main : public wxApp
{
private:
    mainFrame* frame;
public:
    virtual bool OnInit();
};

main.cpp

#include "main.h"

wxIMPLEMENT_APP(main);

bool main::OnInit()
{
    frame = new mainFrame("Kill Me", wxPoint(15, 10), wxSize(640, 480));
    frame->Show();
    return true;
}

mainFrame.h

#pragma once
#include "About.h"

using str = std::string;

class mainFrame : public wxFrame
{
public:
    mainFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
    ~mainFrame();
private:
    About* abtF = NULL;

    wxButton* hewwo = NULL;

    wxMenuBar* mbar = NULL;
    wxMenu* sett = NULL;
    wxMenu* quitApp = NULL;
    wxMenu* abt = NULL;

    void onHewwo(wxCommandEvent& evt);
    void onSett(wxCommandEvent& evt);
    void quit(wxCommandEvent& evt);
    void about(wxCommandEvent& evt);

    wxDECLARE_EVENT_TABLE();
};

enum {
    ID_SETT = 1,
    ID_BTN = 2
};

mainFrame.cpp

#include "mainFrame.h"

wxBEGIN_EVENT_TABLE(mainFrame, wxFrame)
    EVT_BUTTON(ID_BTN, onHewwo)
    EVT_MENU(ID_SETT, onSett)
    EVT_MENU(wxID_EXIT, quit)
    EVT_MENU(wxID_ABOUT, about)
wxEND_EVENT_TABLE()


mainFrame::mainFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
    :
    wxFrame(nullptr, wxID_ANY, title, pos, size) {
    
    hewwo = new wxButton(this, ID_BTN, "Hewwo World", wxPoint(15, 15), wxSize(70, 20));


    sett = new wxMenu();
    sett->AppendSeparator();
    sett->Append(ID_SETT, "&Settings");

    quitApp = new wxMenu();
    quitApp->AppendSeparator();
    quitApp->Append(wxID_EXIT, "&Quit this crap");

    abt = new wxMenu();
    abt->AppendSeparator();
    abt->Append(wxID_ABOUT, "&About");

    mbar = new wxMenuBar();
    mbar->Append(sett, "&Settings");
    mbar->Append(abt, "&About");
    mbar->Append(quitApp, "&Quit");

    SetMenuBar(mbar);
}

void mainFrame::onHewwo(wxCommandEvent& evt) {
    wxMessageBox("Hewwo", "Hewwo", wxOK | wxICON_INFORMATION, this);
}

void mainFrame::onSett(wxCommandEvent& evt) {
    wxMessageBox("Settings", "Settings", wxOK | wxICON_INFORMATION, this); // Just a test
}

void mainFrame::about(wxCommandEvent& evt) {
    abtF = new About(wxPoint(10, 10), wxSize(480, 320));
    abtF->Show();
}

void mainFrame::quit(wxCommandEvent& evt) {
    Close(true);
}

mainFrame::~mainFrame() {
    delete abtF;
}

I'm using Visual Studio 2019. (I followed OneLoneCoder's (javidx9) youtube video on wxWidgets)


Solution

  • That is how a wxFrame with only one child behaves.

    If you don't want that, use a wxSizer to layout your button (position, align, expand etc).

    Reference:

    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

    wxFrame docs -> Default event processing -> wxEVT_SIZE