如何创建小部件(按钮)形式派生 Class

How to create a widgets (Buttons) form Derived Class

我是新手,我正在研究 wxwidgets 和 c++,在这个主题中我想问一下如何创建一个小部件(按钮)形式派生 Class(继承 MainFrame class)的 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);
}

我只想创建驱动形式的小部件 class。 非常感谢。

您需要向 wxFrame 派生框架添加一个面板 (wxPanel) 和一个 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);
}

其他有用资源: