本例效果圖(用 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:TempDelphiX.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.
窗體文件:
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClIEntHeight = 250
ClIEntWidth = 384
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
DesignSize = (
384
250)
PixelsPerInch = 96
TextHeight = 13
object DXDraw1: TDXDraw
Left = 8
Top = 8
Width = 265
Height = 137
AutoInitialize = True
AutoSize = True
Color = clBlack
Display.FixedBitCount = False
Display.FixedRatio = True
Display.FixedSize = True
Options = [doAllowReboot, doWaitVBlank, doCenter, do3D, doDirectX7Mode, doHardware, DOSelectDriver]
SurfaceHeight = 137
SurfaceWidth = 265
TabOrder = 0
Traces = <>
end
object Button1: TButton
Left = 321
Top = 217
Width = 24
Height = 25
Anchors = [akRight, akBottom]
Caption = 'Button1'
TabOrder = 1
OnClick = Button1Click
ExplicitLeft = 344
ExplicitTop = 232
end
object Button2: TButton
Left = 351
Top = 217
Width = 25
Height = 25
Anchors = [akRight, akBottom]
Caption = 'Button2'
TabOrder = 2
OnClick = Button2Click
ExplicitLeft = 374
ExplicitTop = 232
end
object DXImageList1: TDXImageList
Items.ColorTable = {
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000}
Items = <>
Left = 40
Top = 152
end
object DXTimer1: TDXTimer
ActiveOnly = True
Enabled = True
Interval = 1000
OnTimer = DXTimer1Timer
Left = 8
Top = 152
end
end