wxButton覆盖整个客户区C++

wxButton covering entire client area C++

我在 C++ 中使用 wxWidgets 3.1.5 创建了一个应用程序,除了我的主 window.
上的一个测试按钮外,一切正常 这是一张照片:

菜单栏、菜单及其功能完美无缺,但按钮覆盖了整个客户区。

代码如下:

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;
}

我正在使用 Visual Studio 2019。 (我在 wxWidgets 上关注了 OneLoneCoder 的 (javidx9) youtube 视频)

这就是只有一个 child 的 wxFrame 的行为方式。

如果您不想这样,请使用 wxSizer 布局您的按钮(定位、对齐、展开等)。

参考:

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