13

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

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
jhodzzz
  • 155
  • 1
  • 8

3 Answers3

11

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).

Nat
  • 5,414
  • 26
  • 38
  • what if i dont want color at all ? like remove the back ground color i tired `TargetCanvas.Brush.Style := bsClear;` but fail – MartinLoanel Apr 05 '16 at 22:58
  • 1
    @MartinLoanel You will need to do a whole lot more to make the whole control transparent. Ask that as a different question and you might get some answers or someone may have already done it. – Nat Apr 13 '16 at 00:51
7

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.

Erik Virtel
  • 810
  • 2
  • 9
  • 27
0

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;
Stéphane B.
  • 3,260
  • 2
  • 30
  • 35