You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Delphi中TVirtualStringTree组件图片重复显示问题及修复咨询

TVirtualStringTree图标重复显示修复方法

你当前使用TVirtualStringTree的VST1GetImageIndex事件设置图标时出现了重复显示的问题,截图如下:

重复图标显示

原代码如下:

procedure TFAbzarCode.VST1GetImageIndex(Sender: TBaseVirtualTree;
  Node: PVirtualNode; Kind: TVTImageKind; Column: TColumnIndex;
  var Ghosted: Boolean; var ImageIndex: TImageIndex);
var
  NodeData: ^rTreeData;
  MImageIndex:integer;
begin
  NodeData := Sender.GetNodeData(Node); 
  ImageIndex:=NodeData.ImageIndex;
end;

问题原因

你的代码没有区分TVTImageKind类型,不管是普通图标、选中状态图标还是状态图标,都统一设置了ImageIndex,导致多个图标位置都显示了相同的图标,从而出现重复。

修复代码

只在需要显示图标的Kind类型下设置索引,其他情况将ImageIndex设为-1(表示不显示图标),示例如下:

procedure TFAbzarCode.VST1GetImageIndex(Sender: TBaseVirtualTree;
  Node: PVirtualNode; Kind: TVTImageKind; Column: TColumnIndex;
  var Ghosted: Boolean; var ImageIndex: TImageIndex);
var
  NodeData: ^rTreeData;
begin
  // 默认不显示图标
  ImageIndex := -1;
  NodeData := Sender.GetNodeData(Node);
  // 仅在普通图标(ikNormal)类型下设置图标索引,根据需求可调整为其他Kind类型
  if Kind = ikNormal then
    ImageIndex := NodeData.ImageIndex;
end;

补充说明

  • TVTImageKind包含多种图标类型,比如ikNormal(普通状态图标)、ikSelected(选中状态图标)、ikState(状态图标)等,你可以根据实际需求选择对应的类型设置。
  • 如果需要仅在特定列显示图标,还可以增加Column参数判断,比如if (Kind = ikNormal) and (Column = 0),只在第一列显示图标。

内容的提问来源于stack exchange,提问作者sh sh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 22:13:14