delphi中如何用鼠标移动圆圈?

发布于 2024-08-23 16:11:37 字数 64 浏览 4 评论 0原文

delphi中如何用鼠标移动圆圈?

circle:Shape;

How to move circle with mouse in delphi?

circle:Shape;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风苍溪 2024-08-30 16:11:37

请务必使用 ClientToScreenScreenToClient 将从 Control 上的 MouseMove 获取的鼠标 X、Y 客户端坐标转换为父级客户端。

以下过程将 Control 的中心移动到其客户端坐标中的点 (X,Y):

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 时移动 TShape,您必须提供以下 MouseMove 事件处理程序:

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;

要测试它,请放置一个使用以下代码在表单中添加按钮:

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 事件处理程序挂钩其他控件...:-)

Be sure to convert the Mouse X,Y client coordinates that you get from MouseMove on your Control to the Parent's client using ClientToScreen and ScreenToClient.

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;

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;

Now, that's a minimalist example, but it should get you started.
For fun, just hook other controls with this MouseMove event handler... :-)

抚你发端 2024-08-30 16:11:37

如果您访问我的网页,您可以找到一些示例(全部包含代码),它们可以帮助您解决这个问题。
“带有图形和平面图的视觉作品示例”;使用两个组件来直观地管理、移动、调整大小和保存元素;一种用于选择、移动、调整大小……(TSeleccOnRuntime),另一种(TSaveComps)用于保存状态(位置、大小……)。
替代文本 http://neftali-mirror.site11.com/images/imagen_ej_restaurante.png

直观地选择形状;用于解释选择视觉形状和图像的两种模式的示例。

在运行时(如 IDE)上创建、移动和响应控件; TSeleccOnRuntime 组件的另一个示例。模拟和IDE。

替代文本 http://neftali-mirror.site11.com/images/image_ej_form_designer.png

最后是另一个在运行时创建/销毁组件并用鼠标移动示例;该样品是在没有任何组件的情况下制作的。示例中的所有代码。

替代文字 http://neftali-mirror.site11.com/images/imagen_ej_mover_mouse.png

我希望对你有用。
问候

PD:请原谅我的英语不好。

If you go to my webpage, you can Find some samples (all with code included) thah can help you about this question.
"Sample for visual work with figures and plans"; Use two components for manage, move, resize and save elements visually; One for selection, movement, resize,... (TSeleccOnRuntime) and other (TSaveComps) for save the state (position, size,...).
alt text http://neftali-mirror.site11.com/images/imagen_ej_restaurante.png

Select Shapes Visually; Sample for explain two modes for select visually shapes and images.

Create, move and resive controls on Runtime (like IDE); Another sample of TSeleccOnRuntime component. Simulate and IDE.

alt text http://neftali-mirror.site11.com/images/image_ej_form_designer.png

And finally another sample for Create/destroy components in runtime and move with mouse; This sample is made without components. All code at the sample.

alt text http://neftali-mirror.site11.com/images/imagen_ej_mover_mouse.png

I hope that is usefull for you.
Regards

P.D: Excuse for my bad english.

夜雨飘雪 2024-08-30 16:11:37

好吧,我没有太多可说的,但是让某些东西移动来跟随鼠标通常是这样工作的:

在某处有一个“IsFollowingMouse”标志。当您应该跟随鼠标时将其打开。
在窗体的 MouseMove 事件上,执行如下操作:

procedure TMyForm.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  if FIsFollowingMouse then
  begin
    myCircle.left := x + fShapeOffsetX;
    myCircle.top := y + fShapeOffsetY;
  end;
end;

偏移量是您使用的变量,用于给出鼠标指针位置与 TShape 左上角位置之间的差异。

Well, I don't have too much to go on, but having something move to follow the mouse generally works like this:

Have a "IsFollowingMouse" flag somewhere. Turn it on when you should be following the mouse.
On the form's MouseMove event, do something like this:

procedure TMyForm.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  if FIsFollowingMouse then
  begin
    myCircle.left := x + fShapeOffsetX;
    myCircle.top := y + fShapeOffsetY;
  end;
end;

The offsets are variables you use that gives the difference between the location of the mouse pointer and the top-left corner of the TShape.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文