如果获取某种类型文件(如: .bmp .txt .zip 等很多)对应的图标,而不是某个特定文件(如: .exe 文件)所包含的图标。
我主要是想在界面上显示出来。
我主要是想在界面上显示出来。
解决方案 »
- 除了PCHAR还有什么办法能把STRING转换成PCHAR?
- delphi 2007 的问题
- DBGRIDEH控件DBGridEh1DrawColumnCell事件显示不同颜色后格线上花屏是怎么回事,怎么解决?
- 请大家帮忙看看这段操作excel 的代码,创建excel文件后,打开excel返应很慢。。。分用光了
- 从oracle导出数据到txt后txt的格式问题
- 怎样给FastReport要打印的值赋值?
- delphi修改注册表ip 帅哥美女请进来指导 我没分给:(
- 将Dephi编译成一个exe文件所遇到的问题
- 似乎是一个古老的问题,但是答案始终没有!
- 如何合并EXCEL单元格,如何使单元格中的内容居中?
- 一个软件要写,感兴趣的进来看一下
- ascii的问题
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;
Icon: TIcon;
FileInfo: SHFILEINFO;
//在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;