1、通过控件,适合局部区域移动
procedure TForm1.Button1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
Windows.ReleaseCapture;
Perform(WM_SYSCOMMAND, SC_MOVE + 1, 0);
end;
2、通过处理窗口消息,适合整体窗口移动
procedure TTestForm.WMNCHitTest(var Msg: TWMNCHitTest);
function CanDoDrag(const APos: TPoint): Boolean;
begin
Result := (APos.Y < 52);
end;
var
LScreenPos: TPoint;
LPos: TPoint;
begin
DefaultHandler(Msg);
if Msg.Result = HTCLIENT then
begin
LScreenPos.X := Msg.XPos;
LScreenPos.Y := Msg.YPos;
LPos := ScreenToClient(LScreenPos);
if CanDoDrag(LPos) then
Msg.Result := HTCAPTION;
end;
end;