Board index » delphi » Help on mouse drag

Help on mouse drag

Help on mousemove in Delphi3 --

Im trying to figure how to read the mousemoves while a mousebutton is
pressed. I wan to drag a Tshape on my form with the mouse. I've tried
ondrag, onmove etc but cant get it to work. Thanks in advance.

/Jimmy, j...@mbox300.swipnet.se

 

Re:Help on mouse drag


Quote
Jimmy wrote:

> Help on mousemove in Delphi3 --

> Im trying to figure how to read the mousemoves while a mousebutton is
> pressed. I wan to drag a Tshape on my form with the mouse. I've tried
> ondrag, onmove etc but cant get it to work. Thanks in advance.

> /Jimmy, j...@mbox300.swipnet.se

Hi Jimmy,

try this:

var
  IsMouseDown: Boolean;
  a, b: Integer;

procedure TForm1.FormCreate(Sender: TObject);
begin
  IsMouseDown := False;
end;

procedure TForm1.Shape1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if Button = mbLeft then  //optional
  begin  
    IsMouseDown := True;
    a := x;
    b := y;
  end;
end;

procedure TForm1.Shape1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  if IsMouseDown then
  begin
    Shape1.Left := Shape1.Left - a + x;
    Shape1.Top := Shape1.Top - b + y;
  end;
end;

procedure TForm1.Shape1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  IsMouseDown := False;
end;

Good luck!

Kerstin

Other Threads