【问题标题】:wxWidgets. Resizing subarea in wxFrame like in CMake GUIwxWidgets。像在 CMake GUI 中一样调整 wxFrame 中的子区域大小
【发布时间】:2021-01-31 07:56:05
【问题描述】:

我是 wxWidgets 的新手,所以我正在尝试制作类似于 CMake GUI 中的窗口。如何将该区域划分为两个可自行调整大小的区域:

我正在使用 linux 进行开发和 cmake。 我没有使用任何 UI 编辑器

PS:对不起,我的英语不是我的母语:o

【问题讨论】:

标签: c++ cmake wxwidgets cmake-gui


【解决方案1】:

在 CMake GUI 中创建元素扩展的框架的方法是将 wxSplitterWindow 与 sizer 一起使用,并使用 sizer flags 构造函数中的比例来指定每个项目应扩展多少。

这是一个简短的例子:

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

// for all others, include the necessary headers (this file is usually all you
// need because it includes almost all "standard" wxWidgets headers)
#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif

#include <wx/splitter.h>

class MyFrame: public wxFrame
{
    public:
        MyFrame();
};

MyFrame::MyFrame()
        :wxFrame(NULL, wxID_ANY, "CMake style frame", wxDefaultPosition,
                 wxSize(600, 400))
{
    wxSplitterWindow* splitter = new wxSplitterWindow(this, wxID_ANY,
                                                      wxDefaultPosition, wxDefaultSize,
                                                      wxSP_LIVE_UPDATE);

    wxPanel* top = new wxPanel(splitter,wxID_ANY);

    wxStaticText* statText1 = new wxStaticText(top, wxID_ANY,
                                               "Where is the source");
    wxTextCtrl* textCtrl1 = new wxTextCtrl(top, wxID_ANY, wxEmptyString);
    wxButton* button1 = new wxButton(top, wxID_ANY, "Browse Source...");
    wxBoxSizer* sizer2 = new wxBoxSizer(wxHORIZONTAL);
    sizer2->Add(statText1,wxSizerFlags(0).Centre());
    sizer2->Add(textCtrl1,wxSizerFlags(1).Border(wxLEFT));
    sizer2->Add(button1,wxSizerFlags(0).Border(wxLEFT));

    wxStaticText* statText2 = new wxStaticText(top ,wxID_ANY, "Where to build the binaries:");
    wxStaticText* statText3 = new wxStaticText(top, wxID_ANY, "Search:");

    wxTextCtrl* textCtrl2 = new wxTextCtrl(top, wxID_ANY, wxEmptyString,
                                           wxDefaultPosition, wxDefaultSize,
                                           wxTE_MULTILINE|wxTE_DONTWRAP);
    wxStaticText* statText4 = new wxStaticText(top, wxID_ANY, "Press Configure:");
    wxButton* button2 = new wxButton(top, wxID_ANY, "Configure");

    wxBoxSizer* topSizer = new wxBoxSizer(wxVERTICAL);

    // Now Add the items to the backfround panel's sizer:
    topSizer->Add(sizer2,wxSizerFlags(0).Expand().DoubleBorder(wxALL));
    topSizer->Add(statText2,wxSizerFlags(0).DoubleBorder(wxLEFT|wxRIGHT|wxBOTTOM));
    topSizer->Add(statText3,wxSizerFlags(0).DoubleBorder(wxLEFT|wxRIGHT|wxBOTTOM));

    // The next item is added with proportion 1 so that it will expand in size.
    topSizer->Add(textCtrl2,
                  wxSizerFlags(1).Expand().DoubleBorder(wxLEFT|wxRIGHT|wxBOTTOM));
    topSizer->Add(statText4,
                  wxSizerFlags(0).Center().DoubleBorder(wxLEFT|wxRIGHT|wxBOTTOM));
    topSizer->Add(button2,wxSizerFlags(0).DoubleBorder(wxLEFT|wxRIGHT));

    top->SetSizer(topSizer);


    wxPanel* botton = new wxPanel(splitter,wxID_ANY);
    wxTextCtrl* textCtrl3 = new wxTextCtrl(botton, wxID_ANY, wxEmptyString,
                                           wxDefaultPosition, wxDefaultSize,
                                           wxTE_MULTILINE|wxTE_DONTWRAP);
    wxBoxSizer* botomSizer = new wxBoxSizer(wxVERTICAL);

    botomSizer->Add(textCtrl3,
                    wxSizerFlags(1).Expand().DoubleBorder(wxLEFT|wxRIGHT|wxBOTTOM));
    botton->SetSizer(botomSizer);

    // Add the top and bottom parts to the splitter, and set the sash gravity to
    // .5 so that both halfs will expand equally when the frame is resized.
    splitter->SplitHorizontally(top,botton);
    splitter->SetSashGravity(.5);

    Layout();
}

class MyApp : public wxApp
{
    public:
        virtual bool OnInit()
        {
            ::wxInitAllImageHandlers();
            MyFrame* frame = new MyFrame();
            frame->Show();
            return true;
        }
};

wxIMPLEMENT_APP(MyApp);

在 Windows 上,如下所示:

我没有从 CMake GUI 框架中复制每个小部件,但我认为那里已经足够让您了解正在发生的事情了。

【讨论】:

  • 我相信 OP 正在寻找动态调整大小的布局。
  • 呃。 CMake GUI 框架中有一个拆分器。您必须将鼠标光标缓慢移动到它上面才能在 Windows 上注意到它,所以我错过了它的存在。我已更新示例代码以添加拆分器。
猜你喜欢
  • 2021-10-19
  • 2012-06-25
  • 1970-01-01
  • 2011-11-26
  • 2014-05-22
  • 2019-05-27
  • 2014-11-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多