From: s9510929@babel.ee.up.ac.za (Kevin Newman)
Newsgroups: comp.lang.pascal
Subject: Delphi: Moving a window by draging the client area
Date: Tue, 25 Apr 1995 11:53:27 GMT
Organization: Faculty of Engineering, University of Pretoria

Someone asked a while ago about how to move a window by draging the client
area. I finally got it right: the following unit is self explanatory.

Kevin Newman (s9510929@babel.ee.up.ac.za)
University of Pretoria
South Africa

unit Movedrag;

{ You can move this form by clicking and draging in the client area.
  The caption still operates as usual, but you can move the form via the
  client area even if there is no caption (MainForm.BorderStyle = bsNone).

  Written by Kevin Newman. e-mail: s9510929@babel.ee.up.ac.za }

interface

uses
  Forms, Controls, Classes, Messages, WinProcs, WinTypes, StdCtrls;

type
  TMainForm = class(TForm)
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
    MoveWin: Boolean;
    procedure Moved(var info); message WM_MOVE;
  end;

var
  MainForm: TMainForm;

implementation

{$R *.DFM}

procedure TMainForm.Moved(var info);
begin
  MoveWin := False;
end;

procedure TMainForm.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if Button = mbLeft then
  begin
    MoveWin := True;
    SendMessage(Handle, WM_LButtonUp, 0, 0);
    SendMessage(Handle, WM_NCLButtonDown, HTCAPTION, 0);
  end;
end;

procedure TMainForm.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  if MoveWin then SendMessage(Handle, WM_NCHITTEST, 2, 0);
end;

end.
