delphitvirtualstringtree

VirtualStringTree, changing the row background color


I have got some of the latest version of the VirtualTreeView and try to change the background color of the whole row of TVirtualStringTree both in selected and in non-selected states (toFullRowSelect is included somewhere). There is a lot of similar questions with different answers but none seems to fit well. In all of them you just write a handler that includes the code snippet like this:

  TargetCanvas.Brush.Color := SomeColor;
  TargetCanvas.FillRect(SomeRect);

But it's not that simple as I thought:

  1. OnBeforeCellPaint handler works well only if the row is not selected
  2. OnDrawText handler works in both states but the entire row looks divided by spaces between cells
  3. OnBeforeItemErased affects the whole row but again if it is not in selected state
  4. The painting in some other handlers either are repainted later automatically or require fully manual drawing which looks excessive for a simple task.

So I failed to find an easy way.

I added the additional conditions:

  1. The row must stay in selected state cause the tree could be in MultiSelected mode (toMultiSelect is included).
  2. The colors of selected and unselected states of the row may differ as well as different selected rows may have different colors too.

The best I could create is to write a handler on OnDrawTexr event: tree

This tree is produced by an array of records (oversimplified):

type
  TJob=record
    Running:Boolean;// the job is stopped or running
    MaxDuration:integer;//0 - infinite, or seconds
    Start:TDateTime;//The job start time
  end;

Solution

  • Use OnBeforeCellPaint for non selected row, use Colors.FocusedSelectionColor property for selected row..