用鼠标移动 TRectangle (FMX, Win32)
move TRectangle with mouse (FMX, Win32)
我有一个 FMX 表单,上面有一个与客户端对齐的 TLayout。在 TLayout 上我有一个 TRectangle。我可以在按钮单击事件中使用以下代码轻松移动 TRectangle:
Rectangle1->Position->X = Rectangle1->Position->X + 10;
有没有一种干净的方法可以让我用鼠标来做这个(移动矩形)?比如单击矩形并将其移动到新位置?我只是在玩弄一个小绘图程序来学习....
使用 C++Builder 10.2 版本 25.0.29899.2631 并在 Win32 中构建。
更新:我采用了 Hans 的方法并最终使其运行良好。我在下面添加了完整的代码作为答案。耶!
拖动组件的一种方法是存储鼠标按下时鼠标位置与控件位置之间的偏移量,然后使用此偏移量计算鼠标移动事件中控件的位置。
在半伪代码中,它看起来像这样:
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;
这是在 Win32 中左键单击并移动 FMX 窗体上的 TRectangle 所需的完整代码(尚未在移动设备上尝试过)。只需创建一个新的 FireMonkey 多设备应用程序并在其上放置一个 TRectangle 和一个 TButton。
添加到表单 class 声明的代码(在 class TForm1 : public TForm {
下的 .h 文件中):
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.
添加到按钮的 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.
感谢 Hans 指导开始!
此外,我注意到在其他控件之间移动时拖动不流畅。如果这让您感到困扰,那么您需要将其他控件设置为 "HitTest" false,以便它们忽略它。如果您想在移动鼠标和矩形时查看所有 TPointF 坐标,请添加 TEdit 框 - 在尝试计算坐标时帮助很多。
我有一个 FMX 表单,上面有一个与客户端对齐的 TLayout。在 TLayout 上我有一个 TRectangle。我可以在按钮单击事件中使用以下代码轻松移动 TRectangle:
Rectangle1->Position->X = Rectangle1->Position->X + 10;
有没有一种干净的方法可以让我用鼠标来做这个(移动矩形)?比如单击矩形并将其移动到新位置?我只是在玩弄一个小绘图程序来学习....
使用 C++Builder 10.2 版本 25.0.29899.2631 并在 Win32 中构建。
更新:我采用了 Hans 的方法并最终使其运行良好。我在下面添加了完整的代码作为答案。耶!
拖动组件的一种方法是存储鼠标按下时鼠标位置与控件位置之间的偏移量,然后使用此偏移量计算鼠标移动事件中控件的位置。
在半伪代码中,它看起来像这样:
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;
这是在 Win32 中左键单击并移动 FMX 窗体上的 TRectangle 所需的完整代码(尚未在移动设备上尝试过)。只需创建一个新的 FireMonkey 多设备应用程序并在其上放置一个 TRectangle 和一个 TButton。
添加到表单 class 声明的代码(在 class TForm1 : public TForm {
下的 .h 文件中):
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.
添加到按钮的 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.
感谢 Hans 指导开始!
此外,我注意到在其他控件之间移动时拖动不流畅。如果这让您感到困扰,那么您需要将其他控件设置为 "HitTest" false,以便它们忽略它。如果您想在移动鼠标和矩形时查看所有 TPointF 坐标,请添加 TEdit 框 - 在尝试计算坐标时帮助很多。