我正在尝试扫描文件夹中的文件&子文件夹,然后将结果添加到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