如何在运行时移动由鼠标控制的TPanel?

发布于 2025-01-16 23:08:51 字数 278 浏览 0 评论 0原文

我想在运行时使用 Delphi 10.4.2 和 FMX 通过鼠标将 TPanel 移动到另一个 TPanel 上。我尝试了 OnMouseDownOnMouseMoveOnMouseUp 事件。但尚不清楚事件中XY值的内容是什么。文档说它们是屏幕坐标。相对于屏幕、窗体、父控件还是控件本身?如何解决TPanel的移动问题?

I want to move a TPanel on another TPanel by mouse at runtime with Delphi 10.4.2 and FMX. I tried OnMouseDown, OnMouseMove and OnMouseUp events. But it is not clear what the contents of X and Y values are in the events. The documentation says that they are screen coordinates. Relative to the screen, form, parent control or the control itself? How can I solve the movement of the TPanel?

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

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

发布评论

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

评论(1

难得心□动 2025-01-23 23:08:51

文档说它们是屏幕坐标。

不,事实并非如此。 FMX.Types.TMouseEventFMX.Types.TMouseMoveEvent
文档都说:

X 和 Y - 鼠标指针在控件工作区内的像素坐标。

如何解决TPanel的移动问题?

像这样:

var
  LastPt: TPointF;
  Dragging: Boolean = False;

procedure TMyForm.PanelToDragMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Single);
begin
  if Button = TMouseButton.mbLeft then
  begin
    LastPt := TPointF.Create(X, Y);
    Dragging := True;
  end;
end;

procedure TMyForm.PanelToDragMouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Single);
begin
  if Button = TMouseButton.mbLeft then
    Dragging := False;
end;

procedure TMyForm.PanelToDragMouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Single);
var
  CurrPt: TPointF;
begin
  if Dragging then
  begin
    CurrPt := TPointF.Create(X, Y);
    PanelToDrag.Position.Point := PanelToDrag.Position.Point + (CurrPt - LastPt);
    LastPt := CurrPt;
  end;
end;

基本上,当鼠标在面板上移动时,代码只是计算鼠标从最后一个已知位置移动到当前位置的偏移量,然后将该偏移量应用到面板的当前位置 code> 在其 Parent 中。

The documentation says that they are screen coordinates.

No, it doesn't. The FMX.Types.TMouseEvent and FMX.Types.TMouseMoveEvent
documentation both say:

X and Y--the pixel coordinates of the mouse pointer within the client area of the control.

How can I solve the movement of the TPanel?

Like this:

var
  LastPt: TPointF;
  Dragging: Boolean = False;

procedure TMyForm.PanelToDragMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Single);
begin
  if Button = TMouseButton.mbLeft then
  begin
    LastPt := TPointF.Create(X, Y);
    Dragging := True;
  end;
end;

procedure TMyForm.PanelToDragMouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Single);
begin
  if Button = TMouseButton.mbLeft then
    Dragging := False;
end;

procedure TMyForm.PanelToDragMouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Single);
var
  CurrPt: TPointF;
begin
  if Dragging then
  begin
    CurrPt := TPointF.Create(X, Y);
    PanelToDrag.Position.Point := PanelToDrag.Position.Point + (CurrPt - LastPt);
    LastPt := CurrPt;
  end;
end;

Basically, while the mouse is moving around the Panel, the code is simply calculating the offset the mouse has moved from the last known position to the current position, and then applying that offset to the Panel's current Position within its Parent.

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