2

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.

4
  • Sorry I forgot to mention about the platform: Its windows using Delphi. Commented Dec 8, 2011 at 17:41
  • You seem to be understanding things incorrectly. You don't call FormMouseDown; it's an event that is automatically called (through Windows message processing) when the user clicks the mouse button. There should never be a need to call it yourself. Commented Dec 8, 2011 at 18:17
  • procedure TMDIChild.FormMouseDown(Sender: TObject; Button: TMouseButton;Shift: TShiftState; X, Y: Integer); begin Canvas.MoveTo(X, Y);{ set pen position } end; procedure TMDIChild.FormMouseUp(Sender: TObject; Button: TMouseButton;Shift: TShiftState; X, Y: Integer); begin Canvas.LineTo(X, Y);{ draw line from PenPos to (X, Y) } end; Is my understanding right? If these two procedures combine then it should draw a line at the mouse clicked position? Commented Dec 9, 2011 at 20:22
  • You missed what I said. I didn't say don't use OnMouseDown - I said you don't call it yourself. But no, the code in your last comment is not how to do what you're asking to do. Read Warren and David's answers for the proper way to draw the line (in the OnPaint event and nowhere else). Commented Dec 9, 2011 at 20:27

3 Answers 3

2

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().

Sign up to request clarification or add additional context in comments.

2 Comments

Mouse.CursorPos is the mouse position now. The coords passed to the event handler are the coords when the mouse went down. So Mouse.CursorPos is wrong here. The coords passed to the event handler are client coords.
I am not getting the mouse cursor. I tried using it and it is drawing the figure at center of the form. I read something like this in delphi developers guide which uses the code procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin Canvas.TextOut(X, Y, 'Here!');{ write text at (X, Y) } end; But even this is not responding to the mouse event if I click on the form.
0

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).

2 Comments

Hi Warren, I am using canvas graphics with Delphi. Just I need to capture the current mouse co-ordinates and pass it to another procedure. How to capture the current mouse co-ordinates?
You are totally confused it seems. The X,Y parameters in the MouseDown event are exactly the values you want. Yes. You said that's what you want, and then you said how do I get them? Well, when the mouse gets clicked down, take those X,Y values and STORE THEM IN A VARIABLE SOMEWHERE. If the mouse position is needed at some time other than when you press the mouse button, look at the MouseMove event instead of MouseDown.
0

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.

8 Comments

Hi David, yes that is why I am using canvas graphics tool in Delphi. Now I just want the current mouse position wherever the user clicks on the form.
This comment makes little sense to me. You can't paint to form canvas in a mouse down event. If you want the coords then X and Y do the job. Your question was not "what are the current X and Y coords". When you say current what do you mean? The coords when the mouse went down, or the coords when the event is handled. When the mouse went down is what you should be asking for and those coords are the X and Y parameters to the mouse down event. In Sender client coords.
Current position means where the mouse went down. First the user will click on the thumbnail and triggers the event. Now user can move his mouse anywhere in the form and click anywhere. I want this co-ordinates where he/she clicks to be passed into my drawing function. So that the image can be drawn at that point where he/she clicked.
X and Y parameters of mouse event is what you need.
Yes David, I just need those x,y parameters
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.