You need to enable JavaScript to run this app. 如何在Delphi中用类ListView控件实现C/D/E盘文件目录浏览功能
优惠活动
大模型
产品
解决方案
定价
更多

如何在Delphi中用类ListView控件实现C/D/E盘文件目录浏览功能

Delphi实现带图标磁盘选择、文件浏览与内容查看功能

1. 带图标的磁盘TComboBox实现

要在TComboBox中显示带图标的本地磁盘,需开启自定义绘制并获取系统磁盘图标:

  • 设置ComboBox的Style属性为csOwnerDrawFixed,允许自定义绘制每一项
  • 通过GetLogicalDriveStrings获取系统所有磁盘,筛选出本地固定磁盘(DRIVE_FIXED)
  • 调用SHGetFileInfo获取磁盘图标,存入ImageList
  • 在OnDrawItem事件中绘制图标与磁盘路径文本

代码示例:

uses ShellApi;

procedure TForm1.FormCreate(Sender: TObject);
var
  DriveStrings: array[0..255] of Char;
  p: PChar;
  FileInfo: TSHFileInfo;
begin
  ImageList1.Clear;
  ComboBox1.Items.Clear;
  GetLogicalDriveStrings(SizeOf(DriveStrings), DriveStrings);
  p := DriveStrings;
  while p^ <> #0 do
  begin
    if GetDriveType(p) = DRIVE_FIXED then
    begin
      SHGetFileInfo(p, 0, FileInfo, SizeOf(FileInfo), SHGFI_ICON or SHGFI_SMALLICON or SHGFI_USEFILEATTRIBUTES);
      ImageList1.AddIcon(FileInfo.hIcon);
      ComboBox1.Items.Add(p);
      DestroyIcon(FileInfo.hIcon);
    end;
    Inc(p, StrLen(p) + 1);
  end;
end;

procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
var
  CB: TComboBox;
begin
  CB := Control as TComboBox;
  CB.Canvas.FillRect(Rect);
  ImageList1.Draw(CB.Canvas, Rect.Left + 2, Rect.Top + 2, Index);
  CB.Canvas.TextOut(Rect.Left + ImageList1.Width + 4, Rect.Top + 2, CB.Items[Index]);
end;

2. TListView加载带图标的文件与文件夹

当选择磁盘路径后,在TListView中展示带图标的文件和文件夹:

  • 设置ListView的ViewStyle为vsIcon(或vsReport),关联ImageList用于存储图标
  • 遍历目标路径下的文件/文件夹,通过SHGetFileInfo获取对应图标
  • 向ListView添加项,绑定图标并存储完整路径信息

代码示例:

procedure TForm1.LoadPath(const Path: string);
var
  SearchRec: TSearchRec;
  FileInfo: TSHFileInfo;
  ListItem: TListItem;
  ItemPath: string;
begin
  ListView1.Items.Clear;
  ImageList2.Clear;
  if FindFirst(Path + '*.*', faAnyFile, SearchRec) = 0 then
  begin
    repeat
      if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
      begin
        ItemPath := Path + SearchRec.Name;
        SHGetFileInfo(PChar(ItemPath), 0, FileInfo, SizeOf(FileInfo), SHGFI_ICON or SHGFI_SMALLICON);
        ImageList2.AddIcon(FileInfo.hIcon);
        DestroyIcon(FileInfo.hIcon);

        ListItem := ListView1.Items.Add;
        ListItem.Caption := SearchRec.Name;
        ListItem.ImageIndex := ImageList2.Count - 1;
        ListItem.SubItems.Add(ItemPath); // 存储完整路径
        ListItem.SubItems.Add(IfThen((SearchRec.Attr and faDirectory) <> 0, 'Folder', 'File'));
      end;
    until FindNext(SearchRec) <> 0;
    FindClose(SearchRec);
  end;
end;

procedure TForm1.ComboBox1Change(Sender: TObject);
begin
  if ComboBox1.Text <> '' then
    LoadPath(ComboBox1.Text);
end;

3. 点击加载路径与查看文件内容

处理ListView的点击事件,实现路径跳转与文件内容查看:

  • 点击文件夹项时,加载该路径下的文件/文件夹
  • 点击文件项时,根据扩展名判断类型:文本文件用TMemo显示,图片用TImage显示,其他类型调用默认程序打开

代码示例:

procedure TForm1.ListView1Click(Sender: TObject);
var
  FullPath, Ext: string;
begin
  if not Assigned(ListView1.Selected) then Exit;
  FullPath := ListView1.Selected.SubItems[0];
  
  if ListView1.Selected.SubItems[1] = 'Folder' then
  begin
    if FullPath[Length(FullPath)] <> '\' then
      FullPath := FullPath + '\';
    LoadPath(FullPath);
    ComboBox1.Text := FullPath; // 更新路径选择框
  end
  else
  begin
    Ext := LowerCase(ExtractFileExt(FullPath));
    if (Ext = '.txt') or (Ext = '.pas') or (Ext = '.ini') or (Ext = '.log') then
    begin
      Memo1.Lines.LoadFromFile(FullPath);
      Memo1.Visible := True;
      Image1.Visible := False;
    end
    else if (Ext = '.jpg') or (Ext = '.png') or (Ext = '.bmp') or (Ext = '.ico') then
    begin
      Image1.Picture.LoadFromFile(FullPath);
      Image1.Visible := True;
      Memo1.Visible := False;
    end
    else
    begin
      // 调用系统默认程序打开文件
      ShellExecute(Handle, 'open', PChar(FullPath), nil, nil, SW_SHOWNORMAL);
    end;
  end;
end;

内容的提问来源于stack exchange,提问作者X12

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 12:52:35