//文件或目录转换成 TreeView
procedure DirToTreeView(Tree: TTreeView; Directory:
string; Root: TTreeNode;
IncludeFiles:
Boolean);
var
SearchRec :
TSearchRec;
ItemTemp :
TTreeNode;
begin
with Tree.Items do
try
BeginUpdate;
if Directory[Length(Directory)] <> ‘\‘ then Directory :=
Directory + ‘\‘;
if FindFirst(Directory +
‘*.*‘, faDirectory, SearchRec) = 0 then
begin
repeat
if (SearchRec.Attr and faDirectory =
faDirectory) and (SearchRec.Name[1] <> ‘.‘) then
begin
if (SearchRec.Attr and faDirectory >
0) then
Root
:= AddChild(Root,
SearchRec.Name);
ItemTemp
:=
Root.Parent;
DirToTreeView(Tree,
Directory + SearchRec.Name, Root,
IncludeFiles);
Root
:=
ItemTemp;
end
else if
IncludeFiles then
if SearchRec.Name[1]
<> ‘.‘ then
AddChild(Root,
SearchRec.Name);
until FindNext(SearchRec) <> 0;
FindClose(SearchRec);
end;
finally
EndUpdate;
end;
end;
procedure TForm1.ToolButton7Click(Sender:
TObject);
var
node:TTreeNode;
begin
node:=nil;//放在OnCreate下面可以正常
DirToTreeView(TreeView1,‘Z:\‘,node,False);
end;
delphi 文件或目录转换成 TreeView