Home > OS >  VirtualStringTree, changing the row background color
VirtualStringTree, changing the row background color

Time:10-31

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.

CodePudding user response:

in VirtualTrees, if node select, it must draw.. in VirtualTrees.pas line 25974

          if vsSelected in Node.States then
          begin
             if Focused or (toPopupMode in FOptions.FPaintOptions) then
             begin
                Brush.Color := FColors.FocusedSelectionColor;
                Pen.Color := FColors.FocusedSelectionBorderColor;
              end
              else
              begin
                Brush.Color := FColors.UnfocusedSelectionColor;
                Pen.Color := FColors.UnfocusedSelectionBorderColor;
              end;
          ....
          end

you can delete it:

              //Brush.Color := FColors.FocusedSelectionColor;
              //Pen.Color := FColors.FocusedSelectionBorderColor;

also you can do it on OnBeforeCellPaint and change node to not select:

  TargetCanvas.Brush.Color := $00776655;
  TargetCanvas.FillRect(CellRect);
  Exclude(Node.States, vsSelected);

it will like follow:

enter image description here enter image description here

CodePudding user response:

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

  • Related