【问题标题】:move TRectangle with mouse (FMX, Win32)用鼠标移动 TRectangle (FMX, Win32)
【发布时间】:2019-05-20 14:55:01
【问题描述】:

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

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

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

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

更新:我采用了 Hans 的方法并最终使它工作得很好。我在下面添加了完整的代码作为答案。耶!

【问题讨论】:

  • 我还在表单的OnMouseUp 事件中添加了fMouseIsDown = false。少一点的牛肉干。好的开始!!!

标签: firemonkey c++builder


【解决方案1】:

这是在 Win32 中左键单击并在 FMX 表单上移动 TRectangle 所需的完整代码(尚未在移动设备上尝试过)。只需创建一个新的 FireMonkey 多设备应用程序并在其上放置一个 TRectangle 和一个 TButton。

要添加到表单的类声明的代码(在 .h 文件中,就在 class TForm1 : public TForm { 下):

bool fMouseIsDown; // gets set to TRUE when left mouse click on the rectangle
TPointF fMousePosGlobal; // this is the mouses position relative to the screen
TPointF fMousePosForm; // this is the mouse pos relative to the form
TPointF captionOffset; // this is a small offset necessary since the form's TOP and LEFT are outside of form's client area due to caption bar and left edge of form
TPointF fMouseInRectAtClick; // this is the mouse pos relative to the rectangle (top left of rectangle is 0,0)

要添加到矩形的Rectangle1MouseDown 事件的代码:

if (Button == 0) {  // 0 for left mb, 1 for right mb
fMouseIsDown = true;  
fMouseInRectAtClick.X = X;  //mouse pos with respect to rectangle at time of click
fMouseInRectAtClick.Y = Y;
}

添加到矩形的Rectangle1MouseMove 事件的代码(也添加到表单的FormMouseMove 或者有时您会在快速拖动时丢失矩形):

fMousePosGlobal = Screen->MousePos(); //mouse global pos
fMousePosForm.X = fMousePosGlobal.X - Form1->Left;  // mouse pos relative to the form
fMousePosForm.Y = fMousePosGlobal.Y - Form1->Top;
if (fMouseIsDown) {
Form1->Rectangle1->Position->X =  fMousePosForm.X - captionOffset.X - fMouseInRectAtClick.X;
Form1->Rectangle1->Position->Y = fMousePosForm.Y - captionOffset.Y - fMouseInRectAtClick.Y;
}

要添加到Rectangle1MouseUp 事件的代码:

fMouseIsDown = false;  // add this to the form's MouseUp too in case you "lose" the rectangle on a drag.  That only happened when i forget to set the offsets.

要添加到 Button 的 Button1Click 事件的代码:

captionOffset.X = 8.0; // this accounts for the width of the form left edge
captionOffset.Y = 30.0; // this accounts for the height of the form caption
// if you don't add this your "drag point" on the rectangle will jump as soon as you start the drag.

感谢汉斯的指导!

另外,我注意到在其他控件上移动时拖动并不顺畅。如果这让您感到困扰,那么您需要将那些其他控件“HitTest”设置为 false,以便他们忽略它。如果您想在移动鼠标和矩形时查看所有 TPointF 坐标,请添加 TEdit 框 - 在尝试计算坐标时会有所帮助。

【讨论】:

  • 更新 - 此代码在移动设备上运行(在 iOS 上测试)。另外,如果你想在运行时添加矩形check this out.
【解决方案2】:

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

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

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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-09
    • 2013-11-06
    • 2013-07-04
    • 1970-01-01
    • 2011-04-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多