如何在 TVirtualStringTree 中绘制特殊级别的背景颜色

How to paint the background color in special level in TVirtualStringTree

我尝试在 VirtualStringTree 的所有特殊级别中使用背景颜色绘制高线文本。它看起来像是所有相同级别的选定节点。 下面的代码不起作用。请哪位指点一下。

procedure TMainForm.Tree1PaintText(Sender: TBaseVirtualTree; const TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType);
var  Data: PNodeData;LEVEL:INTEGER;  tree1node,tree4Node: PVirtualNode;
begin 
    Data := Tree1.GetNodeData(Node);
    Level := tree1.GetNodeLevel(node);

 case column of
     0:begin
        if Level = 0 then BEGIN
               TargetCanvas.Font.Style := TargetCanvas.Font.Style + [fsBold];
               TargetCanvas.Font.Color :=CLyellow;
               targetcanvas.Brush.Color :=clgreen;//don't work
               targetcanvas.Brush.Style :=bssolid;     

             END;
            if  Level = 1 then BEGIN
                  TargetCanvas.Font.Color :=CLaqua; 
                  targetcanvas.Brush.Color :=clgreen;
            end;
       end;

VT 更快地填充单元格背景,在 PrepareCell 方法中更具体。所以现在尝试设置 canvas 笔刷已经太晚了。尝试从 OnBeforeCellPaint 事件填充节点矩形:

procedure TForm1.VirtualStringTree1BeforeCellPaint(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 := [=10=]00F9FF;
      1: TargetCanvas.Brush.Color := [=10=]00BFFF;
      2: TargetCanvas.Brush.Color := [=10=]0086FF;
    end;
    TargetCanvas.FillRect(R);
  end;
end;

预览:

一种方法是使用 eaColor 作为 OnBeforeItemErase 事件中的擦除操作:

procedure TMainForm.Tree1BeforeItemErase(Sender: TBaseVirtualTree; TargetCanvas: TCanvas;
          Node: PVirtualNode; ItemRect: TRect; var ItemColor: TColor; var EraseAction: TItemEraseAction);
begin
   if not Sender.Selected[Node] then begin
      case Sender.GetNodeLevel(Node) of
        0: ItemColor := clgreen;
        1: ItemColor := clAgua;
      end;
      EraseAction := eaColor;
   end;
end;