在Lazarus 0.9.28.2项目中,我有TTreeView
,我的表单上有DirTree
的名称frmConvert
,但是我想用所有目录树填充它,自C:\
。
像这样:
C:\ Directory Tree http://i.imagehost.org/0185/cdirtree.png
当用户选择目录时,在第二个TTreeView
中,名称为FileTree
,显示该目录中的所有文件,但过滤后只显示PDF。
此外,我希望获得帮助来放置这些图标,因为对于最终用户而言更加有条理和友好是非常好的。
问题
DirTree
),如图片中所示?FileTree
上所选目录中的文件填充第二个TTreeview(DirTree
)?DirTree
上为每个文件夹(仅限文件夹)设置图标?答案 0 :(得分:4)
填充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;
****代码填充FileTree(已修订)****
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公共文件中有一个),然后将图像列表与目录树视图和filetree树视图
如何调用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;