我有一个 Win32 项目(Windows 子系统)和一个在 wxFormBuilder 中创建的面板。问题是面板在实例化时需要一个父窗口,但我没有任何窗口句柄。
作为测试,我创建了一个 Frame 类型窗口,实例化时不需要任何参数,这可以在创建对象时显示窗口/框架。
由于我更喜欢Dialog类型的窗口(而不是框架),我怎样才能让Panel显示出来?
#include <wx/wx.h>
#include "gui.h"
class MyApp : public wxApp
{
public:
bool OnInit() override;
};
wxIMPLEMENT_APP(MyApp);
bool MyApp::OnInit()
{
MyPanel* mp = new MyPanel(NULL); //<---RUNTIME ERROR: "can't create wxWindow without parent"
mp->Show(true);
return true;
}
生成的 GUI 信息如下所示:
class MyPanel : public wxPanel
{
private:
protected:
wxButton* m_bnInit;
public:
MyPanel(wxWindow* parent, wxWindowID id = wxID_ANY, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize(500, 300), long style = wxTAB_TRAVERSAL, const wxString& name = wxEmptyString);
~MyPanel();
};
似乎没有办法只显示面板,它需要有一个框架。不确定是否有更好的方法,但这有效:
bool MyApp::OnInit()
{
wxFrame* parent = new wxFrame(NULL, wxID_ANY, "Parent Frame");
MyPanel* myWinPanel = new MyPanel(parent);
parent->Show(true);
myWinPanel->Show(true);
return true;
}