标签:
private //窗体移动; OldPos,NewPos:TPoint; bMove:Boolean; procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin bMove:=True; OldPos:=ClientToScreen(Point(X,Y)); end; procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); begin if not bMove then Exit; NewPos:=ClientToScreen(Point(X,Y)); Left:=Left+NewPos.X-OldPos.X; Top:=Top+NewPos.Y-OldPos.Y; OldPos:=NewPos; end; procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin bMove:=False; end;
拓展:以此让一个控件可移动;
1 //变量 2 isMouseDown : boolean ; //是否拖拽 3 posX,posY : Integer; //拖动开始时候的坐标 4 5 procedure TForm1.edt2MouseDown(Sender: TObject; Button: TMouseButton; 6 Shift: TShiftState; X, Y: Integer); 7 begin 8 isMouseDown := True; //告诉窗体我要拖了 9 posX := Mouse.CursorPos.X - edt2.Left ; //记录开始的位置 10 posY := Mouse.CursorPos.Y - edt2.Top ; // 11 end; 12 13 procedure TForm1.edt2MouseMove(Sender: TObject; Shift: TShiftState; X, 14 Y: Integer); 15 begin 16 if isMouseDown then //鼠标是按下状态(要拖) 17 begin 18 edt2.Left := Mouse.CursorPos.X - posX ; //新的位置 19 edt2.Top := Mouse.CursorPos.Y - posY ; 20 end; 21 end; 22 23 procedure TForm1.edt2MouseUp(Sender: TObject; Button: TMouseButton; 24 Shift: TShiftState; X, Y: Integer); 25 begin 26 isMouseDown := False; //告诉窗体我拖完了 27 end;
实现窗体随着鼠标移动(控件)--《用delphi开发共享软件》-15.1任务管理器
标签:
原文地址:http://www.cnblogs.com/rogge7/p/4460787.html