下面是wxPerl的例子中的一个,从中可以开出它的基本结构(我有一些改动)
#!/usr/bin/perl
#############################################################################
## Name: samples/hello/hello.pl
## Purpose: Hello wxPerl sample
## Author: Mattia Barbon
## Modified by:
## Created: 02/11/2000
## RCS-ID: $Id: hello.pl,v 1.3 2004/10/19 20:28:14 mbarbon Exp $
## Copyright: (c) 2000 Mattia Barbon
## Licence: This program is free software; you can redistribute it and/or
## modify it under the same terms as Perl itself
#############################################################################
use strict;
use Wx; #导入Wx
package MyApp;
#每一个wxPerl的程序都要有一个类继承Wx::App
use base qw(Wx::App);
# OnInit继承自Wx::App,这个方法在对象创建的时候会自动运行
sub OnInit {
my( $this ) = shift;
# 创建一个新的frame并设定为 top(最顶?)的frame
my( $frame ) = MyFrame->new();
$this->SetTopWindow( $frame );
# 显示这个frame
$frame->Show( 1 );
}
#下面定义这个frame
package MyFrame;
#use vars qw(@ISA);
#our @ISA = qw(Wx::Frame);
use base qw(Wx::Frame);
use Wx::Event qw(EVT_PAINT);
# this imports some constants
use Wx qw(wxDECORATIVE wxNORMAL wxBOLD);
use Wx qw(wxDefaultPosition);
use Wx qw(wxWHITE);
sub new {
# new frame with no parent, id -1, title 'Hello, world!'
# default position and size 350, 100
my( $this ) = shift->SUPER::new( undef, -1, 'Hello, world!',
wxDefaultPosition , [350, 100] );
# create a new font object and store it
$this->{FONT} = Wx::Font->new( 40, wxDECORATIVE, wxNORMAL, wxBOLD, 0 );
# set background colour
$this->SetBackgroundColour( wxWHITE );
$this->SetIcon( Wx::GetWxPerlIcon() );
# declare that all paint events will be handled with the OnPaint method
EVT_PAINT( $this, /&OnPaint );
return $this;
}
sub OnPaint {
my( $this, $event ) = @_;
# create a device context (DC) used for drawing
my( $dc ) = Wx::PaintDC->new( $this );
# select the font
$dc->SetFont( $this->font );
# darw a friendly message
$dc->DrawText( 'Hello, world!', 10, 10 );
}
sub font {
$_[0]->{FONT};
}
#主程序 创建一个MyApp对象
package main;
# create an instance of the Wx::App-derived class
my( $app ) = MyApp->new();
# start processing events
$app->MainLoop;