I have this code from the internet to drag a borderless form by holding the Left mouse button down:
procedure TForm6.Image1MouseDown(Sender: TObject; Button: TMouseButton;Shift: TShiftState;X,Y: Integer);
const
SC_DRAGMOVE = $F012;
begin
if Button = mbLeft then
begin
ReleaseCapture;
Perform(WM_SYSCOMMAND, SC_DRAGMOVE, 0);
end;
end;
It works fine but I need to drag by Right mouse button. Which parameter must be changed for this?
How to move window by right mouse button using C++? has a solution which handles the dragging itself, instead of letting Windows do it. Projecting that work from MFC needs one to know what Delphi's Forms already handle, instead of overly calling WinApi functions.
One major issue is to incorporate a window's caption height, which can rely on multiple factors. In my example I used a normal one for a sizable window and it works as expected using Windows 7 without any theme (looks like Windows 95). Having no caption, or having a tool window, or having no border, or having a window which can't be sized needs the calls to
GetSystemMetrics()
adjusted.I incorporated both: dragging by left mouse button and by right mouse button. Although I encourage still displaying a potential context menu at the end of the dragging (like the Explorer does so for dragging files), because it's still a right mouse button and every user expects a popup menu for that click.
My example also works for both: bound to either a
TWinControl
or to theTForm
itself.Note that initiating the dragging is bound to the control's
OnMouseDown
event, but handling and ending the dragging must be bound to the form's events: