Reputation: 33
I have a thumbnail of a picture. I have written code to redraw this image in Delphi as soon as the user clicks this button. However the requirement is user can click the thumbnail and can click anywhere in the form to create the image.
For instance lets say I have a thumbnail of a circle image, now user shall click this thumbnail and click somewhere in the form and the circle should appear.
For this I came to know we need to use
TForm1.FormMouseDown(Sender: TObject;Button: TMouseButton; Shift: TShiftState;X, Y: Integer) ;
I didn't get how to send X,Y coordinates to this? Ex:
procedure TMDIChild.FormMouseDown(Sender: TObject;Button: TMouseButton; Shift: TShiftState;X, Y: Integer);
begin
Canvas.Ellipse(x-20,y-20,x+20,y+20) ;
end;
Should draw an ellipse(circle) when the left click button is clicked somewhere in the form after clicking the thumbnail. But x,y should be current mouse pointer and how do I get the current mouse pointer after the user has clicked the thumbnail?
I really appreciate your help.
Thanks, Giridhar.
Upvotes: 2
Views: 1725
Reputation: 441
You may try
Mouse.CursorPos.x and Mouse.CursorPos.y
and if you would like the change the origin of coords (screen or form) you should use ScreenToClient()
or ClientToScreen()
.
Upvotes: 2
Reputation: 612794
I don't fully understand the problem but I can see one thing wrong. You can't expect to paint to the form canvas in a mouse down event and hope that what you draw will remain. Painting in Windows simply does not work that way. You need to paint the form in response to a WM_PAINT
message.
There is no persistent drawing surface associated with a window. When a window needs to be painted the system posts a WM_PAINT
message to your message queue. You are then obliged to paint the current state of the window.
The simplest way to achieve that in your case will be to paint to an off screen bitmap in response to mouse messages and then display that bitmap as part of the paint cycle. You can access the paint cycle by putting a TPaintBox
on your form and handling the OnPaint
event. In that event simply draw the bitmap to the paint box canvas.
You'll need to make the paint box repaint itself whenever you update the bitmap. Do this by calling the Invalidate
method of the paint box.
I recommend reading Petzold's book Programming Windows to get a thorough understanding of how painting works in Windows.
Upvotes: 0
Reputation: 68862
Drop a paintbox onto your form, and write some code in the paintbox control OnPaint event like this:
procedure TForm1.PaintBox1Paint(Sender: TObject);
var
pb:TPaintBox;
h,w:Integer;
begin
pb := TPaintBox(Sender);
h := pb.Height;
w := pb.Width;
pb.Canvas.Ellipse( 10, 10, w-10, h-10 );
end;
There is no way to draw from FormMouseDown directly. So add some code in FormMouseDown to capture the co-ordinates, store them in a variable, and then invalidate your paintbox control (Paintbox1.Invalidate) and the ellipse will get drawn by the paintbox control. (You can put a paintbox on top of another control, if you need to, for instance).
Upvotes: 0