var
...
AFileInfo: TSHFileInfo;
procedure FormCreate(Sender: TObject);
begin
//在smallimages 中获取系统关联图标
SmallImages := TImageList.CreateSize(16,16);
SmallImages.ShareImages := True;
SmallImages.Handle :=
ShGetFileInfo('', FILE_ATTRIBUTE_NORMAL or FILE_ATTRIBUTE_DIRECTORY,
AFileInfo, SizeOf(AFileInfo),
SHGFI_SMALLICON or SHGFI_OPENICON or SHGFI_SYSICONINDEX);
end;
function GetIcon(AFileName: string; var AIconIndex: Integer;
var ATypeName: string; ISFolder: Boolean): Boolean;
const
Flags = SHGFI_SYSICONINDEX or SHGFI_USEFILEATTRIBUTES or SHGFI_TYPENAME;
var
Attrs: DWORD;
begin
Result := True;
if ISFolder then
Attrs := FILE_ATTRIBUTE_DIRECTORY
else
Attrs := FILE_ATTRIBUTE_NORMAL;
try
SHGetFileInfo(PChar(AFileName), Attrs, AFileInfo, SizeOf(AFileInfo), Flags);
AIconIndex := AFileInfo.iIcon;
ATypeName := AFileInfo.szTypeName;
except
Result := False;
end;
end;
use it
if GetIcon(<filename or dirpath>, AIndex, AType, False) then
begin
Item.ImageIndex := AIndex;
end;