当动态改变 wxNotebook 中元素的大小时(例如隐藏一个按钮),整个笔记本元素的大小不会更新。这可能会留下空白或阻止显示某些元素。
通常,调用父窗口的 Fit() 或 FitInside() 应该可以解决问题,但对于 notebook 则不行。我们如何调整它的大小?
演示问题的最小示例:
#include <wx/wx.h>
#include <wx/gbsizer.h>
#include <wx/notebook.h>
const int HIDEME_ID = 512, SHOWME_ID = 513;
class MyApp: public wxApp
{
wxButton* hideme;
wxSizer* panel_sizer, *frame_sizer, *sw_sizer;
wxScrolledWindow* sw;
wxNotebook* nb;
void hide_button( wxCommandEvent& ) {
hideme->Hide();
relayout();
}
void relayout() {
/* Insert layout code here */
}
void show_button( wxCommandEvent& ) {
hideme->Show();
relayout();
}
virtual bool OnInit() {
wxFrame *frame = new wxFrame( NULL, wxID_ANY, _("Hello World"), wxPoint(50, 50), wxSize(450, 340) );
frame_sizer = new wxBoxSizer( wxVERTICAL );
frame->SetSizer( frame_sizer );
sw = new wxScrolledWindow( frame, wxID_ANY );
sw->SetScrollRate( 5, 5 );
frame_sizer->Add( sw, 1, wxEXPAND );
sw_sizer = new wxBoxSizer( wxVERTICAL );
sw->SetSizer( sw_sizer );
nb = new wxNotebook( sw, wxID_ANY );
wxPanel* panel = new wxPanel( nb, wxID_ANY );
nb->AddPage( panel, wxT("Tab1") );
sw_sizer->Add( new wxButton( sw, wxID_ANY, _("Button1"), wxDefaultPosition, wxSize(200,200) ), 0, wxEXPAND );
sw_sizer->Add( nb, 0, wxEXPAND );
sw_sizer->Add( new wxButton( sw, wxID_ANY, _("Button3") ), 1, wxEXPAND );
sw_sizer->Add( new wxButton( sw, wxID_ANY, _("Button4") ), 0, wxEXPAND );
panel_sizer = new wxBoxSizer( wxVERTICAL );
panel_sizer->Add( hideme = new wxButton(panel, HIDEME_ID, wxT("HideMe")), 0, wxEXPAND );
hideme->Hide();
panel_sizer->Add( new wxButton(panel, SHOWME_ID, wxT("ShowMe")), 0, wxEXPAND );
panel->SetSizer( panel_sizer );
frame->Show(true);
SetTopWindow(frame);
return true;
}
DECLARE_EVENT_TABLE();
};
BEGIN_EVENT_TABLE(MyApp, wxApp)
EVT_BUTTON(HIDEME_ID, MyApp::hide_button)
EVT_BUTTON(SHOWME_ID, MyApp::show_button)
END_EVENT_TABLE()
IMPLEMENT_APP( MyApp );