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