I found it hard to locate any easy examples of drawing on Bitmaps using wxPerl. Here is some working code that I put together to see if I could make it work. Just make sure you point the .jpg filename to one that exits on your machine, and you have wxPerl installed. Many thanks to Huub Peters for helping me with Mouse events and understanding DC.
#!/usr/bin/perl -w --
use Wx 0.15 qw[:allclasses];
use strict;
use warnings;
package MyFrame;
use Wx qw[:everything];
use base qw(Wx::Frame);
use strict;
our $gl_self;
sub new {
my( $self, $parent, $id, $title, $pos, $size, $style, $name ) = @_
+;
$parent = undef unless defined $parent;
$id = wxID_ANY unless defined $id;
$title = "" unless defined $title;
$pos = wxDefaultPosition unless defined $pos;
$size = wxDefaultSize unless defined $size;
$name = "" unless defined $name;
$style = wxDEFAULT_FRAME_STYLE unless defined $style;
$self = $self->SUPER::new( $parent, $id, $title, $pos, $size, $sty
+le, $name );
$self->SetTitle("Drawing on image");
$gl_self= $self; # Set Global variable for use in EVT handler
# Image for drawing on
$self->{image1} = Wx::Image->new( 'C:\insert_your_jpeg.jpg', wxBI
+TMAP_TYPE_ANY, -1 );
$self->{Loc_Photo_Bmp} = Wx::Bitmap->new( $self->{image1} ) ;
$self->{bitmap_1} = Wx::StaticBitmap->new( $self, wxID_ANY, $self-
+>{Loc_Photo_Bmp});
# Button to draw image
use Wx::Event qw( EVT_LEFT_UP );
$self->{bitmap_1}->SetCursor(wxCROSS_CURSOR);
$self->{bitmap_1}->Connect( wxID_ANY,wxID_ANY,wxEVT_LEFT_UP, \&on_
+button );
#
# Sizer
#
$self->{sizer_1} = Wx::BoxSizer->new(wxVERTICAL);
$self->{sizer_1}->Add($self->{bitmap_1}, 0, 0, 0);
$self->SetSizer($self->{sizer_1});
$self->{sizer_1}->Fit($self);
$self->Layout();
return $self;
}
sub on_button{
my ($self, $event) = @_;
# select it into a memory dc
if (defined $gl_self->{Loc_Photo_Bmp}){
my $mdc = Wx::MemoryDC->new();
$mdc->SelectObject($gl_self->{Loc_Photo_Bmp});
my $pen = Wx::Pen->new( Wx::Colour->new(255,255,255), 3, wxSOL
+ID);
$mdc->SetPen( $pen );
$mdc->SetBrush( wxTRANSPARENT_BRUSH );
# Determine mouse event
# my $m= Wx::MouseEvent->new($event);
my $r = 50;
my $x=$event->GetX();
my $y=$event->GetY();
# Draw circle round mouse event
$mdc->DrawCircle( $x, $y, $r );
$mdc->SelectObject(wxNullBitmap); # deselect the bitmap out of
+ the DC
$gl_self->{bitmap_1} ->SetBitmap($gl_self->{Loc_Photo_Bmp});
$gl_self->{bitmap_1}->SetCursor(wxSTANDARD_CURSOR);
$gl_self->{bitmap_1}->Disconnect( wxID_ANY,wxID_ANY,wxEVT_LEFT
+_DOWN );
$gl_self->{bitmap_1}->Disconnect( wxID_ANY,wxID_ANY,wxEVT_LEFT
+_UP );
}
$event->Skip() ;
return $self;
}
1;
package main;
unless(caller){
local *Wx::App::OnInit = sub{1};
my $app = Wx::App->new();
Wx::InitAllImageHandlers();
my $frame_1 = MyFrame->new();
$app->SetTopWindow($frame_1);
$frame_1->Show(1);
$app->MainLoop();
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.