delphi - 如何在TVirtualStringTree的一列中显示图标或图像?

标签 delphi virtualtreeview tvirtualstringtree

在 Delphi VCL 项目中,我创建了一个包含两列的简单 TVirtualStringTree。第一列将包含标识所表示数据的名称的文本。数据记录还包含状态字段。第二列旨在使用不带文本的图像(16x16 像素)来表示记录的状态。

我搜索过demo,但没有掌握VTV显示节点的完整流程,也没有成功让图标显示在指定列的节点上。

所以我有三个相关问题:

  1. 我了解了如何在 OnGetText 事件中分配文本,但我应该在哪里分配或更改图像以反射(reflect)记录中的当前状态?

  2. 如何让图像实际显示在列中?

  3. 图像的大小是否受到限制,或者图像可以大于图标吗?如果是这样,我是否需要更改任何设置来调整每行的高度(如果可能)?

最佳答案

您需要将(在您的情况下为 16x16)TImageList 分配给 TVirtualStringTree.Images 属性,然后处理事件 OnGetImageIndex 例如:

procedure TForm1.VirtualStringTree1GetImageIndex(Sender: TBaseVirtualTree;
  Node: PVirtualNode; Kind: TVTImageKind; Column: TColumnIndex;
  var Ghosted: Boolean; var ImageIndex: Integer);
var
  NodeRec: PNodeRec;
begin
  NodeRec := Sender.GetNodeData(Node);
  if Assigned(NodeRec) then 
  begin
    if (Column = 1) then
    begin 
      if Kind in [ikNormal, ikSelected] then
      begin
        case NodeRec.Status of // check the needed status(es)
          1: ImageIndex := 1; // whichever image you need
          2: ImageIndex := 2; // whichever image you need
          // ...
        end; 
      end;
    end;
  end;
end;

Am I limited in size for the images, or can they be larger than icons? If so, do I need to change any settings to adjust the height of each row (if possible)

不确定您的意思,因为您说您需要 16x16 图像。如果您需要尺寸可能不同的不同图像列表,您可以使用OnGetImageIndexEx。对于可变高度,您可以在 TreeOptions.MiscOptions 中设置 toVariableNodeHeight 并处理 OnMeasureItem 事件。例如,将图形绘制到 VTV Canvas 中的另一种方法是处理 OnBeforeItemPaint/OnAfterItemPaint

关于delphi - 如何在TVirtualStringTree的一列中显示图标或图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43858775/

相关文章:

delphi - VirtualStringTree 过滤正确完成

delphi - 如何在TVirtualStringTree中绘制动画水平条?

delphi - TVirtualStringTree 中的 GetText 被触发更多次

delphi - 具有隐藏节点的 VirtualStringTree 行的颜色

delphi - TToolBar:如何摆脱竖线?

delphi - 为什么 WebApplication.SendFile 失败并显示 "Reply type already set"?

android - Firemonkey - 在移动应用程序中定位组件的技巧

delphi - Delphi Datasnap REST 服务器中的异常处理