??? 如何获取与某种类型文件相关联的图标?

poordever 2005-08-22 10:35:53
如果获取某种类型文件(如: .bmp .txt .zip 等很多)对应的图标,而不是某个特定文件(如: .exe 文件)所包含的图标。


我主要是想在界面上显示出来。
...全文
156 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
poordever 2005-08-23
  • 打赏
  • 举报
回复
aiirii(ari-淘金坑) ,我并不是取文件的图标,更不需要遍历文件夹,谢谢你的回复。

hthunter(核桃-我的心在下雨,雨中我和她携手漫步),感谢,不过我需要将你的程序改一下才能够正常显示出图标来。谢谢!
hthunter 2005-08-22
  • 打赏
  • 举报
回复
//只需要扩展名就可以获得图标
//在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;
aiirii 2005-08-22
  • 打赏
  • 举报
回复
关键在这两个中:
Icon: TIcon;
FileInfo: SHFILEINFO;
aiirii 2005-08-22
  • 打赏
  • 举报
回复
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;

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧