它可以将某一文件的大、小显示图标调出来。
var
FileInfo: TSHFileInfo;
Flags: Integer;
begin
FillChar(FileInfo, SizeOf(FileInfo), #0);
Flags := SHGFI_PIDL or SHGFI_SYSICONINDEX or SHGFI_ICON or SHGFI_IPENICON{或者 or SHGFI_LCARGEICON 或 or SHGFI_SMALLICON};
SHGetFileInfo(PChar(PIDL),
0,
FileInfo,
SizeOf(FileInfo),
Flags);
Result := FileInfo.iIcon;
end;
如果要根据扩展名取文件的图标,可以利用下面的函数:
uses
ShellApi ;
function GetAssociatedIcon ( const AExtension : string ; ASmall : Boolean ): HIcon ;
var
Info : TSHFileInfo ;
Flags : Cardinal ;
begin
if ASmall then
Flags := SHGFI_ICON or SHGFI_SMALLICON or SHGFI_USEFILEATTRIBUTES
else
Flags := SHGFI_ICON or SHGFI_LARGEICON or SHGFI_USEFILEATTRIBUTES ;
SHGetFileInfo ( PChar ( AExtension ), FILE_ATTRIBUTE_NORMAL , Info , SizeOf ( TSHFileInfo ), Flags );
Result := Info . hIcon ;
end ;
procedure TForm1 . Button1Click ( Sender : TObject );
begin
Icon . Handle := GetAssociatedIcon ( '.html' , True );
end ;