一 窗体设计
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 397
ClientWidth = 530
Color = clNone
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
OnDestroy = FormDestroy
PixelsPerInch = 96
TextHeight = 13
object Image1: TImage
Left = 2
Top = 2
Width = 7
Height = 9
Transparent = True
end
object Timer1: TTimer
OnTimer = Timer1Timer
Left = 270
Top = 226
end
end
二 实现代码
unit Unit1;interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TDot=record
Pos:TPoint;
Vel:TPoint;
end;
TForm1 = class(TForm)
Timer1: TTimer;
Image1: TImage;
procedure FormCreate(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
Dots:array[0..10] of TDot;
offScreen:TBitmap;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
iCount:integer;
begin
offScreen :=TBitmap.Create;
offScreen.Width :=Form1.ClientWidth;
offScreen.Height :=Form1.ClientHeight;
for iCount := 0 to 9 do
begin
Dots[iCount].Pos.X :=Random(ClientWidth);
Dots[iCount].Pos.Y :=Random(ClientHeight);
if Random(2)=0 then
Dots[iCount].Vel.X :=-1
else
Dots[iCount].Vel.X :=1;
if Random(2)=0 then
Dots[iCount].Vel.Y :=-1
else
Dots[iCount].Vel.Y :=1;
end;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
offScreen.Free;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
iCount:integer;
begin
offScreen.Canvas.Brush.Color :=clBlack;
offScreen.Canvas.FillRect(offScreen.Canvas.ClipRect);
for iCount := 0 to 9 do
begin
Dots[iCount].Pos.X :=Dots[iCount].Pos.X+Dots[iCount].Vel.X;
Dots[iCount].Pos.Y :=Dots[iCount].Pos.Y+Dots[iCount].Vel.Y;
if (Dots[iCount].Pos.X<0) or (Dots[iCount].Pos.X>ClientWidth) then
Dots[iCount].Pos.X :=0-Dots[iCount].Vel.X;
if (Dots[iCount].Pos.Y<0) or (Dots[iCount].Pos.Y>ClientHeight) then
Dots[iCount].Pos.Y :=0-Dots[iCount].Vel.Y;
offScreen.Canvas.Pixels[Dots[iCount].Pos.X,Dots[iCount].Pos.Y] :=clRed;
offScreen.Canvas.Pixels[Dots[iCount].Pos.X+1,Dots[iCount].Pos.Y] :=clRed;
offScreen.Canvas.Pixels[Dots[iCount].Pos.X,Dots[iCount].Pos.Y+1] :=clRed;
offScreen.Canvas.Pixels[Dots[iCount].Pos.X+1,Dots[iCount].Pos.Y+1] :=clRed;
end;
ExcludeClipRect(Canvas.Handle,Image1.Left,Image1.Top,Image1.Left+Image1.Width,Image1.Top+Image1.Height);
Canvas.Draw(0,0,offScreen);
end;
end.