2012-05-02 41 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