Ak chceme dat do pohybu komponentu napr. TButton tak musime menit vlastnosti Left a Top tejto komponenty. To znamena, ze ak chcem posunut tlacitko na poziciu 100,100 v nejakom formulari napisem nieco ako:
begin
//....
Button1.Left := 100;
Button1.Top := 100;
//....
end;
Utekajuce tlacitko:
Mala ukazka poukaze ako napr. urobit nedostihnutelne tlacitko.
1.Do formulara vlozte komponentu TButton
2.Do vlastnosti name tlacitka napiste BtMouse
3.Do formulara vlozte komponentu TTimer
4.Vlastnost Interval komponenty Timer nastavte na 100
5.Vytvorte udalost OnMouseMove ku komponente TButton a napiste do nej:
var
P: TPoint;
begin
GetCursorPos(P);
P := ScreenToClient(P);
BtMouse.Left := P.X + 50;
end;
6.Vytvorte udalost OnTimer ku komponente TTimer a napiste do nej:
begin
BtMouse.Left := BtMouse.Left + 3;
if BtMouse.Left > Form1.ClientWidth then
BtMouse.Left := -1 * BtMouse.Width;
end;
A hotovo...
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,StrUtils, ExtCtrls;
type
TForm1 = class(TForm)
BtMouse: TButton;
Label1: TLabel;
Timer1: TTimer;
procedure BtMouseMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.BtMouseMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
var
P: TPoint;
begin
GetCursorPos(P);
P := ScreenToClient(P);
BtMouse.Left := P.X + 50;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
BtMouse.Left := BtMouse.Left + 3;
if BtMouse.Left > Form1.ClientWidth then
BtMouse.Left := -1 * BtMouse.Width;
end;
end.
Komentáre