本例效果图(用 gif 动画演示, 无论如何也出不来 DirectX 的流畅效果):
代码文件:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DXDraws, StdCtrls, DXClass;
type
TForm1 = class(TForm)
DXDraw1: TDXDraw;
DXImageList1: TDXImageList;
DXTimer1: TDXTimer;
Button1: TButton;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure DXTimer1Timer(Sender: TObject; LagCount: Integer);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
var
arr: array of record X,Y,a,b: Integer; end;
PicItem: TPictureCollectionItem;
procedure TForm1.FormCreate(Sender: TObject);
const
ImgPath1 = 'C:\Temp\DelphiX.bmp';
begin
DXDraw1.Align := alClient;
DXImageList1.DXDraw := DXDraw1;
DXImageList1.Items.Add;
PicItem := DXImageList1.Items[DXImageList1.Items.Count-1];
PicItem.Picture.LoadFromFile(ImgPath1);
SetLength(arr, 1);
arr[0].X := Random(DXDraw1.Width - PicItem.Width);
arr[0].Y := Random(DXDraw1.Height - PicItem.Height);
arr[0].a := 1;
arr[0].b := 1;
Button1.Caption := '-';
Button2.Caption := '+';
DXTimer1.Interval := 10;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if Length(arr) > 1 then SetLength(arr, Length(arr)-1);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
SetLength(arr, Length(arr)+1);
arr[High(arr)].X := Random(DXDraw1.Width - PicItem.Width);
arr[High(arr)].Y := Random(DXDraw1.Height - PicItem.Height);
arr[High(arr)].a := 1;
arr[High(arr)].b := 1;
end;
procedure TForm1.DXTimer1Timer(Sender: TObject; LagCount: Integer);
var
i: Integer;
begin
DXDraw1.Surface.Fill(0);
for i := 0 to Length(arr) - 1 do
begin
if arr[i].X > DXDraw1.Width - PicItem.Width then arr[i].a := -1;
if arr[i].Y > DXDraw1.Height - PicItem.Height then arr[i].b := -1;
if arr[i].X = 0 then arr[i].a := 1;
if arr[i].Y = 0 then arr[i].b := 1;
Inc(arr[i].X, arr[i].a);
Inc(arr[i].Y, arr[i].b);
PicItem.Draw(DXDraw1.Surface, arr[i].X, arr[i].Y, 0);
end;
DXDraw1.Flip;
end;
end.