delphic++buildervirtualtreeviewtvirtualstringtree

VirtualTreeView - different color of text in the same node


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

Folder view with different font colors

In the above example I have shown some of the possible scenarios I want to reach. FolderA has bold text and after that red-colored unbolded text just behind it in the same node. I am looking for way to make this sort of output.

However, if this is too hard or too problematic to create, I would be happy with FolderB or FolderC type of output - which could probably be made with 2 columns, one containing the folder name and another containing the count of files inside.

FolderD is here just as example of a folder with no files and the output for that folder (text is unbolded and there is no number).

I am looking for any directions how to make this effect as it seems that VirtualTreeView can only have single color or bold setting per one node. Any tips or suggestions how to move in the direction of FolderA or FolderB or FolderC highly appreciated so I have a starting point. Delphi or C++ Builder examples are both welcome (the final code will be in C++ Builder though).


Solution

  • 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