【问题标题】:How to paint the background color in special level in TVirtualStringTree如何在 TVirtualStringTree 中以特殊级别绘制背景颜色
【发布时间】:2018-09-10 16:10:58
【问题描述】:

我尝试在 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;

【问题讨论】:

    标签: delphi virtualtreeview tvirtualstringtree


    【解决方案1】:

    VT 更快地填充单元格背景,在 PrepareCell 方法中更具体。因此,尝试设置画布画笔为时已晚。尝试从 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 := $0000F9FF;
          1: TargetCanvas.Brush.Color := $0000BFFF;
          2: TargetCanvas.Brush.Color := $000086FF;
        end;
        TargetCanvas.FillRect(R);
      end;
    end;
    

    预览:

    【讨论】:

    • 恭喜您达到 5k 代表! :)
    【解决方案2】:

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

    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;
    

    【讨论】:

    • 嗨,我测试了代码。它可以在不同的列中显示背景颜色。 “itemcolor”似乎只适用于 fillrect all item 。如果仅在项目的文本中填充(不包括空格),如何改进?。谢谢
    • 是的,ItemErase 适用于整行(即所有列),如果您需要更多控制,您可能必须使用自定义绘制。
    猜你喜欢
    • 1970-01-01
    • 2020-02-02
    • 1970-01-01
    • 2016-10-04
    • 2020-04-21
    • 1970-01-01
    • 2014-06-20
    相关资源
    最近更新 更多