使用目录树和过滤填充 TTreeView

发布于 2024-08-16 07:56:55 字数 696 浏览 7 评论 0原文

在 Lazarus 0.9.28.2 项目中,我的 Form(frmConvert) 上有一个 TTreeView,其名称为 DirTree,但我想填充它包含从 C:\ 开始的所有目录树。

像这样:
C:\ 目录树 http://i.imagehost.org/0185/cdirtree.png

当用户选择目录时,在第二个名为 FileTreeTTreeView 中,会显示该目录中的所有文件,但经过过滤后仅显示 PDF。

我还需要帮助来放置这些图标,因为对于最终用户来说更有组织性和友好性非常好。

问题

  • 如何使用所有目录填充第一个 TTreeView(DirTree),如图所示?
  • 如何使用 DirTree 上所选目录中的文件填充第二个 TTreeview(FileTree)?
  • 如何为 DirTree 上的每个文件夹(仅限文件夹)设置图标?

On a Lazarus 0.9.28.2 project I have a TTreeView, with the name DirTree on my Form(frmConvert), but I want to populate it with all the directory tree, since C:\.

Like this:
C:\ Directory Tree http://i.imagehost.org/0185/cdirtree.png

And when the user select the directory, in the second TTreeView, with the name FileTree, appear all the files in that directory, but filtered to show only PDFs.

Also I want help to put these icons, because is very nice to be more organized and friendly for the end-user.

Questions

  • How can I populate the first TTreeView(DirTree) with all the directorys, like in the image?
  • How can I populate the second TTreeview(FileTree) with the files on the directory selected on DirTree?
  • How can I set a icon for each folder(only folders) on DirTree?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘦怹 2024-08-23 07:56:55

填充 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 公共文件中有一个),然后将图像列表与目录树视图和文件树树视图关联

如何调用 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;

Code to populate the dirTree (REVISED)

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;

****Code to populate FileTree (REVISED) ****

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;

Additional procedure to add to form

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;

You need to add an image list to your form, add a folder icon to it (there is one in the borland common files) and then associated the image list with the directory treeview and the filetree treeview

EXAMPLE OF HOW TO CALLED FILTEREDTV procedure

Attach the following code to the OnClick event of the directory tree

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文