怎样在delphi中实现控件的拖拽

下面这2种方法都能实现对控件窗体拖拽

 

 

方法1

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

  Shift: TShiftState; X, Y: Integer);

begin

  ReleaseCapture;

  SendMessage(Handle,WM_NCLBUTTONDOWN,HTCAPTION,0);

end;

 

 
方法2

  public
    { public declarations }
    posX, posY: Integer;

 

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

  Shift: TShiftState; X, Y: Integer);

begin

  posX := Mouse.CursorPos.X - Form1.Left;

  posY := Mouse.CursorPos.Y - Form1.Top;

end;

 

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState;

  X, Y: Integer);

begin

  if Shift =[ssLeft] then

  begin

    Form1.Left := Mouse.CursorPos.X - posX; // 新的位置

    Form1.Top := Mouse.CursorPos.Y - posY;

  end;

end;

 

你可能感兴趣的:(Delphi)