28/03/2010


Podemos mover um componente em run time com o click do mouse, basta definir os eventos onMouseDown, onMouseUp e onMouseMove.


Para podermos fazer nosso teste, coloque um TPanel no seu form.

Declare as seguintes variáveis no public:



public
MouseDownSpot : TPoint;
Capturing : bool;
{ Public declarations }
end;
Agora defina os eventos como abaixo:


procedure TForm1.Panel1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
        Capturing := true;
        MouseDownSpot.X := x;
        MouseDownSpot.Y := Y;
end;


procedure TForm1.Panel1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
        if Capturing then
        begin
                panel1.Left:= panel1.Left-(MouseDownSpot.x-x);
                panel1.Top:= panel1.Top - (MouseDownSpot.y-y);
        end;
end;


procedure TForm1.Panel1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
        if Capturing then begin
                Capturing := false;
                Panel1.Left := panel1.Left - (MouseDownSpot.x -x);
                Panel1.Top := panel1.Top - (MouseDownSpot.y - y);
        end;
end;
Agora é só testar os eventos!


[ Fonte ]

0 comentários:

Postar um comentário