【问题标题】:Extendable wxSizer depending on window size可根据窗口大小扩展 wxSizer
【发布时间】:2019-04-11 13:58:38
【问题描述】:

我想使用 wxWidgets 制作一个包含一些按钮和面板的布局。问题是我无法在其他框架的中心创建可扩展的 wxSizer。

我尝试使用一些 wxWidgets 组件,例如 wxBoxSizer、wxFlexGridSizer 等。对于实现这种布局有什么好的建议吗?

窗口的简单表示是这样的:

【问题讨论】:

  • 你能展示一些你尝试过的代码吗?您是否使用过任何 RAD 工具,例如 wxSmith、wxGlade 等?用这样的工具创建ANY 布局非常简单...

标签: c++ wxwidgets


【解决方案1】:

您需要将其视为多个嵌套块。

  1. 在最高级别,您有 3 个块水平排列:右矩形、中心矩形和左矩形。为了做到这一点,您需要一个水平方向的wxBoxSizer
  2. 在第二层,中心矩形,您有 3 个块垂直排列:顶部矩形、中心矩形和底部矩形。您需要一个垂直方向的wxBoxSizer
  3. 为了让中心扩大以填充空间,您需要使用proportion 参数。

这里是一个代码 sn-p 示例:

wxBoxSizer* rootsizer = new wxBoxSizer(wxHORIZONTAL);

//TODO: the right side control is whatever you need it to be
wxPanel* rightPanel = new wxPanel(parent, wxID_ANY, wxDefaultPosition, rightPanelSize);
//NOTE the use of 0 as the proportion; this means the right panel will NOT expand
//horizontally; it is fixed in size.  The wxEXPAND flag means it will expand vertically
//to fill
rootsizer->Add(rightpanel, 0, wxEXPAND, 0);

//Our center sizer to contain our center controls
wxBoxSizer* centersizer = new wxBoxSizer(wxVERTICAL);
//NOTE the use of 1 as the proportion; this means the center panel will expand HORIZONTALLY to fill all available space
rootsizer->Add(centersizer, 1, wxEXPAND, 0);

//TODO: whatever control goes on the left
wxPanel* leftpanel = new wxPanel(parent, wxID_ANY, wxDefaultPosition, leftPanelSize);
//Proportion is again 0 to prevent expanding horizontally
rootsizer->Add(leftpanel, 0, wxEXPAND, 0);

//Now the second level

//TODO: whatever control goes on the top
wxPanel* toppanel = new wxPanel(parent, wxID_ANY, wxDefaultPosition, topPanelSize);
//Proportion is 0 to prevent expanding VERTICALLY (because we are now in the center sizer
//which is a vertical sizer
centersizer->Add(toppanel, 0, wxEXPAND, 0);

//Our final depth, the centermost control, is set to a proportion of 1 to fill the space
//vertically
//TODO: whatever is your center control
wxPanel* centerpanel = new wxPanel(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize);
centersizer->Add(centerpanel, 1, wxEXPAND, 0);

//And then the bottom control at proportion 0
wxPanel* bottompanel= new wxPanel(parent, wxID_ANY, wxDefaultPosition, bottomPanelSize);
centersizer->Add(bottompanel, 0, wxEXPAND, 0);

通过组合多个级别的sizer,以及wxEXPAND 并正确分配proportion,您可以获得适当的灵活性。你只需要学会从一组只在一个方向扩展和收缩的控件的角度来看待它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-07
    • 2016-06-08
    • 1970-01-01
    • 2014-06-21
    • 1970-01-01
    • 1970-01-01
    • 2012-05-06
    相关资源
    最近更新 更多