delphitvirtualstringtree

TvirtualStringTree custom color for nodes levels ( column and row )


I have TVirtualStringTree and I want to change its nodes background color based on the nodes levels as this picture :

enter image description here

I have tried like this but its giving my desired result for the rows only:

procedure TForm1.VSTBeforeCellPaint(Sender: TBaseVirtualTree;
  TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex;
  CellPaintMode: TVTCellPaintMode; CellRect: TRect; var ContentRect: TRect);
var
  R: TRect;
begin
  if CellPaintMode = cpmPaint then
  begin
    R := Sender.GetDisplayRect(Node, Column, True, False, True);
    R.Offset(0, -R.Top);
    case Sender.GetNodeLevel(Node) of
      0: TargetCanvas.Brush.Color := $0000F9FF;
      1: TargetCanvas.Brush.Color := $0000BFFF;
      2: TargetCanvas.Brush.Color := $000086FF;
    end;
    TargetCanvas.FillRect(CellRect);
  end;

end;

Solution

  • Not sure if its totally correct but its giving good result :

    procedure VSTBeforeCellPaint(Sender: TBaseVirtualTree;
      TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex;
      CellPaintMode: TVTCellPaintMode; CellRect: TRect; var ContentRect: TRect);
    var
      R: TRect;
      RectArr:Array of TRect;
      NodeLevel,i,k:Integer;
      ArrayColors:Array[0..6] of Tcolor ;
    begin
     NodeLevel:=Sender.GetNodeLevel(Node);
      if CellPaintMode = cpmPaint then
      begin
      if Column=0 then
       begin
        R := CellRect;
    
        setlength(RectArr,NodeLevel+2);
        RectArr[Length(RectArr)-1]:=Rect(r.Left+node.NodeHeight*(NodeLevel+1)-Node.NodeHeight,r.Top,r.Right,r.Bottom);
        for i := 0 to Length(RectArr)-2 do
        RectArr[i]:=Rect(r.Left+Node.NodeHeight*(i-1),r.Top,r.Left+Node.NodeHeight*i+Node.NodeHeight,r.Bottom);
          ArrayColors[0]:= $0000F9FF;
          ArrayColors[1] := $0000BFFF;
          ArrayColors[2]:= $000086FF;
          ArrayColors[3] := ClLime;
          ArrayColors[4] := Clred;
          ArrayColors[5] := clSkyBlue;
          ArrayColors[6]:= clGrayText;
    
        for I := 0 to Length(RectArr)-1 do
        begin
        K:=i-1;
        if k<0 then    k:=0;
    
        TargetCanvas.Brush.Color:=ArrayColors[k];
        TargetCanvas.FillRect(RectArr[i]);
        end;
       end else
           begin
           case Sender.GetNodeLevel(Node) of
           0: TargetCanvas.Brush.Color := $0000F9FF;
           1: TargetCanvas.Brush.Color := $0000BFFF;
           2: TargetCanvas.Brush.Color := $000086FF;
           3:TargetCanvas.Brush.Color := ClLime;
           4:TargetCanvas.Brush.Color := Clred;
           5:TargetCanvas.Brush.Color := clSkyBlue ;
           6:TargetCanvas.Brush.Color := clGrayText ;
           end;
           TargetCanvas.FillRect(CellRect);
           end;
      end;
    
    end;
    

    The Result : enter image description here