wxNotebook - 如何获取单个选项卡的高度和/或 wxSize 值?
我正在编写一些 wxWidgets 示例用于教育目的。 我的问题很简单:我正在使用 wxNotebook,我需要一些技巧来获取单个选项卡的当前大小,尤其是高度。 简单来说,如果我将一个 wxNotebook 放在一个 wxFrame 中,例如,它有他的 wxMenubar - 显然,它占据了高度 - ,我只会得到选项卡高度值,而不是 wxFrame 高度值,其中还包括 wxMenubar 的大小。 我需要这些信息来正确居中新组件。
有关示例,请参阅下面的示例代码。
#include "wx/wx.h"
#include "wx/gbsizer.h"
class MyFrame : public wxFrame
{
public:
MyFrame() : wxFrame(NULL, wxID_ANY, wxT("Application"), wxDefaultPosition, wxSize(500, 300))
{
wxNotebook *tabs = new wxNotebook(this, wxID_ANY, wxPoint(-1,-1), wxSize(-1,-1), wxNB_TOP);
wxPanel *extPanel = new wxPanel(tabs, wxID_ANY); // external panel will be directly added to wxNotebook
wxPanel *innerPanel = new wxPanel(extPanel, wxID_ANY, wxDefaultPosition, wxDefaultSize); /* for now, innerPanel has default size */
innerPanel->SetBackgroundColour(wxColor(0, 0, 255)); // I change background color for debug only
innerPanel->SetMinSize(wxSize(200, 200)); // I use SetMinSize() method to communicate to the sizer _required_ size for the panel
wxGridBagSizer *gbs = new wxGridBagSizer(3, 3); // I use a wxGridBagSizer to position one panel inside external
/* **** THE FOLLOWING IS THE CRITICAL LINE **** */
wxSize mainSize = this->GetSize(); /* for now, I get the _wxFRAME_ wxSize; I would get wxNOTEBOOK size instead */
wxSize innPSize = innerPanel->GetMinSize(); // I get current (Min)Size of innerPanel
wxSize emptyCellSize((mainSize.GetWidth() - innPSize.GetWidth()) / 2, (mainSize.GetHeight() - innPSize.GetHeight()) / 2);
gbs->SetEmptyCellSize(emptyCellSize); // I Use SetEmptyCellSize() method to center the inner panel
gbs->Add(innerPanel, wxGBPosition(1, 1)); // 1, 1: central cell
extPanel->SetSizer(gbs);
tabs->AddPage(extPanel, wxT("Positioning test"));
Show(true);
}
};
class MyApp : public wxApp
{
public:
virtual bool OnInit()
{
MyFrame *frame = new MyFrame();
}
};
IMPLEMENT_APP(MyApp);
正如您所看到的,布局并不完美。 ps 如果您知道另一种更有效的使用 wxGridBagSizer 使组件居中的方法,请告诉我。
I'm writing some wxWidgets samples for educational purposes.
My problem is simple: I'm using wxNotebook, and I need some trick to get current size of a single tab, especially height.
In simple terms, if I place a wxNotebook inside a wxFrame which have, for example, his wxMenubar - which, obviously, take up place in height -, I would get tab height value only, NOT wxFrame height value which also includes size of wxMenubar.
I need this information to properly center new components.
See the sample code below for an example.
#include "wx/wx.h"
#include "wx/gbsizer.h"
class MyFrame : public wxFrame
{
public:
MyFrame() : wxFrame(NULL, wxID_ANY, wxT("Application"), wxDefaultPosition, wxSize(500, 300))
{
wxNotebook *tabs = new wxNotebook(this, wxID_ANY, wxPoint(-1,-1), wxSize(-1,-1), wxNB_TOP);
wxPanel *extPanel = new wxPanel(tabs, wxID_ANY); // external panel will be directly added to wxNotebook
wxPanel *innerPanel = new wxPanel(extPanel, wxID_ANY, wxDefaultPosition, wxDefaultSize); /* for now, innerPanel has default size */
innerPanel->SetBackgroundColour(wxColor(0, 0, 255)); // I change background color for debug only
innerPanel->SetMinSize(wxSize(200, 200)); // I use SetMinSize() method to communicate to the sizer _required_ size for the panel
wxGridBagSizer *gbs = new wxGridBagSizer(3, 3); // I use a wxGridBagSizer to position one panel inside external
/* **** THE FOLLOWING IS THE CRITICAL LINE **** */
wxSize mainSize = this->GetSize(); /* for now, I get the _wxFRAME_ wxSize; I would get wxNOTEBOOK size instead */
wxSize innPSize = innerPanel->GetMinSize(); // I get current (Min)Size of innerPanel
wxSize emptyCellSize((mainSize.GetWidth() - innPSize.GetWidth()) / 2, (mainSize.GetHeight() - innPSize.GetHeight()) / 2);
gbs->SetEmptyCellSize(emptyCellSize); // I Use SetEmptyCellSize() method to center the inner panel
gbs->Add(innerPanel, wxGBPosition(1, 1)); // 1, 1: central cell
extPanel->SetSizer(gbs);
tabs->AddPage(extPanel, wxT("Positioning test"));
Show(true);
}
};
class MyApp : public wxApp
{
public:
virtual bool OnInit()
{
MyFrame *frame = new MyFrame();
}
};
IMPLEMENT_APP(MyApp);
As you can see, layout is imperfect.
p.s. If you know another, more efficient way to center a component using wxGridBagSizer, let me know.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可以通过将网格的列或行标记为“可增长”来完成网格袋布局的居中单元格。当网格单元格被标记为可增长时,所有单元格都会增长以占据父窗口为其提供的所有空间(并且当父窗口调整大小时,单元格也会自动调整大小)。例如,如果您有 3 列并希望中间的列居中,您可以执行以下操作:
这里创建了 3 列;对于每一列,必须调用 AddGrowableCol() 方法以使列展开;比例变量告诉大小每列相对于所有其他列应该有多大。在这种情况下,中间的列将是其他列的两倍大。 AddGrowableRow 方法确保列将占据父面板的整个高度。
Centering cells of a gridbag layouts can be done by labeling the columns or rows of the grid as "growable." When a grid cell is labeled as growable, all of the cells will grow to take up all of the space given to it by the parent window (also the cells are automatically resized when the parent window is resized). For example, if you had 3 columns and wanted the middle column to be centered you can do this:
Here, 3 columns are created; for each column the AddGrowableCol() method has to be called to make the columns expand; the proportion variable tells the size how big each column should be in relation to all others. In this case, the middle column will be twice as big as the others. The AddGrowableRow method makes sure that the columns will take up the entire height of the parent panel.