自动调整wxGrid的大小

时间:2017-01-16 13:45:07

标签: c++ wxwidgets sizer

我用wcc和wxSmith构建的wxWidgets程序有几个带有文本和按钮的sizer,一个wxMathPlot窗口和一个wxGrid。当我在选择中选择某些内容时,网格会填充不同数量的信息,并在必要时增加更多行或删除不必要的行。当我从1行开始时,当做出选择时,仍然会显示一行,但是当我触摸窗口的边框以调整其大小时,所有内容都会调整大小。网格增长使得每行显示时没有滚动条,并且绘图窗口会根据网格离开的空间而增大或缩小。是否有可能对此行为进行编码,因此每次网格获得新输入时都会调用它? 我试过

UserGrid->Fit();
SendSizeEvent();

在网格方法的最后,但这不起作用。 我有一个OnResize()方法如下:

void Lizenz_Logfile_ReaderFrame::OnResize(wxSizeEvent& event)
{
    DoGetBestSize();
    UserGrid->Fit();
    //UserGrid->Refresh();
    Layout();

}

这是我的布局(如果我尝试过的某些设置会创建想要的行为)

 //(*Initialize(Lizenz_Logfile_ReaderFrame)
    wxBoxSizer* BoxSizer4;
    wxMenuItem* MenuItem2;
    wxFlexGridSizer* FlexGridSizer3;
    wxMenuItem* MenuItem1;
    wxFlexGridSizer* FlexGridSizer5;
    wxBoxSizer* BoxSizer2;
    wxMenu* Menu1;
    wxBoxSizer* BoxSizer1;
    wxMenuBar* MenuBar1;
    wxFlexGridSizer* FlexGridSizer6;
    wxMenu* Menu2;

    Create(parent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE|wxFULL_REPAINT_ON_RESIZE, _T("wxID_ANY"));
    SetClientSize(wxSize(525,713));
    Panel4 = new wxPanel(this, ID_PANEL4, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T("ID_PANEL4"));
    BoxSizer1 = new wxBoxSizer(wxVERTICAL);
    BoxSizer2 = new wxBoxSizer(wxHORIZONTAL);
    LogFilePickerCtrl = new wxFilePickerCtrl(Panel4, ID_FILEPICKERCTRL2, wxEmptyString, wxEmptyString, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxFLP_FILE_MUST_EXIST|wxFLP_OPEN|wxFLP_USE_TEXTCTRL, wxDefaultValidator, _T("ID_FILEPICKERCTRL2"));
    BoxSizer2->Add(LogFilePickerCtrl, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer1->Add(BoxSizer2, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    FlexGridSizer5 = new wxFlexGridSizer(0, 3, 0, 0);
    RadioButtonProe = new wxRadioButton(Panel4, ID_RADIOBUTTON4, _("PROE"), wxDefaultPosition, wxDefaultSize, wxRB_GROUP, wxDefaultValidator, _T("ID_RADIOBUTTON4"));
    FlexGridSizer5->Add(RadioButtonProe, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    RadioButtonEng = new wxRadioButton(Panel4, ID_RADIOBUTTON5, _("ENG"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_RADIOBUTTON5"));
    FlexGridSizer5->Add(RadioButtonEng, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    RadioButtonUi = new wxRadioButton(Panel4, ID_RADIOBUTTON6, _("UI"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_RADIOBUTTON6"));
    FlexGridSizer5->Add(RadioButtonUi, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    StaticText7 = new wxStaticText(Panel4, ID_STATICTEXT1, _("Label"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT1"));
    FlexGridSizer5->Add(StaticText7, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    ChoiceDate = new wxChoice(Panel4, ID_CHOICE3, wxDefaultPosition, wxDefaultSize, 0, 0, 0, wxDefaultValidator, _T("ID_CHOICE3"));
    FlexGridSizer5->Add(ChoiceDate, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer1->Add(FlexGridSizer5, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    FlexGridSizer6 = new wxFlexGridSizer(0, 3, 0, 0);
    StaticText10 = new wxStaticText(Panel4, ID_STATICTEXT20, _("Durchschnittlich genutzte Lizenzen \nam Tag"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT20"));
    FlexGridSizer6->Add(StaticText10, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    StaticTextDurchschnitt = new wxStaticText(Panel4, ID_STATICTEXT21, _("Anzahl"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT21"));
    FlexGridSizer6->Add(StaticTextDurchschnitt, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    FlexGridSizer6->Add(-1,-1,1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    StaticText12 = new wxStaticText(Panel4, ID_STATICTEXT22, _("Maximum genutzte Lizenzen"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT22"));
    FlexGridSizer6->Add(StaticText12, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    StaticTextMaximum = new wxStaticText(Panel4, ID_STATICTEXT23, _("Anzahl"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT23"));
    FlexGridSizer6->Add(StaticTextMaximum, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    StaticTextMaximumTime = new wxStaticText(Panel4, ID_STATICTEXT24, _("Wann"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT24"));
    FlexGridSizer6->Add(StaticTextMaximumTime, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    StaticText15 = new wxStaticText(Panel4, ID_STATICTEXT25, _("Minimum genutzte Lizenzen"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT25"));
    FlexGridSizer6->Add(StaticText15, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    StaticTextMinimum = new wxStaticText(Panel4, ID_STATICTEXT26, _("Anzahl"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT26"));
    FlexGridSizer6->Add(StaticTextMinimum, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    StaticTextMinimumTime = new wxStaticText(Panel4, ID_STATICTEXT27, _("Wann"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT27"));
    FlexGridSizer6->Add(StaticTextMinimumTime, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer1->Add(FlexGridSizer6, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer4 = new wxBoxSizer(wxHORIZONTAL);
    MathPlot1 = new mpWindow(Panel4, ID_MATHPLOT2, wxDefaultPosition, wxDefaultSize, wxRAISED_BORDER|wxTAB_TRAVERSAL);
    xAxis = new mpScaleX(_("Time"), 1, true);
    MathPlot1->AddLayer(xAxis);
    yAxis = new mpScaleY(_("Licenses"), 4, true);
    MathPlot1->AddLayer(yAxis);
    VectorLicenses = new mpFXYVector(_("Licenses"), 0);
    VectorLicenses->SetContinuity(true);
    MathPlot1->AddLayer(VectorLicenses);
    VectorUser = new mpFXYVector(_("User"), 0);
    VectorUser->SetContinuity(true);
    MathPlot1->AddLayer(VectorUser);
    MathPlot1->UpdateAll();
    MathPlot1->Fit();
    BoxSizer4->Add(MathPlot1, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer1->Add(BoxSizer4, 2, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    FlexGridSizer3 = new wxFlexGridSizer(0, 3, 0, 0);
    StaticText9 = new wxStaticText(Panel4, ID_STATICTEXT12, _("User\nFür Statistik Lizenz oben wählen"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT12"));
    FlexGridSizer3->Add(StaticText9, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    ChoiceUser = new wxChoice(Panel4, ID_CHOICE2, wxDefaultPosition, wxDefaultSize, 0, 0, 0, wxDefaultValidator, _T("ID_CHOICE2"));
    FlexGridSizer3->Add(ChoiceUser, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    FlexGridSizer3->Add(-1,-1,1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    StaticText4 = new wxStaticText(Panel4, ID_STATICTEXT13, _("Durchschnittliche Zeit \nLizenznutzung pro Tag"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT13"));
    FlexGridSizer3->Add(StaticText4, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    StaticTextStundenDurchschnitt = new wxStaticText(Panel4, ID_STATICTEXT14, _("Betrag"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT14"));
    FlexGridSizer3->Add(StaticTextStundenDurchschnitt, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    StaticText1 = new wxStaticText(Panel4, ID_STATICTEXT15, _("Stunden"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT15"));
    FlexGridSizer3->Add(StaticText1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    StaticText8 = new wxStaticText(Panel4, ID_STATICTEXT16, _("Tag mit Stundenmaximum"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT16"));
    FlexGridSizer3->Add(StaticText8, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    StaticTextStundenMax = new wxStaticText(Panel4, ID_STATICTEXT17, _("Stunden"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT17"));
    FlexGridSizer3->Add(StaticTextStundenMax, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    StaticTextTagMax = new wxStaticText(Panel4, ID_STATICTEXT18, _("Tag"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT18"));
    FlexGridSizer3->Add(StaticTextTagMax, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer1->Add(FlexGridSizer3, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    UserGrid = new wxGrid(Panel4, ID_GRID1, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE, _T("ID_GRID1"));
    UserGrid->CreateGrid(1,5);
    UserGrid->EnableEditing(false);
    UserGrid->EnableGridLines(true);
    UserGrid->SetColLabelValue(0, _("Lizenz"));
    UserGrid->SetColLabelValue(1, _("Tag"));
    UserGrid->SetColLabelValue(2, _("Von"));
    UserGrid->SetColLabelValue(3, _("Bis"));
    UserGrid->SetColLabelValue(4, _("Nutzungszeit"));
    UserGrid->SetDefaultCellFont( UserGrid->GetFont() );
    UserGrid->SetDefaultCellTextColour( UserGrid->GetForegroundColour() );
    BoxSizer1->Add(UserGrid, 0, wxEXPAND|wxALIGN_LEFT|wxALIGN_TOP, 0);
    Panel4->SetSizer(BoxSizer1);
    BoxSizer1->Fit(Panel4);
    BoxSizer1->SetSizeHints(Panel4);
    MenuBar1 = new wxMenuBar();
    Menu1 = new wxMenu();
    MenuItem1 = new wxMenuItem(Menu1, idMenuQuit, _("Quit\tAlt-F4"), _("Quit the application"), wxITEM_NORMAL);
    Menu1->Append(MenuItem1);
    MenuBar1->Append(Menu1, _("&File"));
    Menu2 = new wxMenu();
    MenuItem2 = new wxMenuItem(Menu2, idMenuAbout, _("About\tF1"), _("Show info about this application"), wxITEM_NORMAL);
    Menu2->Append(MenuItem2);
    MenuBar1->Append(Menu2, _("Help"));
    SetMenuBar(MenuBar1);
    StatusBar1 = new wxStatusBar(this, ID_STATUSBAR1, 0, _T("ID_STATUSBAR1"));
    int __wxStatusBarWidths_1[1] = { -1 };
    int __wxStatusBarStyles_1[1] = { wxSB_NORMAL };
    StatusBar1->SetFieldsCount(1,__wxStatusBarWidths_1);
    StatusBar1->SetStatusStyles(1,__wxStatusBarStyles_1);
    SetStatusBar(StatusBar1);

    Connect(ID_FILEPICKERCTRL2,wxEVT_COMMAND_FILEPICKER_CHANGED,(wxObjectEventFunction)&Lizenz_Logfile_ReaderFrame::OnLogFilePickerCtrlFileChanged1);
    Connect(ID_RADIOBUTTON4,wxEVT_COMMAND_RADIOBUTTON_SELECTED,(wxObjectEventFunction)&Lizenz_Logfile_ReaderFrame::OnRadioButtonProeSelect);
    Connect(ID_RADIOBUTTON5,wxEVT_COMMAND_RADIOBUTTON_SELECTED,(wxObjectEventFunction)&Lizenz_Logfile_ReaderFrame::OnRadioButtonEngSelect);
    Connect(ID_RADIOBUTTON6,wxEVT_COMMAND_RADIOBUTTON_SELECTED,(wxObjectEventFunction)&Lizenz_Logfile_ReaderFrame::OnRadioButtonUiSelect);
    Connect(ID_CHOICE3,wxEVT_COMMAND_CHOICE_SELECTED,(wxObjectEventFunction)&Lizenz_Logfile_ReaderFrame::OnChoiceDateSelect);
    Connect(ID_CHOICE2,wxEVT_COMMAND_CHOICE_SELECTED,(wxObjectEventFunction)&Lizenz_Logfile_ReaderFrame::OnChoiceUserSelect);
    Connect(idMenuQuit,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&Lizenz_Logfile_ReaderFrame::OnQuit);
    Connect(idMenuAbout,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&Lizenz_Logfile_ReaderFrame::OnAbout);
    //*)

有没有办法让窗口调整大小以便网格完全显示?

编辑:正如我在调试时发现的那样,OnResize()在实际调整窗口大小时永远不会被调用。它必须是在后台完成的事情,因为我对窗口大小没有做任何其他事情。

编辑2:在尝试了很多之后,似乎在使用UserGrid->AppendRow()后,网格不能很好地重绘。在该行之后,预先可见的滚动条消失,并且仅在用户调整窗口大小时才重新出现。我已经尝试了UserGrid->AdjustScrollbars(),然后UserGrid->ForceRefresh()没有效果。

1 个答案:

答案 0 :(得分:1)

来自http://docs.wxwidgets.org/trunk/overview_windowsizing.html, wxWidgets提供了两种主要的调整大小的方法:

  • wxWindow :: Fit()设置适合其子项的窗口大小。 添加每个子项的大小,然后此父窗口更改它 尺寸适合所有人。
  • wxWindow :: Layout()相反。孩子们会改变他们的体型, 根据sizer规则,它们可以适应可用的空间 他们的父母。 [...]是容器窗口的默认EVT_SIZE处理程序调用的内容

因为网格可以有数千行/列,所以它的大小可能很大。不要试图告诉父母要适应它。您最好为网格(或其大小)设置最大和最小大小,然后在每次更改行数/列数或大小时使用Fit()或Layout()。