填充 dirTree 的代码(已修订)
procedure TForm1.FormClick(Sender: TObject);
var
sr: TSearchRec;
FileAttrs: Integer;
theRootNode : tTreeNode;
theNode : tTreeNode;
begin
FileAttrs := faDirectory; // Only care about directories
theRootNode := DirTree.Items.AddFirst(nil,'c:\');
if FindFirst('c:\*.*', FileAttrs, sr) = 0 then
begin
repeat
if (sr.Attr and FileAttrs) = sr.Attr then
begin
theNode := dirTree.Items.AddChild(theRootNode,sr.name);
AddDirectories(theNode,'c:\'+sr.Name);
end;
until FindNext(sr) <> 0;
FindClose(sr);
end;
// DirTree.FullExpand;
end;
****填充文件树的代码(已修订)****
procedure TForm1.FilteredTV(theDir: string;ext:String;startNode:tTreeNode);
var
sr: TSearchRec;
FileAttrs: Integer;
theNode : tTreeNode;
begin
if copy(ext,1,1)<>'.' then ext := '.'+ext;
FileAttrs := faAnyfile;
if startNode = nil then
StartNode := FileTree.Items.AddFirst(nil,theDir);
if FindFirst(theDir+'\*.*', FileAttrs, sr) = 0 then
begin
repeat
if (sr.Attr=faDirectory) and (copy(sr.Name,1,1)<>'.') then
begin
theNode := FileTree.Items.AddChild(StartNode,sr.name);
theNode.ImageIndex := 0; // Use folder image for directories
FilteredTV(theDir+'\'+sr.name,ext,theNode);
end
else
if ((sr.Attr and FileAttrs) = sr.Attr) and (ExtractFileExt(sr.name)=ext)
then
begin
theNode := FileTree.Items.AddChild(StartNode,sr.name);
theNode.ImageIndex := -1; // No image for files
end;
until FindNext(sr) <> 0;
FindClose(sr);
end;
FileTree.FullExpand;
end;
添加到表单的附加程序
procedure TForm1.AddDirectories(theNode: tTreeNode; cPath: string);
var
sr: TSearchRec;
FileAttrs: Integer;
theNewNode : tTreeNode;
begin
FileAttrs := faDirectory; // Only care about directories
if FindFirst(cPath+'\*.*', FileAttrs, sr) = 0 then
begin
repeat
if ((sr.Attr and FileAttrs) = sr.Attr) and (copy(sr.Name,1,1) <> '.')
then
begin
theNewNode := dirTree.Items.AddChild(theNode,sr.name);
AddDirectories(theNewNode,cPath+'\'+sr.Name);
end;
until FindNext(sr) <> 0;
FindClose(sr);
end;
end;
您需要在表单中添加一个图像列表,向其中添加一个文件夹图标(在 borland 公共文件中有一个),然后将图像列表与目录树视图和文件树树视图相关联
如何调用 FILTEREDTV 程序的示例
将以下代码附加到目录树的 OnClick 事件中
procedure TForm1.DirTreeClick(Sender: TObject);
var
cBuild : string;
theNode : tTreeNode;
begin
if DirTree.Selected <> nil then
begin
theNode := DirTree.Selected;
cBuild := theNode.Text;
while theNode.Parent <> nil do
begin
cBuild := theNode.Parent.Text+'\'+cBuild;
theNode := theNode.Parent;
end;
cBuild := stringReplace(cBuild,'\\','\',[rfReplaceAll]);
FilteredTV(cBuild,'pdf',nil);
end;
end;