如何在delphi中用鼠标移动圆? [英] How to move circle with mouse in delphi?
本文介绍了如何在delphi中用鼠标移动圆?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在delphi中用鼠标移动圆圈?
How to move circle with mouse in delphi?
circle:Shape;
推荐答案
请务必使用 ClientToScreen
和 ScreenToClient
.
以下过程将控件的中心移至其客户坐标中的点(X,Y):
The following procedure moves the center of a Control to the point (X,Y) in it's client coordinates:
procedure MoveControl(AControl: TControl; const X, Y: Integer);
var
lPoint: TPoint;
begin
lPoint := AControl.Parent.ScreenToClient(AControl.ClientToScreen(Point(X, Y)));
AControl.Left := lPoint.X - AControl.Width div 2;
AControl.Top := lPoint.Y - AControl.Height div 2;
end;
现在要在单击TShape时移动它,您必须提供以下MouseMove事件处理程序:
Now to move your TShape when when it is clicked, you have to provide the following MouseMove event handler:
procedure TForm1.ShapeToMoveMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
if ssLeft in Shift then // only move it when Left-click is down
MoveControl(Sender as TControl, X, Y);
end;
要进行测试,请使用以下代码在表单中添加一个按钮:
And to test it, drop a button in your Form with this code:
procedure TForm1.ButtonTestClick(Sender: TObject);
begin
with TShape.Create(nil) do
begin
Name := Format('ShapeToMove%d',[Self.ControlCount + 1]);
Parent := Self; // Parent will free it
Shape := stCircle;
Width := 65;
Height := 65;
OnMouseMove := ShapeToMoveMouseMove;
end;
end;
现在,这是一个简单的示例,但这应该可以帮助您入门.
为了娱乐,只需使用此MouseMove事件处理程序钩住其他控件即可::-)
Now, that's a minimalist example, but it should get you started.
For fun, just hook other controls with this MouseMove event handler... :-)
这篇关于如何在delphi中用鼠标移动圆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文