2012-06-25 56 views
5

我用SoftGem的VirtualStringTree在Delphi 7如何爲不存在的節點顯示虛擬樹視圖網格線?

有沒有一種方法,使網格線(就像在TListView的)?我只能找到toShowHorzGridLines,它只顯示當前節點的行數,而不是下面任何空白的行數,而toShowVertGridLines只顯示垂直行數。

如何在項目添加之前在空白處顯示它們?

回答

5

我不認爲有一種簡單的方法來實現這一點,而不修改PaintTree方法,因爲沒有任何節點事件不能被觸發,因爲應該繪製線條的節點根本不存在。

這是一個骯髒的方式如何額外繪製基於最低可見節點的水平線。事實上,它繪製線條與在該地區的DefaultNodeHeight值由橙色在這個截圖充滿的距離:

enter image description here

下面是代碼:

type 
    TVirtualStringTree = class(VirtualTrees.TVirtualStringTree) 
    public 
    procedure PaintTree(TargetCanvas: TCanvas; Window: TRect; Target: TPoint; 
     PaintOptions: TVTInternalPaintOptions; PixelFormat: TPixelFormat = pfDevice); override; 
    end; 

implementation 

{ TVirtualStringTree } 

procedure TVirtualStringTree.PaintTree(TargetCanvas: TCanvas; Window: TRect; 
    Target: TPoint; PaintOptions: TVTInternalPaintOptions; 
    PixelFormat: TPixelFormat); 
var 
    I: Integer; 
    EmptyRect: TRect; 
    PaintInfo: TVTPaintInfo; 
begin 
    inherited; 
    if (poGridLines in PaintOptions) and (toShowHorzGridLines in TreeOptions.PaintOptions) and 
    (GetLastVisible <> nil) then 
    begin 
    EmptyRect := GetDisplayRect(GetLastVisible, 
     Header.Columns[Header.Columns.GetLastVisibleColumn].Index, False); 
    EmptyRect := Rect(ClientRect.Left, EmptyRect.Bottom + DefaultNodeHeight, 
     EmptyRect.Right, ClientRect.Bottom); 
    ZeroMemory(@PaintInfo, SizeOf(PaintInfo)); 
    PaintInfo.Canvas := TargetCanvas; 
    for I := 0 to ((EmptyRect.Bottom - EmptyRect.Top) div DefaultNodeHeight) do 
    begin 
     PaintInfo.Canvas.Font.Color := Colors.GridLineColor; 
     DrawDottedHLine(PaintInfo, EmptyRect.Left, EmptyRect.Right, 
     EmptyRect.Top + (I * DefaultNodeHeight)); 
    end; 
    end; 
end; 

這裏恆定的結果,變量節點高度:

enter image description here

毛刺(在上面的屏幕截圖中可見的線只是虛線渲染的結果。如果將您的虛擬樹視圖中的LineStyle屬性設置爲lsSolid,您將看到正確的結果。

+2

現在看結果,矩形被移位。我稍後會解決它(我現在必須去)。但仍然是非常骯髒的解決方案。 – TLama

+0

根本不髒!這實際上很不錯!非常感謝你! –

+2

唷,線路位置正確。這是由虛線渲染引起的。但是有一個錯誤。要確定行的右邊界,我使用索引爲「Header.Columns.Count - 1」的列出了什麼問題。如果你有例如2列,並將第二個移動到第一個位置,我應該使用的索引(最右列的索引)爲0,而不是1(什麼是「Header.Columns.Count - 1」)。現在我正在使用這個'GetLastVisibleColumn'應該是正確的方式。 – TLama