Chinaunix首页 | 论坛 | 博客
  • 博客访问: 225750
  • 博文数量: 36
  • 博客积分: 1188
  • 博客等级: 军士长
  • 技术积分: 802
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-08 21:45
文章分类

全部博文(36)

文章存档

2020年(1)

2017年(2)

2015年(1)

2014年(1)

2013年(1)

2012年(3)

2011年(27)

分类: WINDOWS

2011-04-05 10:24:07

    今天查了好久的资料,终于把分割窗口给整出来了。Wx::perl学习的资料还真是少啊,拿出来跟大家分享下,方便学习Wx::perl的朋友。
  1. use Wx;

  2. package WxSplitterWindow;
  3. use base qw(Wx::SplitterWindow Class::Accessor::Fast);

  4. use Wx qw(:splitterwindow wxDefaultPosition wxDefaultSize);
  5. use Wx::Event qw(EVT_SPLITTER_SASH_POS_CHANGED EVT_SPLITTER_SASH_POS_CHANGING
  6.   EVT_SPLITTER_UNSPLIT EVT_MENU);

  7. __PACKAGE__->mk_accessors(qw(first_window second_window));

  8. sub new {
  9.     my ( $class, $parent ) = @_;
  10.     my $self =
  11.       $class->SUPER::new( $parent, -1, wxDefaultPosition, wxDefaultSize,
  12.         wxSP_3D | wxSP_LIVE_UPDATE );
  13.     my $top = Wx::GetTopLevelParent($parent);
  14.     #EVT_SPLITTER_UNSPLIT( $self, $self, \&split_horizontally);

  15.     my $filemenu = Wx::Menu->new;
  16.     EVT_MENU(
  17.         $top,
  18.         $filemenu->Append( -1, "Split vertically" ),
  19.         sub { $self->split_vertically }
  20.     );
  21.     EVT_MENU(
  22.         $top,
  23.         $filemenu->Append( -1, "Split horizontally" ),
  24.         sub { $self->split_horizontally }
  25.     );
  26.     $filemenu->AppendSeparator;
  27.     EVT_MENU( $top, $filemenu->Append( -1, "Unsplit" ),
  28.         sub { $self->unsplit } );

  29.     $self->first_window( Wx::TextCtrl->new( $self, -1, 'First pane' ) );
  30.     $self->second_window( Wx::TextCtrl->new( $self, -1, 'Second pane' ) );

  31.     #$self->Initialize( $self->first_window );
  32.     #一开始就显示为分割的上下两窗口
  33.     $self->split_horizontally;
  34.     $self->{menu} = [ '&Splitter', $filemenu ];

  35.     return $self;
  36. }

  37. sub split_horizontally {
  38.     my ( $self, $event ) = @_;

  39.     $self->Unsplit if $self->IsSplit;
  40.     $self->first_window->Show;
  41.     $self->second_window->Show;
  42.     $self->SplitHorizontally( $self->first_window, $self->second_window );
  43. }

  44. sub split_vertically {
  45.     my ( $self, $event ) = @_;

  46.     $self->Unsplit if $self->IsSplit;
  47.     $self->first_window->Show;
  48.     $self->second_window->Show;
  49.     $self->SplitVertically( $self->first_window, $self->second_window );
  50. }

  51. sub unsplit {
  52.     my ( $self, $event ) = @_;

  53.     $self->Unsplit if $self->IsSplit;
  54. }

  55. package MyApp;
  56. use base 'Wx::App';
  57. use Wx qw(:sizer wxDefaultPosition wxDefaultSize wxID_EXIT);
  58. use Wx::Event qw(EVT_MENU);

  59. our @id = ( 0 .. 5 ); # IDs array

  60. sub OnInit {
  61.     my $self = shift;
  62.     my $frame =
  63.       Wx::Frame->new( undef, -1, 'wxPerl', wxDefaultPosition, wxDefaultSize );

  64.     #实例化分割窗口WxSplitterWindow类,并将$frame作为分割窗口的父窗口
  65.     my $spWindow = WxSplitterWindow->new($frame);

  66.     my $firstmenu = Wx::Menu->new();
  67.     $firstmenu->Append( $id[0], "Normal Item" );
  68.     $firstmenu->Append( wxID_EXIT, "&Close\tCtrl+W" );

  69.     my $menubar = Wx::MenuBar->new();
  70.     $menubar->Append( $firstmenu, "&First Menu" );

  71.     #将WxSplitterWindow相关的菜单加进Wx::Frame菜单条,该类中定义有$self->{menu}变量
  72.     $menubar->Append( $spWindow->{menu}[1], $spWindow->{menu}[0] );
  73.     $frame->SetMenuBar($menubar);
  74.     $frame->Show(1);
  75.     $self->SetTopWindow($frame);

  76.     EVT_MENU( $self, $id[0], sub { } );
  77.     EVT_MENU( $self, wxID_EXIT, sub { $self->ExitMainLoop; } );
  78.     return 1;
  79. }

  80. package main;

  81. my $app = MyApp->new;
  82. $app->MainLoop;
阅读(5388) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~