如果您只是创建 20 TLabel
,添加 anApplication.Processmessages
不是一个好主意,因为它会重新绘制它以立即显示新添加的组件...
使用常规应用程序,生成 2000 只需要不到 1 秒的时间TLabel
……如果我设置自定义样式,则需要更长的时间……也许,这是你的问题……
因此,为了快速生成一代,我这样做了:
uses ..., System.StrUtils, System.Diagnostics, Vcl.Themes, Vcl.Styles;
procedure TForm3.GenerateLabels(bNoStyle: Boolean);
var
iLoop, iMax: Integer;
aLabel : TLabel;
pointLeft : Integer;
pointTop : Integer;
timeSpent : TStopwatch;
begin
iMax := 2000;
pointLeft := 3;
pointTop := 3;
timeSpent := TStopwatch.StartNew; // how long does it take
if bNoStyle then
TStyleManager.TrySetStyle('Windows'); // = no style
for iLoop := 1 to iMax do
begin
Application.ProcessMessages;
// creating a labels in loop
aLabel:=TLabel.Create(Self);
labels.Add(aLabel); // TList for managing.
with aLabel do
begin
Left := pointLeft;
Top := pointTop;
Caption := 'title';
parent := scrlbx1; //TScrollBox
end;
pointTop := pointTop + 20;
end;
if bNoStyle then
TStyleManager.TrySetStyle('Carbon'); // previous style
labels[0].Caption := IfThen(bNoStyle, 'No style', 'Style');
labels[1].Caption := IntToStr(timeSpent.ElapsedMilliseconds) + 'ms'; // display the spent time
labels[2].Caption := IntToStr(labels.Count);
timeSpent.Stop;
end;