Recent

Author Topic: Click a TPanel to drag/move form around desktop  (Read 1102 times)

tearsfornations

  • New Member
  • *
  • Posts: 35
  • Jesus is worth talking about.
    • Lightning Bolt Quiz - How many Lightning Bolts do you deserve?
Click a TPanel to drag/move form around desktop
« on: April 19, 2023, 11:40:21 pm »
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)
-
Used C++Builder for the longest time, got macOS, How many Lightning Bolts do you deserve? Find out at my website: https://boltquiz.org

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Click a TPanel to drag/move form around desktop
« Reply #1 on: April 20, 2023, 01:49:06 am »
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

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes , SysUtils , Forms , Controls , Graphics , Dialogs , ExtCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Panel1: TPanel;
  16.     procedure FormCreate(Sender: TObject);
  17.     procedure Panel1MouseDown(Sender: TObject; Button: TMouseButton;
  18.       Shift: TShiftState; X , Y: Integer);
  19.     procedure Panel1MouseMove(Sender: TObject; Shift: TShiftState; X ,
  20.       Y: Integer);
  21.     procedure Panel1MouseUp(Sender: TObject; Button: TMouseButton;
  22.       Shift: TShiftState; X , Y: Integer);
  23.   private
  24.     FDragging: Boolean;
  25.     FLastX,
  26.     FLastY: Integer;
  27.   public
  28.  
  29.   end;
  30.  
  31. var
  32.   Form1: TForm1;
  33.  
  34. implementation
  35.  
  36. {$R *.lfm}
  37.  
  38. { TForm1 }
  39.  
  40. procedure TForm1.FormCreate(Sender: TObject);
  41. begin
  42.   FDragging := False;
  43. end;
  44.  
  45. procedure TForm1.Panel1MouseDown(Sender: TObject; Button: TMouseButton;
  46.   Shift: TShiftState; X , Y: Integer);
  47. begin
  48.   FDragging := True;
  49.   FLastX    := X;
  50.   FLastY    := Y;
  51. end;
  52.  
  53. procedure TForm1.Panel1MouseMove(Sender: TObject; Shift: TShiftState; X ,
  54.   Y: Integer);
  55. begin
  56.   if FDragging then
  57.     begin
  58.       Self.Left := Self.Left + X - FLastX;
  59.       Self.Top  := Self.Top + Y - FLastY;
  60.     end;
  61. end;
  62.  
  63. procedure TForm1.Panel1MouseUp(Sender: TObject; Button: TMouseButton;
  64.   Shift: TShiftState; X , Y: Integer);
  65. begin
  66.   FDragging := False;
  67. end;
  68.  
  69. end.

To keep form inside visible screen area, if such behavior is wanted, you can try this:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Panel1MouseUp(Sender: TObject; Button: TMouseButton;
  2.   Shift: TShiftState; X , Y: Integer);
  3. var
  4.   LMon: TMonitor;
  5. begin
  6.   FDragging := False;
  7.   // none of this has the taskbar included
  8.   LMon := Screen.MonitorFromPoint(Mouse.CursorPos, mdNearest);
  9.   // fix if window go outside of visible screen area / left side
  10.   if (Self.Left < LMon.Left) then
  11.     Self.Left := LMon.Left;
  12.   // fix if window go outside of visible screen area / right side
  13.   if ((Self.Left + Self.Width) > LMon.Width) then
  14.     Self.Left := LMon.Width - Self.Width;
  15.   // fix if window go outside of visible screen area / top side
  16.   if (Self.Top < LMon.Top) then
  17.     Self.Top := LMon.Top;
  18.   // fix if window go outside of visible screen area / bottom side
  19.   if ((Self.Top + Self.Height) > LMon.Height) then
  20.     Self.Top := LMon.Height - Self.Height;
  21. end;
Let me know if it works on MacOS same as it works on Windows where I've tested it.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

tearsfornations

  • New Member
  • *
  • Posts: 35
  • Jesus is worth talking about.
    • Lightning Bolt Quiz - How many Lightning Bolts do you deserve?
Re: Click a TPanel to drag/move form around desktop
« Reply #2 on: April 20, 2023, 05:14:23 am »
it works good. I didn't test the screen area part.
Thank you!
-
Used C++Builder for the longest time, got macOS, How many Lightning Bolts do you deserve? Find out at my website: https://boltquiz.org

 

TinyPortal © 2005-2018