在调整子面板大小后强制窗口在Wx中调整大小

时间:2014-01-24 16:02:09

标签: wxwidgets wxperl

我有一个包含带有ListBox和Panel的BoxSizer的Frame。我想以编程方式调整面板大小,然后强制框架调整大小。我可以按如下方式调整Panel的大小,但是如何强制BoxSizer或Frame调整大小?

下面的示例代码创建了一个带有调整大小菜单选项的应用程序,该选项调整了面板的大小:

#!/usr/bin/perl

use strict;
use warnings;
use Wx;

package TestApp;

use base qw (Wx::App);
use Wx qw (wxMINIMIZE_BOX wxSYSTEM_MENU wxCAPTION wxCLOSE_BOX wxCLIP_CHILDREN);

sub OnInit {
    my $self  = shift;
    my $frame = TestFrame->new (
        undef,
        -1,
        'Test App',
        [-1, -1],
        [-1, -1],
        wxMINIMIZE_BOX | wxSYSTEM_MENU | wxCAPTION | wxCLOSE_BOX | wxCLIP_CHILDREN
    );

    $frame->Show (1);
    $self->SetTopWindow ($frame);

    return 1;
}

package TestFrame;

use base qw (Wx::Frame);
use Wx::Event qw (EVT_MENU);
use Wx qw (wxHORIZONTAL wxEXPAND wxALL wxBORDER_SIMPLE);

our @id = (0 .. 100);

sub new {
    my $class = shift;
    my $self  = $class->SUPER::new (@_);

    my $boxsizer = Wx::BoxSizer->new (wxHORIZONTAL);

    my $listbox = Wx::ListBox->new (
        $self,
        -1,
        [-1, -1],
        [64, -1]
    );

    my $panel = Wx::Panel->new (
        $self,
        -1,
        [-1, -1],
        [-1, -1],
        wxBORDER_SIMPLE
    );

    $boxsizer->Add (
        $listbox,
        0,
        wxEXPAND | wxALL,
        5
    );

    $boxsizer->Add (
        $panel,
        0,
        wxALL,
        5
    );

    my $menubar = Wx::MenuBar->new ();
    my $menu = Wx::Menu->new ();
    $menu->Append ($id[0], "Small Panel");
    $menu->Append ($id[1], "Large Panel");
    $menubar->Append ($menu, 'File');
    $self->SetMenuBar ($menubar);

    $panel->SetClientSize (100, 200);
    $self->SetSizerAndFit ($boxsizer);

    EVT_MENU ($self, $id[0], sub {
        $panel->SetMinSize ([-1, -1]);
        $panel->SetClientSize ([100, 200]);
        $panel->SetMinSize ($panel->GetClientSize ());
        $self->SetClientSize ($boxsizer->GetSize ());
        $self->Fit ();
    });

    EVT_MENU ($self, $id[1], sub {
        $panel->SetMinSize ([-1, -1]);
        $panel->SetClientSize ([200, 300]);
        $panel->SetMinSize ($panel->GetClientSize ());
        $self->SetClientSize ($boxsizer->GetSize ());
        $self->Fit ();
    });

    return $self;
}

package main;

my ($app) = TestApp->new ();

$app->MainLoop ();

1 个答案:

答案 0 :(得分:2)

如果您拨打$self->Fit()而非$panel->SetMinSize(),请致电SetClientSize()