gpt4 book ai didi

c++ - 如何创建窗口小部件(按钮)的派生类

转载 作者:行者123 更新时间:2023-12-02 10:33:45 25 4
gpt4 key购买 nike

我是新手,正在设计wxwidgets和c++的风格,在本主题中,我想问一下如何创建派生类(继承MainFrame类的APMainFrame)形式的小部件(按钮)

代码MainFrame GUI:

MainFrame::MainFrame( wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style ) : wxFrame( parent, id, title, pos, size, style )
{
this->SetSizeHints( wxDefaultSize, wxDefaultSize );
this->SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWFRAME ) );
.....
this->SetMenuBar( m_menubar1 );
this->Centre( wxBOTH );
}

和APMainFrame.h代码:
class APMainFrame : public MainFrame
{
public:
/** Constructor */
APMainFrame( wxWindow* parent );
//// end generated class members
wxButton* HelloWorld; // here i wanna create function and button for GUI
void OnExit(wxCommandEvent& event);
private:
DECLARE_EVENT_TABLE()
};

enum
{
BUTTON_Hello = wxID_HIGHEST + 1
};

和文件APMainFrame.cpp:
BEGIN_EVENT_TABLE(APMainFrame, MainFrame)
EVT_BUTTON(BUTTON_Hello, APMainFrame::OnExit)
END_EVENT_TABLE() // The button is pressed

APMainFrame::APMainFrame( wxWindow* parent )
:
MainFrame( parent )
{
HelloWorld = new wxButton(this, BUTTON_Hello, _T("Hello World"),
// shows a button on this window
wxDefaultPosition, wxDefaultSize, 0);

}

void APMainFrame::OnExit(wxCommandEvent& event)
{
Close(TRUE);
}

我只是想创建Widgets形式的Drived类。
非常感谢。

最佳答案

您需要在wxPanel派生框架中添加一个面板(wxFrame)和一个sizer。

您已经创建了按钮,但是还需要将其添加到框架中,以便它知道如何渲染它。

因此,像这样:

MainFrame( parent )
{
wxPanel* panel = new wxPanel(this, wxID_ANY);
wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
panel->SetSizer(sizer);
HelloWorld = new wxButton(this, BUTTON_Hello, _T("Hello World"),
// shows a button on this window
wxDefaultPosition, wxDefaultSize, 0);

sizer->Add(HelloWorld);
}

其他有用的资源:
  • Overview of Sizers
  • Sizers explanation with pictures
  • wxWidgets示例
  • 关于c++ - 如何创建窗口小部件(按钮)的派生类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61182190/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com