如何正确调整wxWindows的大小

时间:2016-12-08 14:33:27

标签: c++ layout wxwidgets

我有一个用Code :: blocks编写的wxWidgets C ++程序。在台式电脑上它看起来不错,但不适用于Windows平板电脑。

enter image description here

以下图片应该是什么样子(至少所有文字都应该是可见的): Window on desktop PC

我已经尝试了几种方法,目前我的代码看起来像这样:

TrackDialog::TrackDialog(wxWindow* parent,wxWindowID id,const wxPoint& pos,
                         const wxSize& size)
{
    wxBoxSizer* BoxSizer4;
    wxBoxSizer* BoxSizer5;
    wxBoxSizer* BoxSizer2;
    wxBoxSizer* BoxSizer1;
    wxBoxSizer* BoxSizer3;
    wxBoxSizer* pPanelSizer;

    Create(parent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxSTAY_ON_TOP|wxSUNKEN_BORDER, _T("wxID_ANY"));
    SetMinSize(wxSize(500,400));
    Panel1 = new wxPanel(this, ID_PANEL1, wxPoint(184,136), wxDefaultSize, wxTAB_TRAVERSAL, _T("ID_PANEL1"));
    pPanelSizer = new wxBoxSizer(wxVERTICAL);
    BoxSizer1 = new wxBoxSizer(wxHORIZONTAL);
    StaticSelectText = new wxStaticText(Panel1, ID_STATICTEXT1, _("Select a *.xml file."), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT1"));
    BoxSizer1->Add(StaticSelectText, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    pPanelSizer->Add(BoxSizer1, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer2 = new wxBoxSizer(wxHORIZONTAL);
    XmlFilePickerCtrl = new wxFilePickerCtrl(Panel1, ID_FILEPICKERCTRL1, _T("./resources/"), wxEmptyString, _T("*.xml"), wxDefaultPosition, wxDefaultSize, wxFLP_FILE_MUST_EXIST|wxFLP_OPEN|wxFLP_USE_TEXTCTRL, wxDefaultValidator, _T("ID_FILEPICKERCTRL1"));
    BoxSizer2->Add(XmlFilePickerCtrl, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    pPanelSizer->Add(BoxSizer2, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer3 = new wxBoxSizer(wxHORIZONTAL);
    StaticNumberText = new wxStaticText(Panel1, ID_STATICTEXT2, _("Tracknumber"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT2"));
    StaticNumberText->Disable();
    BoxSizer3->Add(StaticNumberText, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    pPanelSizer->Add(BoxSizer3, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer4 = new wxBoxSizer(wxHORIZONTAL);
    ChoiceTrack = new wxChoice(Panel1, ID_CHOICE1, wxDefaultPosition, wxDefaultSize, 0, 0, 0, wxDefaultValidator, _T("ID_CHOICE1"));
    ChoiceTrack->Disable();
    BoxSizer4->Add(ChoiceTrack, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    pPanelSizer->Add(BoxSizer4, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer5 = new wxBoxSizer(wxHORIZONTAL);
    ButtonOk = new wxButton(Panel1, ID_BUTTON1, _("OK"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON1"));
    BoxSizer5->Add(ButtonOk, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    pPanelSizer->Add(BoxSizer5, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    Panel1->SetSizer(pPanelSizer);
    pPanelSizer->Fit(Panel1);
    pPanelSizer->SetSizeHints(Panel1);
    Center();

    Connect(ID_FILEPICKERCTRL1,wxEVT_COMMAND_FILEPICKER_CHANGED,(wxObjectEventFunction)&TrackDialog::OnXmlFilePickerCtrlFileChanged);
    Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&TrackDialog::OnButtonOkClick);
    //*)
    Connect(ID_CHOICE1,wxEVT_COMMAND_CHOICE_SELECTED,(wxObjectEventFunction)&TrackDialog::OnChoiceTrackSelect1);
    SetSize(DoGetBestClientSize());
    Fit();
}

此外,另一个对话框太大了: Other dialog 使用此代码:

PortsDialog::PortsDialog(wxWindow* parent,wxWindowID id,const wxPoint& pos,
                         const wxSize& size)
{
    //(*Initialize(PortsDialog)
    wxBoxSizer* BoxSizer4;
    wxBoxSizer* BoxSizer5;
    wxBoxSizer* BoxSizer2;
    wxBoxSizer* BoxSizer1;
    wxBoxSizer* BoxSizer3;

    Create(parent, id, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE, _T("id"));
    SetClientSize(wxDefaultSize);
    Move(wxDefaultPosition);
    Panel1 = new wxPanel(this, ID_PANEL1, wxPoint(200,320), wxDefaultSize, wxTAB_TRAVERSAL, _T("ID_PANEL1"));
    BoxSizer1 = new wxBoxSizer(wxVERTICAL);
    BoxSizer2 = new wxBoxSizer(wxVERTICAL);
    StaticComText = new wxStaticText(Panel1, ID_STATICTEXT1, _("COM Ports"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT1"));
    BoxSizer2->Add(StaticComText, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer1->Add(BoxSizer2, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer3 = new wxBoxSizer(wxHORIZONTAL);
    TextCtrlVibroTacPort = new wxTextCtrl(Panel1, ID_TEXTCTRL1, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL1"));
    BoxSizer3->Add(TextCtrlVibroTacPort, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    ButtonVibroTacPort = new wxButton(Panel1, ID_BUTTON1, _("Set VibroTac Port"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON1"));
    BoxSizer3->Add(ButtonVibroTacPort, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer1->Add(BoxSizer3, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer4 = new wxBoxSizer(wxHORIZONTAL);
    TextCtrlGnssPort = new wxTextCtrl(Panel1, ID_TEXTCTRL2, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL2"));
    BoxSizer4->Add(TextCtrlGnssPort, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    ButtonGnssPort = new wxButton(Panel1, ID_BUTTON2, _("Set GNSS Port"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON2"));
    BoxSizer4->Add(ButtonGnssPort, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer1->Add(BoxSizer4, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer5 = new wxBoxSizer(wxHORIZONTAL);
    ButtonClose = new wxButton(Panel1, ID_BUTTON3, _("Close"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON3"));
    BoxSizer5->Add(ButtonClose, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer1->Add(BoxSizer5, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    Panel1->SetSizer(BoxSizer1);
    BoxSizer1->Fit(Panel1);
    BoxSizer1->SetSizeHints(Panel1);

    Connect(ID_TEXTCTRL1,wxEVT_COMMAND_TEXT_ENTER,(wxObjectEventFunction)&PortsDialog::OnButtonVibroTacPortClick);
    Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&PortsDialog::OnButtonVibroTacPortClick);
    Connect(ID_TEXTCTRL2,wxEVT_COMMAND_TEXT_ENTER,(wxObjectEventFunction)&PortsDialog::OnButtonGnssPortClick);
    Connect(ID_BUTTON2,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&PortsDialog::OnButtonGnssPortClick);
    Connect(ID_BUTTON3,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&PortsDialog::OnButtonCloseClick);
    //*)
    SetSize(DoGetBestClientSize());
    Fit();
}

我已经看到了许多不同的方法来设置大小,但直到现在我还无法找到一种设置大小的方法,以便一切看起来都很好。 谢谢你的帮助。

编辑: 使用PortsDialog作为Tibo建议的SetSizerAndFit(BoxSizer1);会导致对话框为空:

Empty PortsDialog

在TrackDialog中使用SetSizerAndFit(pPanelSizer);,它在启动时看起来很好,但是如果我重新加载文本,那么它的一半会再次出现在窗口中,如果窗口关闭,程序会崩溃......这太令人困惑了。

关于版本:我的所有设备(平板电脑和PC)上都有Windows 10 Pro 64位版本,我使用wxWidgets v3.0.2和Code :: Blocks v13.12(使用GCC编译器跟随C ++ 11) ISO)

1 个答案:

答案 0 :(得分:0)

目前还不清楚这个问题是什么,这里似乎有几个完全不相关的东西,我建议关闭/删除这个,并在单独的新提交中询问每一个。

但是为了尝试回答第一个问题,文字并不完全可见,因为平板电脑上没有足够的空间。它不会自行换行,您需要使用支持换行的控件或手动调用Wrap() {/ 1}}。