加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – VirtualTreeView – 同一节点中不同颜色的文本

发布时间:2020-12-15 04:01:11 所属栏目:大数据 来源:网络整理
导读:我试图在TVirtualStringTree中创建一个类似于以下内容的视图: 在上面的例子中,我展示了一些我想要达到的可能场景. FolderA具有粗体文本,之后在同一节点中位于其后面的红色非压缩文本.我正在寻找制作这种输出的方法. 但是,如果创建太难或太难,我会对FolderB
我试图在TVirtualStringTree中创建一个类似于以下内容的视图:

在上面的例子中,我展示了一些我想要达到的可能场景. FolderA具有粗体文本,之后在同一节点中位于其后面的红色非压缩文本.我正在寻找制作这种输出的方法.

但是,如果创建太难或太难,我会对FolderB或FolderC类型的输出感到满意 – 这可能是由2列组成的,一列包含文件夹名称,另一列包含内部文件的数量.

FolderD在这里仅作为没有文件的文件夹的示例和该文件夹的输出(文本是非文本的,没有数字).

我正在寻找任何方向如何产生这种效果,因为似乎VirtualTreeView每个节点只能有单色或粗体设置.任何提示或建议如何向FolderA或FolderB或FolderC的方向移动高度赞赏,所以我有一个起点. Delphi或C Builder示例都是受欢迎的(最终代码将在C Builder中).

解决方法

您可以简单地使用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;

输出:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读