在Delphi中调用FormMouseDown
我有一张图片的缩略图。我已经编写了代码,一旦用户单击此按钮,就会在 Delphi 中重新绘制此图像。但是,要求用户可以单击缩略图,并且可以单击表单中的任意位置来创建图像。
例如,假设我有一个圆形图像的缩略图,现在用户应单击该缩略图,然后单击表单中的某个位置,则应该出现圆形。
为此我知道我们需要使用
TForm1.FormMouseDown(Sender: TObject;Button: TMouseButton; Shift: TShiftState;X, Y: Integer) ;
我不知道如何将 X,Y 坐标发送到此? 例如:
procedure TMDIChild.FormMouseDown(Sender: TObject;Button: TMouseButton; Shift: TShiftState;X, Y: Integer);
begin
Canvas.Ellipse(x-20,y-20,x+20,y+20) ;
end;
单击缩略图后,在表单中的某处单击左键单击按钮时,应绘制一个椭圆(圆形)。 但是 x,y 应该是当前鼠标指针,用户单击缩略图后如何获取当前鼠标指针?
我真的很感谢你的帮助。
谢谢, 吉里达尔。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以尝试
,如果您想更改坐标(屏幕或表单)的原点,您应该使用
ScreenToClient()
或ClientToScreen()
。You may try
and if you would like the change the origin of coords (screen or form) you should use
ScreenToClient()
orClientToScreen()
.将一个油漆盒拖放到窗体上,然后在油漆盒控件 OnPaint 事件中编写一些代码,如下所示:
无法直接从 FormMouseDown 进行绘制。因此,在 FormMouseDown 中添加一些代码来捕获坐标,将它们存储在变量中,然后使您的油漆盒控件 (Paintbox1.Invalidate) 无效,并且椭圆将由油漆盒控件绘制。 (例如,如果需要,您可以将颜料盒放在另一个控件的顶部)。
Drop a paintbox onto your form, and write some code in the paintbox control OnPaint event like this:
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).
我不完全理解这个问题,但我可以看到一件事是错误的。您不能指望在鼠标按下事件中在表单画布上进行绘制,并希望您绘制的内容会保留下来。在 Windows 中绘画根本就不是这样的。您需要绘制表单以响应
WM_PAINT
消息。没有与窗口关联的持久绘图表面。当需要绘制窗口时,系统会将
WM_PAINT
消息发送到消息队列。然后您必须绘制窗口的当前状态。在您的情况下,实现此目的的最简单方法是响应鼠标消息绘制到离屏位图,然后在绘制周期中显示该位图。您可以通过在窗体上放置
TPaintBox
并处理OnPaint
事件来访问绘制周期。在这种情况下,只需将位图绘制到油漆盒画布上即可。每当更新位图时,您都需要使绘画框重新绘画。通过调用绘画框的
Invalidate
方法来完成此操作。我建议阅读 Petzold 的书Programming Windows,以全面了解 Windows 中绘画的工作原理。
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 theOnPaint
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.