VirtualTreeView - 同一节点中文本的不同颜色 [英] VirtualTreeView - different color of text in the same node

查看:256
本文介绍了VirtualTreeView - 同一节点中文本的不同颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 TVirtualStringTree 中创建一个视图,类似于以下内容:

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

< img src =https://i.stack.imgur.com/Hx2NN.pngalt =具有不同字体颜色的文件夹视图>

在上述示例中我已经展示了我想要达到的一些可能的情况。 FolderA 具有粗体文本,并且之后的红色未折叠文本位于同一个节点的后面。我正在寻找这种输出方式。

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.

但是,如果这太难或太难以创建,我会很高兴与 FolderB FolderC 输出类型 - 可能会使用2列,一个包含文件夹名称,另一个包含文件计数。

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 这里只是一个没有文件的文件夹的示例,该文件夹的输出(文本被打开,没有数字)。

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

我正在寻找任何方向如何使这个效果,因为似乎VirtualTreeView只能有一个颜色或粗体设置每一个节点。任何提示或建议如何移动 FolderA FolderB FolderC 的方向非常感激,所以我有一个起点。 Delphi或C ++ Builder示例都是受欢迎的(最终的代码将在C ++ Builder中)。

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

推荐答案

toShowStaticText StringOptions )选项:

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:

这篇关于VirtualTreeView - 同一节点中文本的不同颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆