Is it possible to change the color of a row in a virtual string tree?

I want to change the color of text in a specific row of a virtual string tree. is it possible?


Use the OnBeforeCellPaint event:

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;

This will change the background on every other row (if the rows are on the same level).


To control the color of the text in a specific row, use the OnPaintText event and set 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;

Note that this method is called for every cell in the TreeView. The Node pointer is the same in each cell of a row. So if you have multiple columns and want to set the color for a whole row accoring to the content of a specific column, you can use the given Node like in the example code.


To change the color of text in a specific row, OnDrawText event can be used in which you change current TargetCanvas.Font.Color property.

The code below works with Delphi XE 1 and virtual treeview 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;
链接地址: http://www.djcxy.com/p/35012.html

上一篇: VirtualStringTree CellPaint

下一篇: 是否有可能更改虚拟字符串树中行的颜色?