Move a border less form in Delphi

We usually use mouse to drag the windows form title bar to move the window on our desktop. But what if we required to move a border less form and that also does not have title bar. So here are some solutions to move a border less form in Delphi.

There is a new way to move the form by just dragging on any point in the form itself. This is ideally suitable for those form that don't have title bar.

For example,

FormStyle := bsNone;

Use the following code to drag on window content and you able to move the form just as you drag on title bar:

type
  TForm1 = class(TForm)
  private
    procedure WMNCHitTest(var Msg: TWMNCHitTest); message WM_NCHitTest;
  end;

procedure TForm1.WMNCHitTest(var Msg: TWMNCHitTest);
begin
  inherited;
  if Msg.Result = htClient then
    Msg.Result := htCaption;
end;

The above code attempt to hijack the mouse event to tell the system to treat the mouse click action on windows client area when user click on windows title bar.

But here is a drawback using using above solution. All form's customize mouse events will no longer function as the mouse event has been hijacked by WM_NCHitTest handler.

And what if there is an image control or panel control on the form with Align=alClient and you still wish to drag on the window to move the form around? The above solution will not work as it will only function if you drag on the empty area in the form. To make the dragging more sensible, write a MouseDown event for the control:

For example...

procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
const
  sc_DragMove = $F012;
begin
  ReleaseCapture;
  Perform( wm_SysCommand, sc_DragMove, 0 );
end;

Comments

Popular posts from this blog

ShellExecute in Delphi

How to send Email in Delphi?

Variants in Delphi. Use of Variant Array. How to check a Variant is unassigned or empty or clear ?