2012-05-02 40 views
1

在我的应用程序中,我使用了TLabel对象运行时。创建并缓慢表示大量的TLabel运行时

这里有一个问题:表示标签20需要大量的时间(约1-2秒)。

DoubleBuffered在父组件上没有帮助。 Application.ProcessMessages只允许观看创建的过程,而不是看frosen窗口。

// creating a labels in loop 
aLabel:=TLabel.Create(scrlbx); 
labels.Add(aLabel); // TList for managing. 
with aLabel do begin 
Left:=pointLeft; 
    Top:=pointTop; 
    Caption:='title'; 
    parent:=scrlbx; //TScrollBox 
end; 
pointTop:=pointTop+20; 

把父母分配给另一个循环之后做了一些效果,但没有解决问题。

for I := 0 to labels.Count-1 do begin 
    TLabel(labels[i]).Parent:= scrlbx; 
end; 

禁用和启用TScrollBox.Visible前aftel循环没有影响。

PS: Creting对象并不需要花费时间。 瓶颈是父指派。

UPD:大量意味着如果你是刚刚创造20 TLabel,添加Application.Processmessages是不是一个好主意,因为这将重绘其立即显示新添加的组件约500项..

+0

不能确认问题;测试和1000个标签创建在眼睛眨眼滚动框中。 – kludg

+1

我还测试了2000年...不到一秒...但是...如果我激活*自定义样式*,花费的时间炸毁... – Whiler

+0

你有条件断点和编译64位?我注意到这是可以避免的,如果可能的话。 –

回答

3

使用ScrollBox.DisableAlign.EnabledAlign

procedure TForm1.CreateLabels2; 
var 
    I: Integer; 
    ALabel: TLabel; 
    ATop: Integer; 
begin 
    ATop := 0; 
    ScrollBox2.DisableAlign; 
    for I := 0 to 2000 do 
    begin 
    ALabel := TLabel.Create(ScrollBox2); 
    FLabels.Add(ALabel); 
    with ALabel do 
    begin 
     Caption := 'Title'; 
     SetBounds(0, ATop, Width, Height); 
     Parent := ScrollBox2; 
    end; 
    Inc(ATop, 20); 
    end; 
    ScrollBox2.EnableAlign; 
end; 

enter image description here

+0

哇,启用/禁用对齐解决了一个问题! – yujin1st

+0

原因是,虽然对齐启用新的标签调用任何以前创建的方法realling方法,不是吗? – yujin1st

+0

@ yujin1st是的,只是说是真的。但是没有像_Reallign method_这样的东西,更完整的_Reallign framework_。 – NGLN

0

...

有了一个普通的应用程序,它需要不到1秒内产生2000 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; 

With a style If I desactivate the style

+0

是的,我使用标签的自定义样式。并且启用/禁用对齐可以快速解决问题。你的变体也解决了一个问题,但需要一点时间来申请 – yujin1st

+0

大量的意思是约500项... – yujin1st