📄 dynamiccreate.pas
字号:
unit DynamicCreate;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls, DBTables, Grids;
type
TForm1 = class(TForm)
MemResults: TMemo;
Panel1: TPanel;
BtnCreate: TButton;
CkbSelfAsOwner: TCheckBox;
RGpCreate: TRadioGroup;
procedure BtnCreateClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
var
GlobalOwnedComponentCount,
GlobalComponentCount: DWord;
procedure TForm1.BtnCreateClick(Sender: TObject);
const
INT_NumComponentsToCreate = 1000;
var
msgStr: string;
localOwner: TComponent;
j: integer;
startTickCount,
totalCreationTicks: DWord;
begin
if CkbSelfAsOwner.Checked then
begin
msgStr := Format('Creating %d owned %s components...', [INT_NumComponentsToCreate, '%s']);
localOwner := Self;
end
else
begin
msgStr := Format('Creating %d %s components (with no owner)...', [INT_NumComponentsToCreate, '%s']);
localOwner := nil; // These components will not be freed. Don't do this in real code!
// This is only for the purposes of testing creation times. See the
// online paper at http://www.eagle-software.com/DynamicCreation.htm
// for examples of how to properly create and free a component at
// runtime.
end;
case RGpCreate.ItemIndex of { }
0: // TButton
begin
MemResults.Lines.Add(Format(msgStr, ['TButton']));
startTickCount := GetTickCount;
for j := 0 to INT_NumComponentsToCreate - 1 do { Create components... }
TButton.Create(localOwner);
end;
1: // TLabel
begin
MemResults.Lines.Add(Format(msgStr, ['TLabel']));
startTickCount := GetTickCount;
for j := 0 to INT_NumComponentsToCreate - 1 do { Create components... }
TLabel.Create(localOwner);
end;
2: // TSession
begin
MemResults.Lines.Add(Format(msgStr, ['TSession']));
startTickCount := GetTickCount;
for j := 0 to INT_NumComponentsToCreate - 1 do { Create components... }
TSession.Create(localOwner);
end;
3: // TStringGrid
begin
MemResults.Lines.Add(Format(msgStr, ['TStringGrid']));
startTickCount := GetTickCount;
for j := 0 to INT_NumComponentsToCreate - 1 do { Create components... }
TStringGrid.Create(localOwner);
end;
end; { case }
totalCreationTicks := GetTickCount - startTickCount;
GlobalComponentCount := GlobalComponentCount + INT_NumComponentsToCreate;
if CkbSelfAsOwner.Checked then
GlobalOwnedComponentCount := GlobalOwnedComponentCount + INT_NumComponentsToCreate;
MemResults.Lines.Add(Format('Created Components: %d', [GlobalComponentCount]));
MemResults.Lines.Add(Format('Owned Components: %d', [GlobalOwnedComponentCount]));
MemResults.Lines.Add(Format('Creation Time: %d milliseconds', [totalCreationTicks]));
MemResults.Lines.Add('____________________');
MemResults.Lines.Add('');
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ShowMessage('Warning: This application may create components without freeing them.');
end;
initialization
GlobalOwnedComponentCount := 0;
GlobalComponentCount := 0;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -