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

3

13

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

Acidity answered 23/7, 2010 at 1:33 Comment(1)
So, is your question answered?Mandorla
M
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).

Mandorla answered 23/7, 2010 at 2:24 Comment(2)
what if i dont want color at all ? like remove the back ground color i tired TargetCanvas.Brush.Style := bsClear; but failEmmy
@Emmy 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.Mandorla
C
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.

Cavanaugh answered 11/7, 2012 at 10:18 Comment(0)
P
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;
Pillage answered 30/12, 2014 at 14:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.