再看看下面一段程序吧:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls;
type
TForm1 = class(TForm)
Timer1: TTimer;
procedure Timer1Timer(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Timer1Timer(Sender: TObject);
var
NewLeft, NewTop: Integer;
begin
Randomize;
NewLeft := Random(20);
Randomize;
NewTop := Random(20);
if (NewLeft mod 2)=0 then
Self.Left := Self.Left+NewLeft
else
Self.Left := Self.Left-NewLeft;
if (NewTop mod 2)=0 then
Self.Top := Self.Top+NewTop
else
Self.Top := Self.Top-NewTop;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Timer1.Interval := 1000;
Timer1.Enabled := True;
end;
end.