Firemonkey 用鼠标移动树形角(FMX,Win32)
我有一个FMX表格,上面有一个与客户对齐的TLayout。在特拉扬我有一个三角。我可以在按钮单击事件中使用以下代码轻松移动树形角:Firemonkey 用鼠标移动树形角(FMX,Win32),firemonkey,c++builder,Firemonkey,C++builder,我有一个FMX表格,上面有一个与客户对齐的TLayout。在特拉扬我有一个三角。我可以在按钮单击事件中使用以下代码轻松移动树形角: Rectangle1->Position->X = Rectangle1->Position->X + 10; 有没有一个干净的方法可以让我用鼠标来做这个(移动矩形)?比如单击矩形并将其移动到新位置?我只是想做一个小的绘图程序来学习 使用C++Builder 10.2版本25.0.29899.2631并在Win32中构建 更新:我采用了H
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表单上移动三角体所需的完整代码(尚未在mobile上尝试过)。只需创建一个新的FireMonkey多设备应用程序,并在其上放置一个树形角和一个T按钮 要添加到表单类声明中的代码(在
类TForm1:public TForm{
下的.h文件中):
要添加到矩形的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;
}
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.
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.
要添加到矩形的Rectangle1MouseMove
事件的代码(也添加到表单的FormMouseMove
中,或者有时快速拖动会丢失矩形):
要添加到Rectangle1MouseUp
事件的代码:
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;
}
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.
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.
要添加到按钮按钮1的代码单击事件:
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;
}
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.
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框-这在尝试计算坐标时会有很大帮助。我还在表单的OnMouseUp
事件中添加了fMouseIsDown=false
。稍微少一点疯狂。好的开始!!!更新-这段代码在手机上确实有效(在iOS上测试)。此外,如果要在运行时添加矩形。