How to drag a borderless FMX form on the screen th

2019-07-31 04:12发布

问题:

I am trying to make a form draggable on the screen, i.e. that I could grab it and move it around the screen. Its transparent and has no borders, however an image serves to be the background for other controls. I want to use the image's events to control dragging of the form. How can I do that?

I have found the DragEnter, DragLeave, DragStart methods which have this TDragObject argument, I don't know about.

Can somebody help?

回答1:

Basically you have to do it manually.

Here's some delphi/windows code from a form with a transparent Image (TransImage) on it, no borders etc The events are in the form for the Image so Top & Left refer to TMainScanForm.Top/Left.

This will drag your form around using the image events to detect the clicks and moves

...

// Mouse Drag Control
MouseDown: Boolean;
TopLeft,
MouseStart: TPoint;

...

procedure TMainScanForm.TransImageMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  MouseDown := (Button = mbLeft);
  if MouseDown then
  begin
    MouseStart.X := X;
    MouseStart.Y := Y;
    TopLeft := ClientToScreen(MouseStart);
    TopLeft.X := TopLeft.X - X;
    TopLeft.Y := TopLeft.Y - Y;
    end;
end;

procedure TMainScanForm.TransImageMouseMove( Sender: TObject;
                                  Shift: TShiftState;
                                  X, Y: Integer);
var
  NewPoint: TPoint;
begin
  if MouseDown  then
  begin
    NewPoint.X := X;
    NewPoint.Y := Y;
    NewPoint := ClientToScreen(NewPoint);    // On Screen
    NewPoint.Y := NewPoint.Y - MouseStart.Y; // New Onscreen
    NewPoint.X := NewPoint.X - MouseStart.X;
    Top := NewPoint.Y;
    Left := NewPoint.X;
    Refresh;
  end;
end;

procedure TMainScanForm.TransImageMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  MouseDown := False;
end;