1

I was seeing this code and, with the logical adaptations, it suits me perfectly for my application in BCB 6 but I would like to know how I could do to change the background color of the cells. When I do it with a TListView I use the Brush property of the Canvas:

void __fastcall TForm1 :: ListView1CustomDrawItem (TCustomListView * Sender, TListItem * Item, TCustomDrawState State, bool & DefaultDraw)
{
      Sender-> Canvas-> Brush-> Color = clWhite;
      Sender-> Canvas-> Font-> Color = clBlack;
      Sender-> Canvas-> Font-> Style = TFontStyles () >> fsBold;
}

But I have verified that Sender-> Canvas-> Brush-> Color generates a compilation error ('TCustomControl: Canvas' is not accessible) and using TargetCanvas-> Brush-> Color does not produce any results.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62

1 Answers1

2

The TVirtualStringTree uses a rather different set of procedures for painting the cells of the tree. If you look in the help you will find that there are several events occuring for each cell. The ones you probably are interested in are:

OnBeforeCellPaint
OnPaintText
OnDrawText

OnBeforeCellPaint() provides the CellRect parameter, which you can use to fill the whole background, including tree expand symbol and eventual node image, or, using ContentRect, excluding tree expand symbol space,

procedure TForm1.VSTBeforeCellPaint(Sender: TBaseVirtualTree;
  TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex;
  CellPaintMode: TVTCellPaintMode; CellRect: TRect; var ContentRect: TRect);

and then use OnPaintText() to draw the text

procedure TForm1.VSTPaintText(Sender: TBaseVirtualTree;
  const TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex;
  TextType: TVSTTextType);

Alternatively, maybe easier to use only OnDrawText() in which you can both fill the text background (but, excluding tree expand symbol and image) and draw the text

procedure TForm1.VSTDrawText(Sender: TBaseVirtualTree; TargetCanvas: TCanvas;
  Node: PVirtualNode; Column: TColumnIndex; const Text: string;
  const CellRect: TRect; var DefaultDraw: Boolean);

Btw, I recommend looking at the help file in the dl package, for further details regarding painting the tree. The chapter Paint Cycles and Stages starts with this: The most complex process in Virtual Treeview is without doubts its painting. Read here what stages Virtual Treeview enters during paint and how you can customize this process.

Tom Brunberg
  • 20,312
  • 8
  • 37
  • 54
  • Thanks for your help. I will investigate the best solution. Only one precision: the OnDrawText event does not exist in TVirtualStringTree. – Angel Matilla Candás Oct 05 '19 at 07:51
  • Oh, maybe you are using an older version? (I do not know the history of `OnDrawText`, though). Anyway, the version I'm working with is said to be 6.2.5 (line 80 of VirtualTrees.pas). Looking at `VirtualTrees.hpp` under `typedef TCustomVirtualStringTree inherited;` there is a published `__property OnDrawText;` – Tom Brunberg Oct 05 '19 at 08:26
  • Thanks for answering. I think I am using the latest version of the component and I found the solution thanks to your suggestions using the OnBeforeCellPait event. – Angel Matilla Candás Oct 05 '19 at 09:30
  • @Angel, then try to consider [accepting this answer](https://meta.stackexchange.com/a/5235/179541), please. – TLama Nov 08 '19 at 22:19