delphi 无标题窗体的拖动

在实际工作中,需要把窗体的BorderStyle设置为bsNone,但这样窗体却无法拖动了.那只好自己写代码实现:

private

{Private declarations}

OldX,OldY:integer; //定义变量

procedure TForm1.FormMouseDown(Sender:TObject;Button:TMouseButton;

Shift:TShiftState;X,Y:Integer);

begin

OldX := x;

OldY := y;

end;

procedure TForm1.FormMouseMove(Sender:TObject;Button:TMouseButton

Shift:TShiftState;X, Y:Integer);

begin

if ssleft in shift then //按下鼠标左键

begin

Form1.Left := Form1.Left+x-Oldx;

Form1.Top := Form1.Top+y-Oldy;

end;

end;

但后来发现还有更好的方法,那就是重写WMHitTest消息事件:

procedure WMHitTest(var Msg: TWMNCHitTest); message WM_NCHITTEST;

procedure TForm1.WMHitTest(var Msg: TWMNCHitTest);

var

pt:TPoint;

begin

inherited;

pt:=Point(Msg.xPos,Msg.yPos);

pt:=ScreenToClient(pt);

if (Msg.result = HTCLIENT) and (pt.Y <= height) then Msg.Result := HTCAPTION;

end;