VirtualTreeView - different color of text in the same node

≡放荡痞女 提交于 2019-12-09 15:54:34

问题


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

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).


回答1:


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:



来源:https://stackoverflow.com/questions/27275306/virtualtreeview-different-color-of-text-in-the-same-node

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!