如果获取某种类型文件(如: .bmp  .txt  .zip 等很多)对应的图标,而不是某个特定文件(如: .exe 文件)所包含的图标。
我主要是想在界面上显示出来。

解决方案 »

  1.   

    show files and their associated Icons in a Listview?
      The following example shows how to show all files and their
      associated icons of a folder in a TListView.
      To test the code, you need a ListView1 and a ImageList1 where the icons are stored.  Im folgenden Beispiel werden alle Dateien & zugehörigen
      Icons eines Verzeichnisses in einer TListView angezeigt.
      Um das Beispiel zu testen, braucht man eine ListView1 und eine ImageList1
      Komponente, wo die Icons gespeichert werden.
    }
    uses
      ShellApi;procedure LV_InsertFiles(strPath: string; ListView: TListView; ImageList: TImageList);
    var
      i: Integer;
      Icon: TIcon;
      SearchRec: TSearchRec;
      ListItem: TListItem;
      FileInfo: SHFILEINFO;
    begin
      // Create a temporary TIcon
      Icon := TIcon.Create;
      ListView.Items.BeginUpdate;
      try
        // search for the first file
        i := FindFirst(strPath + '*.*', faAnyFile, SearchRec);
        while i = 0 do
        begin
          with ListView do
          begin
            // On directories and volumes
            if ((SearchRec.Attr and FaDirectory <> FaDirectory) and
              (SearchRec.Attr and FaVolumeId <> FaVolumeID)) then
            begin
              ListItem := ListView.Items.Add;
              //Get The DisplayName
              SHGetFileInfo(PChar(strPath + SearchRec.Name), 0, FileInfo,
                SizeOf(FileInfo), SHGFI_DISPLAYNAME);
              Listitem.Caption := FileInfo.szDisplayName;
              // Get The TypeName
              SHGetFileInfo(PChar(strPath + SearchRec.Name), 0, FileInfo,
                SizeOf(FileInfo), SHGFI_TYPENAME);
              ListItem.SubItems.Add(FileInfo.szTypeName);
              //Get The Icon That Represents The File
              SHGetFileInfo(PChar(strPath + SearchRec.Name), 0, FileInfo,
                SizeOf(FileInfo), SHGFI_ICON or SHGFI_SMALLICON);
              icon.Handle := FileInfo.hIcon;
              ListItem.ImageIndex := ImageList.AddIcon(Icon);
              // Destroy the Icon
              DestroyIcon(FileInfo.hIcon);
            end;
          end;
          i := FindNext(SearchRec);
        end;
      finally
        Icon.Free;
        ListView.Items.EndUpdate;
      end;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      // Assign a Imagelist to the ListView
      ListView1.SmallImages := ImageList1;
      // Show Listview in Report Style and add 2 Columns
      ListView1.ViewStyle := vsReport;
      ListView1.Columns.Add;
      ListView1.Columns.Add;
      LV_InsertFiles('C:\Windows\', ListView1, ImageList1);
    end;
      

  2.   

    关键在这两个中:
     Icon: TIcon;
    FileInfo: SHFILEINFO;
      

  3.   

    //只需要扩展名就可以获得图标
    //在Form中放置一个ListView1,下面的代码可以看到效果:uses ShellAPI, CommCtrl;procedure TForm1.FormCreate(Sender: TObject);
      procedure LoadIcons;
      var
        FileInfo: TSHFileInfo;
        S_ImageListHandle: Cardinal;
      begin
        //取得小图标集句柄
        S_ImageListHandle := SHGetFileInfo(nil, 0, FileInfo,
          SizeOf(FileInfo), SHGFI_SYSICONINDEX or SHGFI_SMALLICON);
        //将小图标集句柄指给列表
        SendMessage(ListView1.Handle, LVM_SETIMAGELIST, LVSIL_SMALL, S_ImageListHandle);
      end;
      function GetIcon(FileName: string): Integer;
      var
        FileInfo: TSHFileInfo;
      begin
        FillChar(FileInfo, SizeOf(FileInfo), #0);
        SHGetFileInfo(PChar(FileName), 0, FileInfo, SizeOf(FileInfo),
          SHGFI_SYSICONINDEX or SHGFI_USEFILEATTRIBUTES or SHGFI_SMALLICON);
        Result := FileInfo.iIcon;
      end;  procedure AddItem(s: String);
      begin
        with ListView1.Items.Add do
        begin
          ImageIndex := GetIcon(s);
          Caption := s;
        end;
      end;
    begin
      LoadIcons;
      ListView1.ViewStyle := vsReport;
      AddItem('.bmp');
      AddItem('.rmvb');
      AddItem('.exe');
    end;
      

  4.   

    aiirii(ari-淘金坑) ,我并不是取文件的图标,更不需要遍历文件夹,谢谢你的回复。 hthunter(核桃-我的心在下雨,雨中我和她携手漫步),感谢,不过我需要将你的程序改一下才能够正常显示出图标来。谢谢!