...move components at Runtime?
|
Autor:
Thomas Stutz |
[ Print tip
] | | |
{
This example shows how to move components at runtime, dragging it.
You are able to see the components move as you drag them.
Attach the Control event handlers (ControlMouseDown,...)
to all components you want to move.
Dieses Beispiel zeigt, wie man Komponenten zur Laufzeit
verschieben kann. Während dem Verschieben werden die
Komponenten voll angezeigt.
An alle Komponenten, welche verschoben werden sollen,
die Eregnisse ControlMouseDown,ControlMouseMove und
ControlMouseUp zuweisen.
}
type
TForm1 = class(TForm)
Image1: TImage;
procedure ControlMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure ControlMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure ControlMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
{ Private declarations }
FDownX,
FDownY: Integer;
FDragging: Boolean;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
type
TMoveCracker = class(TControl);
procedure TForm1.ControlMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
FDownX := X;
FDownY := Y;
FDragging := True;
TMoveCracker(Sender).MouseCapture := True;
end;
procedure TForm1.ControlMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if FDragging then
with Sender as TControl do
begin
Left := X - FDownX + Left;
Top := Y - FDownY + Top;
end;
end;
procedure TForm1.ControlMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if FDragging then
begin
FDragging := False;
TMoveCracker(Sender).MouseCapture := False;
end;
end;
|