VirtualTreeView - different color of text in the same node

前端 未结 1 1452
长发绾君心
长发绾君心 2020-12-06 12:32

I am attempting to create a view in TVirtualStringTree which will be similar to something like this:

相关标签:
1条回答
  • 2020-12-06 13:09

    You could simply use the toShowStaticText (StringOptions) option:

    implementation
    
    type
      PNodeRec = ^TNodeRec;
      TNodeRec = record
        Name: WideString;
        Count: Integer;
        IsBold: Boolean;
      end;
    
    procedure TForm1.FormCreate(Sender: TObject);
    var
      Node: PVirtualNode;
      NodeRec: PNodeRec;
      I: Integer;
    begin
      VirtualStringTree1.TreeOptions.StringOptions := 
        VirtualStringTree1.TreeOptions.StringOptions + [toShowStaticText];
      VirtualStringTree1.NodeDataSize := Sizeof(TNodeRec);
      // Populate some data
      for I := 1 to 10 do
      begin
        Node := VirtualStringTree1.AddChild(nil);
        NodeRec := VirtualStringTree1.GetNodeData(Node);
        Initialize(NodeRec^);
        NodeRec.Name := 'Node' + IntToStr(I);
        NodeRec.Count := I;
        NodeRec.IsBold := I mod 2 = 0;
      end;
    end;
    
    procedure TForm1.VirtualStringTree1GetText(Sender: TBaseVirtualTree;
      Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType;
      var CellText: WideString);
    var
      NodeRec: PNodeRec;
    begin
      NodeRec := PNodeRec(TVirtualStringTree(Sender).GetNodeData(Node));
      if TextType = ttNormal then
        CellText := NodeRec^.Name
      else // ttStatic
        CellText := Format('(%d)', [NodeRec^.Count]);
    end;
    
    procedure TForm1.VirtualStringTree1PaintText(Sender: TBaseVirtualTree;
      const TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex;
      TextType: TVSTTextType);
    var
      NodeRec: PNodeRec;
    begin
      NodeRec := PNodeRec(TVirtualStringTree(Sender).GetNodeData(Node));
      if TextType = ttNormal then
      begin
        if NodeRec^.IsBold then
          TargetCanvas.Font.Style := TargetCanvas.Font.Style + [fsBold];
      end
      else // ttStatic
        TargetCanvas.Font.Color := clRed;
    end;
    

    Output:

    enter image description here

    0 讨论(0)
提交回复
热议问题