用鼠标(FMX,Win32)移动TRectangle [英] move TRectangle with mouse (FMX, Win32)

查看:93
本文介绍了用鼠标(FMX,Win32)移动TRectangle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个FMX表单,上面有一个与客户端对齐的TLayout。在TLayout上,我有一个TRectangle。我可以在按钮单击事件中使用以下代码轻松移动TRectangle:

  Rectangle1-> Position-> X =矩形1->位置-> X + 10; 

我是否有一种干净的方法可以用鼠标执行此操作(移动矩形)?就像单击矩形并将其移动到新位置一样?我只是在玩弄一个小的绘图程序来学习...。



使用C ++ Builder 10.2版本25.0.29899.2631并在Win32中构建。 / p>

更新:我采用了Hans的方法,最终使其运行良好。我在下面添加了完整的代码作为答案。没错!

解决方案

拖动组件的一种方法是存储鼠标位置与鼠标向下的控件位置之间的偏移量,然后使用此偏移量来计算控件在鼠标移动事件中的位置。



在半伪代码中,它看起来像这样:

 将以下内容添加到您的TForm类中:

fMouseIsDown:布尔值;
fMouseDownOffset:TPointF;

过程OnRectangleMouseDown(X,Y)
开始
fMouseIsDown:= true;
fMouseDownOffset:= PointF(Rectangle.Position.X-X,Rectangle.Position.Y-Y)
结尾;

过程OnRectangleMouseMove(X,Y)
如果fMouseIsDown开始
然后
开始
Rectangle.Position.X:= X + fMouseDownOffset.X;
Rectangle.Position.Y:= Y + fMouseDownOffset.Y;
结尾;
结尾;

过程OnRectangleMouseUp(X,Y);
开始
fMouseIsDown:= false;
结尾;


I have an FMX form with a TLayout on it aligned to client. On the TLayout I have a TRectangle. I can move the TRectangle easily with the following code in a button click event:

 Rectangle1->Position->X = Rectangle1->Position->X + 10;

Is there a clean way for me to do this (move the rectangle) with the mouse? Like click on the Rectangle and move it around to a new location? I'm just playing around trying to make a little drawing program to learn....

Using C++Builder 10.2 Version 25.0.29899.2631 and building in Win32.

UPDATE: I took Hans approach and ended up making it work nicely. I've added the full code as an answer below. Yay!

解决方案

A way to drag components is to store the offset between the mouse position and the control position on mouse down, then use this offset to calculate the position of the control in the mouse move event.

In semi-pseudo code it would look like this:

Add the following to your TForm class:

fMouseIsDown: boolean;
fMouseDownOffset: TPointF;

procedure OnRectangleMouseDown(X,Y)
begin
  fMouseIsDown := true;
  fMouseDownOffset := PointF(Rectangle.Position.X-X, Rectangle.Position.Y-Y)
end;

procedure OnRectangleMouseMove(X,Y)
begin
  if fMouseIsDown then
  begin
    Rectangle.Position.X := X+fMouseDownOffset.X;
    Rectangle.Position.Y := Y+fMouseDownOffset.Y;
  end;
end;

procedure OnRectangleMouseUp(X,Y);
begin
  fMouseIsDown := false;
end;

这篇关于用鼠标(FMX,Win32)移动TRectangle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆