The document discusses using Perl to build graphical user interfaces, including using the WxWidgets and WxPerl modules to create windows and menus, handling events, and integrating with other technologies like ActiveX controls and the .NET Common Language Runtime. It provides code examples for setting up a basic WxPerl application framework and handling events from a menu bar. The document also lists several references for more information on Perl GUI programming.
5. Класс приложения use Wx; # every program must have a Wx::App-derive class package MyApp; use strict; use warnings; our @ISA=qw(Wx::App); # this is called automatically on object creation sub OnInit { my( $this ) = @_; # create new MyFrame my $frame = MyFrame->new( "Minimal wxPerl app", [ 50, 50 ] , #position [ 450, 350 ] #size ); # set it as top window (so the app will automatically close when # the last top window is closed) $this->SetTopWindow( $frame ); # show the frame $frame->Show( 1 ); 1; }
6. Класс окна (1) package MyFrame; use strict; use warnings; our @ISA=qw(Wx::Frame); use Wx::Event qw(EVT_MENU); use Wx qw(wxBITMAP_TYPE_ICO wxMENU_TEAROFF); # Parameters: title, position, size sub new { my $class = shift; my $ self = $class->SUPER::new( undef, -1, $_[0], $_[1], $_[2] ); # load an icon and set it as frame icon $ self ->SetIcon( Wx::GetWxPerlIcon() ); # create the menus my $mfile = Wx::Menu->new( undef, wxMENU_TEAROFF ); my $mhelp = Wx::Menu->new(); my( $ID_ABOUT, $ID_EXIT ) = ( 1, 2 ); $mhelp->Append( $ID_ABOUT, "&About...Ctrl-A", "Show about dialog" ); $mfile->Append( $ID_EXIT, "E&xitAlt-X", "Quit this program" );
7. Класс окна (2) my $mbar = Wx::MenuBar->new(); $mbar->Append( $mfile, "&File" ); $mbar->Append( $mhelp, "&Help" ); $ self ->SetMenuBar( $mbar ); # declare that events coming from menu items with the given # id will be handled by these routines EVT_MENU( $ self , $ID_EXIT, amp;OnQuit ); EVT_MENU( $ self , $ID_ABOUT, amp;OnAbout ); # create a status bar (note that the status bar that gets created # has three panes, see the OnCreateStatusBar callback below $ self ->CreateStatusBar( 1 ); # and show a message $ self ->SetStatusText( "Welcome to wxPerl!", 1 ); $ self ; }
8. Обработчики # this is an addition to demonstrate virtual callbacks... # it ignores all parameters and creates a status bar with three fields sub OnCreateStatusBar { my $ self = shift; my $status = Wx::StatusBar->new( $ self , -1 ); $status->SetFieldsCount( 2 ); $status; } # called when the user selects the 'Exit' menu item sub OnQuit { my( $ self , $event ) = @_; # closes the frame $ self ->Close( 1 ); } use Wx qw(wxOK wxICON_INFORMATION wxVERSION_STRING); # called when the user selects the 'About' menu item sub OnAbout { my( $ self , $event ) = @_; # display a simple about box Wx::MessageBox( "This is the about dialog of minimal sample." . "Welcome to wxPerl " . $Wx::VERSION . "" . wxVERSION_STRING, "About minimal", wxOK | wxICON_INFORMATION, $ self ); }
9. Запуск приложения package main; # create an instance of the Wx::App-derived class my $app = MyApp->new(); # start processing events $app->MainLoop();