我想使用 wxPerl 在带有平铺位图的框架上设置背景。在wxWidgets的示例的帮助下,我提出了以下代码。不幸的是,它什么也没做,框架保持空白。这甚至是正确的方法,还是有另一种方法?
use warnings;
use strict;
package MyFrame;
use Wx qw(:everything);
use base qw( Wx::Frame );
sub new {
my ( $class, $path ) = @_;
my $self
= $class->SUPER::new( undef, -1, 'Test', [ -1, -1 ], [ 600, 400 ], );
$self->set_tiling_background($path);
return $self;
}
sub set_tiling_background {
my ( $self, $path ) = @_;
## Create a wxBitmap from the file
my $file = IO::File->new($path);
binmode $file;
my $handler = Wx::BMPHandler->new();
my $image = Wx::Image->new();
$handler->LoadFile( $image, $file );
my $bitmap = Wx::Bitmap->new($image);
## Just to check that the bitmap is good.
$bitmap->SaveFile('saved.bmp', wxBITMAP_TYPE_BMP);
## Draw the bitmap tiling over the frame
## https://github.com/wxWidgets/wxWidgets/blob/master/src/html/htmlwin.cpp
my $dc = Wx::WindowDC->new($self);
my $size_x = $bitmap->GetWidth;
my $size_y = $bitmap->GetHeight;
for ( my $x = 0; $x < 600; $x += $size_x ) {
for ( my $y = 0; $y < 400; $y += $size_y ) {
$dc->DrawBitmap( $bitmap, $x, $y, 0 );
}
}
}
package MyApp;
use base 'Wx::App';
my $path = '/path/to/bitmap.bmp';
sub OnInit {
my ($self) = @_;
my $frame = MyFrame->new($path);
$frame->Show(1);
}
package main;
MyApp->new->MainLoop;