Steve Cookson wrote:
Hi Guys,
I have a small piece of code which displays a bitmap, then when you click
on the bitmap it should draw a circle round the point you clicked.
It works OK, except that it always returns (0, 0) as the coordinates of
the mouse,
where-ever you click.
Where am I going wrong?
Here is the code.
Thanks in advance.
Regards
Steve
#!/usr/bin/perl -w --
use Wx 0.15 qw[:allclasses];
use strict;
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 = -1 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, $style,
$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:set_your_own_bitmap.jpg',
wxBITMAP_TYPE_ANY, -1 );
$self->{Loc_Photo_Bmp} = Wx::Bitmap->new( $self->{image1} ) ;
$self->{bitmap_1} = my $static_bitmap = Wx::StaticBitmap->new( $self,
-1, $self->{Loc_Photo_Bmp});
$self->{bitmap_1}->SetCursor(wxCROSS_CURSOR);
# Button to draw image
$self->{button_1} = Wx::Button->new($self, -1, "button_1");
use Wx::Event qw(EVT_BUTTON EVT_LEFT_UP EVT_LEFT_DOWN);
EVT_LEFT_DOWN( $self->{bitmap_1}, \&on_button );
EVT_LEFT_UP( $self->{bitmap_1}, \&on_button );
#
# Sizer
#
$self->{sizer_1} = Wx::BoxSizer->new(wxVERTICAL);
$self->{sizer_1}->Add($self->{bitmap_1}, 0, 0, 0);
$self->{sizer_1}->Add($self->{button_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), 10, wxSOLID);
$mdc->SetPen( $pen );
$mdc->SetBrush( wxTRANSPARENT_BRUSH );
# Determine mouse event
You don't need this:
my $m= Wx::MouseEvent->new($event);
my $r = 50;
my $x=$m->GetX();
my $y=$m->GetY();
Replace this with:
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});
}
$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();
}
Untested but if memory serves me right the above patches should work.
Cheers,
Huub