README.md

December 4, 2022 · View on GitHub

Better moving form

Description

While looking at somebody else's code, I noticed he was doing all sorts of stuff with the MouseDown and MouseMove events. Here's a better way, it captures the WM_NCHITTEST message which Windows sends to make Windows think the user is clicking on the titlebar, thus moving the form...

More Info

Submitted On
ByMark van Renswoude
LevelBeginner
User Rating5.0 (15 globes from 3 users)
CompatibilityDelphi 5, Delphi 4, Pre Delphi 4
CategoryCustom Controls/ Forms/ Menus
WorldDelphi
Archive File

Source Code

unit Unit1;

interface

uses
  Windows,
  Messages,
  Forms;

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

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.WMNCHitTest(var Msg: TWMNCHitTest);
begin
  // Call default procedure
  inherited;

  // Modify result to make windows think we're
  // clicking on the titlebar when we're actually
  // clicking on the client area...

  if Msg.Result = HTCLIENT then
    Msg.Result := HTCAPTION;
end;

end.