How can I have a TPanel where the user can click and drag it to move it around the desktop?
(so I can hide the border menu bar)
Easiest way would be to set-up OnMouse-Events, for your wanting you need 3 different,
OnMouseDown: to activate the dragging
OnMouseMove: to actual drag the form
OnMouseUp: to release the drag
In theory it should cross-compile for any target
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes , SysUtils , Forms , Controls , Graphics , Dialogs , ExtCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
Panel1: TPanel;
procedure FormCreate(Sender: TObject);
procedure Panel1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X , Y: Integer);
procedure Panel1MouseMove(Sender: TObject; Shift: TShiftState; X ,
Y: Integer);
procedure Panel1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X , Y: Integer);
private
FDragging: Boolean;
FLastX,
FLastY: Integer;
public
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
FDragging := False;
end;
procedure TForm1.Panel1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X , Y: Integer);
begin
FDragging := True;
FLastX := X;
FLastY := Y;
end;
procedure TForm1.Panel1MouseMove(Sender: TObject; Shift: TShiftState; X ,
Y: Integer);
begin
if FDragging then
begin
Self.Left := Self.Left + X - FLastX;
Self.Top := Self.Top + Y - FLastY;
end;
end;
procedure TForm1.Panel1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X , Y: Integer);
begin
FDragging := False;
end;
end.
To keep form inside visible screen area, if such behavior is wanted, you can try this:
procedure TForm1.Panel1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X , Y: Integer);
var
LMon: TMonitor;
begin
FDragging := False;
// none of this has the taskbar included
LMon := Screen.MonitorFromPoint(Mouse.CursorPos, mdNearest);
// fix if window go outside of visible screen area / left side
if (Self.Left < LMon.Left) then
Self.Left := LMon.Left;
// fix if window go outside of visible screen area / right side
if ((Self.Left + Self.Width) > LMon.Width) then
Self.Left := LMon.Width - Self.Width;
// fix if window go outside of visible screen area / top side
if (Self.Top < LMon.Top) then
Self.Top := LMon.Top;
// fix if window go outside of visible screen area / bottom side
if ((Self.Top + Self.Height) > LMon.Height) then
Self.Top := LMon.Height - Self.Height;
end;
Let me know if it works on MacOS same as it works on Windows where I've tested it.