首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> Delphi

Delphi 利用 TreeView 構建 Windows 風格目錄樹:DriveComboBox/FileListBox 協作實現

     編輯:Delphi

前言

標準 TDriveComboBox/TDirectoryListBox/TFileListBox 三聯控件雖簡單但只能逐級返回根目錄切換,無法像資源管理器任意展開/折疊分支。本文展示如何結合 TTreeView、TDriveComboBox(隱藏)、TFileListBox(隱藏、模式 fdDirectory)構建全功能目錄樹,支援任意節點展開、即時加載子目錄。

核心設計思路

  1. 根節點:遍歷 DriveComboBox1.Items 獲取所有磁盤分區,作為樹頂層節點,HasChildren := True 顯示展開箭頭。
  2. 按需加載:響應 OnExpanding 事件,僅當用戶展開節點時,通過 FileListBox1.Directory := 完整路徑 讀取子目錄,添加為子節點。
  3. 路徑構建:遞歸向上拼接父節點 .Text 構建完整路徑(如 C:\Windows\System32)。
  4. 圖標:使用 TImageList 提供文件夾圖標(關閉/打開)。

關鍵代碼實現

1. 表單創建:構建根節點

procedure TForm1.FormCreate(Sender: TObject);
var
  FirstNode, DirNode: TTreeNode;
  ItemCount, Index: Integer;
  ItemStr: string;
begin
  ItemCount := DriveComboBox1.Items.Count;
  FirstNode := DirTreeView.Items.GetFirstNode;
  for Index := 0 to ItemCount - 1 do begin
    ItemStr := DriveComboBox1.Items[Index];
    ItemStr := Copy(ItemStr, 1, Pos(':', ItemStr)); {提取 'C:' 'D:' 等}
    DirNode := DirTreeView.Items.AddChild(FirstNode, ItemStr);
    DirNode.HasChildren := True;   {顯示 + 號}
    DirNode.ImageIndex := 0;       {關閉文件夾圖標}
    DirNode.SelectedIndex := 1;    {打開文件夾圖標}
  end;
end;

2. 節點展開事件:動態加載子目錄

procedure TForm1.DirTreeViewExpanding(Sender: TObject; Node: TTreeNode;
  var AllowExpansion: Boolean);
var
  DirNode: TTreeNode;
  ItemCount, Index, Level, iCount: Integer;
  ItemStr, StrPath: string;
begin
  if Node.Count = 0 then begin {首次展開才加載}
    iCount := 0;
    Level := Node.Level;
    DirNode := Node;
    StrPath := Node.Text + '\';
    {向上遞歸拼接完整路徑}
    while Level > 0 do begin
      StrPath := DirNode.Parent.Text + '\' + StrPath;
      DirNode := DirNode.Parent;
      Dec(Level);
    end;

    FileListBox1.Clear;
    FileListBox1.Directory := StrPath; {觸發讀取}
    ItemCount := FileListBox1.Items.Count;
    for Index := 0 to ItemCount - 1 do begin
      ItemStr := FileListBox1.Items[Index];
      ItemStr := Copy(ItemStr, 2, Pos(']', ItemStr) - 2); {去除 [ ] 包裹}
      if (ItemStr <> '.') and (ItemStr <> '..') then begin
        DirNode := DirTreeView.Items.AddChild(Node, ItemStr);
        DirNode.HasChildren := True;
        DirNode.ImageIndex := 0;
        DirNode.SelectedIndex := 1;
        Inc(iCount);
      end;
    end;
    if iCount = 0 then Node.HasChildren := False; {無子目錄隱藏 + 號}
  end;
end;

界面佈局與屬性設置

控件關鍵屬性
TTreeView (DirTreeView)Images = ImageList1; OnExpanding = DirTreeViewExpanding
TDriveComboBox (DriveComboBox1)Visible = False
TFileListBox (FileListBox1)Visible = False; FileType = fdDirectory
TImageList (ImageList1)含 2 個圖標: 0=關閉文件夾, 1=打開文件夾

運行效果

樹形圖左側顯示所有磁盤分區,點擊 + 號即時展開該目錄下子文件夾,可任意層級展開/折疊,體驗同 Windows 資源管理器一致。

目錄樹構建效果

運行效果圖

完整單元代碼

unit main;

interface
uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, FileCtrl, ComCtrls, ImgList;

type
  TForm1 = class(TForm)
    DirTreeView: TTreeView;
    FileListBox1: TFileListBox;
    DriveComboBox1: TDriveComboBox;
    ImageList1: TImageList;
    procedure FormCreate(Sender: TObject);
    procedure DirTreeViewExpanding(Sender: TObject; Node: TTreeNode;
      var AllowExpansion: Boolean);
  end;

var Form1: TForm1;

implementation
{$R *.DFM}

{ ... 以上兩個核心事件處理代碼 ... }

end.

優化與擴展建議

  • 異步加載:大目錄(如 C:\Windows\winsxs) 讀取耗時,可放入後台線程,通過 TThread.Queue 回主線程添加節點,避免 UI 凍結。
  • 圖標提取:使用 SHGetFileInfo 獲取系統真實文件夾圖標(含覆蓋箭頭、壓縮態等)。
  • 虛擬模式:TVirtualTreeView (Virtual Treeview) 支援百萬節點高性能渲染,適合極大目錄樹。
  • 過濾/搜尋:工具欄添加編輯框,實時過濾樹節點顯示。

總結

利用隱藏的 DriveComboBox/FileListBox 作為目錄枚舉引擎,配合 TreeView.OnExpanding 按需加載,僅需 50 行核心代碼即可構建專業級目錄樹控件,是 Delphi 快速開發文件管理介面的經典範式。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved