保证你在任何地方或控件上都可以拖动窗口implementation{$R *.dfm} var dx,dy:integer; started:boolean=false;procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean); begin if msg.message=wm_lbuttondown then begin dx:=mouse.CursorPos.x-form1.Left; dy:=mouse.CursorPos.Y-form1.Top; started:=true; end; if (msg.message=wm_mousemove) and (started) then begin form1.Left:=mouse.CursorPos.X-dx; form1.Top:=mouse.CursorPos.Y-dy; end; if msg.message=wm_lbuttonup then begin started:=false; end; end;end.
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;type
TForm1 = class(TForm)
procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
Const
SC_DragMove = $F012 ;
begin
ReleaseCapture;
PERFORM(WM_SysCommand,SC_DragMove ,0)
end;end.
Msg.Result:=htCaption;
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;type
TForm1 = class(TForm)
private
{ Private declarations }
procedure HitTest(var Msg: TWmNcHitTest);message wm_NcHitTest; public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.HitTest(var Msg: TWmNcHitTest) ;
begin
if (Msg.XPos > 20) and (Msg.YPos > 20) then
Msg.Result := HtCaption ;
end ;end.
CurrentPos:TPoint; //记录当前位置
DownPos:TPoint; procedure TForm1.titleimgMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
OriginalPos.x := Form1.Left;
OriginalPos.y := Form1.Top;
DownPos.x := X;
DownPos.y := Y;
DownPos := ClientToScreen(DownPos);
if Button = mbLeft then
MouseLButtonDown := true;
end;procedure TForm1.titleimgMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if MouseLButtonDown = true then
begin
CurrentPos.x := X;
CurrentPos.y := Y;
CurrentPos := ClientToScreen(CurrentPos);
Form1.Left := OriginalPos.x + (CurrentPos.x - DownPos.x);
Form1.Top := OriginalPos.y + (CurrentPos.y - DownPos.y);
end;
end;procedure TForm1.titleimgMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
MouseLButtonDown:=false;
end;
var
dx,dy:integer;
started:boolean=false;procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;
var Handled: Boolean);
begin
if msg.message=wm_lbuttondown then
begin
dx:=mouse.CursorPos.x-form1.Left;
dy:=mouse.CursorPos.Y-form1.Top;
started:=true;
end;
if (msg.message=wm_mousemove) and (started) then
begin
form1.Left:=mouse.CursorPos.X-dx;
form1.Top:=mouse.CursorPos.Y-dy;
end; if msg.message=wm_lbuttonup then
begin
started:=false;
end;
end;end.