我正在爲教育目的寫一些wxWidgets樣本。 我的問題很簡單:我使用wxNotebook,我需要一些技巧來獲取單個選項卡的當前大小,特別是高度。 簡單來說,如果我把一個wxNotebook放在一個wxFrame裏面,例如,他的wxMenubar - 顯然這個wxMenubar佔據了高度 - 我只會得到標籤高度值,而不是wxFrame的高度值,它也包含大小wxMenubar。 我需要這些信息才能正確居中新組件。wxNotebook - 我如何獲得單個選項卡的高度和/或wxSize值?
有關示例,請參閱下面的示例代碼。
#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);
正如你所看到的,佈局是不完美的。 p.s.如果您知道使用wxGridBagSizer將組件集中到另一個更高效的方式,請告訴我。
這個解決方案不適合我......並且根本不工作。根據您的指示更改代碼,我會收到SEGMENTATION FAULT錯誤。 –