2010-07-23 38 views

回答

8

使用OnBeforeCellPaint事件:

procedure TForm1.VirtualStringTree1BeforeCellPaint(Sender: TBaseVirtualTree; 
    TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex; 
    CellPaintMode: TVTCellPaintMode; CellRect: TRect; var ContentRect: TRect); 
begin 
    if Node.Index mod 2 = 0 then 
    begin 
    TargetCanvas.Brush.Color := clFuchsia; 
    TargetCanvas.FillRect(CellRect); 
    end; 
end; 

這將改變背景上每隔一行(如果該行是在同一水平上)。

+0

如果我不想要顏色,該怎麼辦?像刪除背景顏色我累了'TargetCanvas.Brush.Style:= bsClear;'但失敗 – MartinLoanel 2016-04-05 22:58:33

+1

@MartinLoanel你需要做更多的事情來使整個控件變得透明。問這是一個不同的問題,你可能會得到一些答案或有人可能已經做到了。 – Nat 2016-04-13 00:51:23

+0

已經找到了方法 – MartinLoanel 2016-04-13 12:36:05

7

要控制特定行中文本的顏色,請使用OnPaintText事件並設置TargetCanvas.Font.Color。

procedure TForm.TreePaintText(Sender: TBaseVirtualTree; const TargetCanvas: 
    TCanvas; Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType); 
var 
    YourRecord: PYourRecord; 

begin 
    YourRecord := Sender.GetNodeData(Node); 

    // an example for checking the content of a specific record field 
    if YourRecord.Text = 'SampleText' then 
    TargetCanvas.Font.Color := clRed; 
end; 

請注意,此方法是爲TreeView中的每個單元調用的。節點指針在一行的每個單元中是相同的。因此,如果您有多個列,並且想要根據特定列的內容設置整行的顏色,則可以使用示例代碼中的給定節點。

0

要改變文本的顏色在一個特定的行,OnDrawText事件可用於在其中改變目前TargetCanvas.Font.Color財產。

下面的代碼可與德爾福XE 1和虛擬樹視圖5.5.2(http://virtual-treeview.googlecode.com/svn/branches/V5_stable/

type 
    TFileVirtualNode = packed record 
    filePath: String; 
    exists: Boolean; 
    end; 

    PTFileVirtualNode = ^TFileVirtualNode ; 

procedure TForm.TVirtualStringTree_OnDrawText(Sender: TBaseVirtualTree; TargetCanvas: TCanvas; Node: PVirtualNode; 
    Column: TColumnIndex; const Text: UnicodeString; const CellRect: TRect; var DefaultDraw: Boolean); 
var 
    pileVirtualNode: PTFileVirtualNode; 
begin 
    pileVirtualNode:= Sender.GetNodeData(Node); 

    if not pileVirtualNode^.exists then 
    begin 
    TargetCanvas.Font.Color := clGrayText; 
    end; 
end; 
相關問題