我正在尝试扫描文件夹中的文件和子文件夹,然后将结果添加到 VirtualStringTree .. 这很好用,除非向树视图的每个节点添加图标(图标来自系统外壳图标) .
出现了一个奇怪的问题,图标仅在某些节点上显示,在大多数情况下,我得到的只有一个图标(见下图)。

这是我使用的代码:
树节点数据声明:
nodeData=record
          Text:String;
          ImageIndex:integer;
         end;
PNodeData=^nodeData;
&这里是扫描文件/文件夹的代码
procedure scanDir(const Dir:string);
var SR: TSearchRec;
begin
addNewNode(Dir); 
if(FindFirst(IncludeTrailingBackslash(Dir) + '*.*', faAnyFile or faDirectory, SR)=0)then
 try
  if(isFile)then addNewNode(IncludeTrailingBackslash(Dir)+SR.Name);
  else if(isFolder)then scanDir(IncludeTrailingBackslash(Dir)+SR.Name);  // recursive call!
 until(FindNext(Sr)<>0);
 finally
 FindClose(SR);
 end;
end;
添加新节点的代码:
procedure addNewNode(Text:String);
var theNode,Node:PVirtualNode;
    d:PNodeData;
    parent:string;
    SHFileInfo :TSHFileINfo;
    Icon: TIcon;
begin
parent:=ExtractFileDir(Text);
if(hashmap.ContainsKey(parent))then theNode:=hashmap.Items[parent]
else theNode:=nil;
Icon := TIcon.Create;
SHGetFileInfo(PChar(Text), 0, SHFileInfo,  SizeOf(SHFileInfo),
                SHGFI_ICON or SHGFI_SMALLICON );
Icon.Handle := SHFileInfo.hIcon;
// begin updating
Form1.theTree.BeginUpdate;
// add a child node
Node:=Form1.theTree.AddChild(theNode);
d:=Form1.theTree.GetNodeData(Node);
// add a checkbox
Node.CheckType:=ctCheckBox;
Node.CheckState:=csCheckedNormal;
if((ExtractFileExt(Text)='')and(not hashmap.ContainsKey(Text)))
then hashmap.Add(Text, Node);
// assign a text to the newly created node
d^.Text:=ExtractFileName(Text);
// assign an ImageIndex to the newly created node
d^.ImageIndex:=Form1.treeImageList.AddIcon(Icon);
// end update
Form1.theTree.EndUpdate;
// Destroy the Icon
DestroyIcon(SHFileInfo.hIcon);
end;
treeVeiw OnGetImageIndex 的代码:
procedure TForm1.theTreeGetImageIndex(Sender: TBaseVirtualTree;
  Node: PVirtualNode; Kind: TVTImageKind; Column: TColumnIndex;
  var Ghosted: Boolean; var ImageIndex: Integer);
var  FileInfo:PNodeData;
begin
  FileInfo := Sender.GetNodeData(Node);
  ImageIndex := FileInfo^.ImageIndex;
end;
PS:treeImageList是链接到treeView.images属性的 imageList