今天查了好久的资料,终于把分割窗口给整出来了。Wx::perl学习的资料还真是少啊,拿出来跟大家分享下,方便学习Wx::perl的朋友。
- use Wx;
- package WxSplitterWindow;
- use base qw(Wx::SplitterWindow Class::Accessor::Fast);
- use Wx qw(:splitterwindow wxDefaultPosition wxDefaultSize);
- use Wx::Event qw(EVT_SPLITTER_SASH_POS_CHANGED EVT_SPLITTER_SASH_POS_CHANGING
- EVT_SPLITTER_UNSPLIT EVT_MENU);
- __PACKAGE__->mk_accessors(qw(first_window second_window));
- sub new {
- my ( $class, $parent ) = @_;
- my $self =
- $class->SUPER::new( $parent, -1, wxDefaultPosition, wxDefaultSize,
- wxSP_3D | wxSP_LIVE_UPDATE );
- my $top = Wx::GetTopLevelParent($parent);
- #EVT_SPLITTER_UNSPLIT( $self, $self, \&split_horizontally);
- my $filemenu = Wx::Menu->new;
- EVT_MENU(
- $top,
- $filemenu->Append( -1, "Split vertically" ),
- sub { $self->split_vertically }
- );
- EVT_MENU(
- $top,
- $filemenu->Append( -1, "Split horizontally" ),
- sub { $self->split_horizontally }
- );
- $filemenu->AppendSeparator;
- EVT_MENU( $top, $filemenu->Append( -1, "Unsplit" ),
- sub { $self->unsplit } );
- $self->first_window( Wx::TextCtrl->new( $self, -1, 'First pane' ) );
- $self->second_window( Wx::TextCtrl->new( $self, -1, 'Second pane' ) );
- #$self->Initialize( $self->first_window );
- #一开始就显示为分割的上下两窗口
- $self->split_horizontally;
- $self->{menu} = [ '&Splitter', $filemenu ];
- return $self;
- }
- sub split_horizontally {
- my ( $self, $event ) = @_;
- $self->Unsplit if $self->IsSplit;
- $self->first_window->Show;
- $self->second_window->Show;
- $self->SplitHorizontally( $self->first_window, $self->second_window );
- }
- sub split_vertically {
- my ( $self, $event ) = @_;
- $self->Unsplit if $self->IsSplit;
- $self->first_window->Show;
- $self->second_window->Show;
- $self->SplitVertically( $self->first_window, $self->second_window );
- }
- sub unsplit {
- my ( $self, $event ) = @_;
- $self->Unsplit if $self->IsSplit;
- }
- package MyApp;
- use base 'Wx::App';
- use Wx qw(:sizer wxDefaultPosition wxDefaultSize wxID_EXIT);
- use Wx::Event qw(EVT_MENU);
- our @id = ( 0 .. 5 ); # IDs array
- sub OnInit {
- my $self = shift;
- my $frame =
- Wx::Frame->new( undef, -1, 'wxPerl', wxDefaultPosition, wxDefaultSize );
- #实例化分割窗口WxSplitterWindow类,并将$frame作为分割窗口的父窗口
- my $spWindow = WxSplitterWindow->new($frame);
- my $firstmenu = Wx::Menu->new();
- $firstmenu->Append( $id[0], "Normal Item" );
- $firstmenu->Append( wxID_EXIT, "&Close\tCtrl+W" );
- my $menubar = Wx::MenuBar->new();
- $menubar->Append( $firstmenu, "&First Menu" );
- #将WxSplitterWindow相关的菜单加进Wx::Frame菜单条,该类中定义有$self->{menu}变量
- $menubar->Append( $spWindow->{menu}[1], $spWindow->{menu}[0] );
- $frame->SetMenuBar($menubar);
- $frame->Show(1);
- $self->SetTopWindow($frame);
- EVT_MENU( $self, $id[0], sub { } );
- EVT_MENU( $self, wxID_EXIT, sub { $self->ExitMainLoop; } );
- return 1;
- }
- package main;
- my $app = MyApp->new;
- $app->MainLoop;
阅读(5388) | 评论(0) | 转发(0) |