If 48x48 or 64x64 icons are present in the Vista Shell how can you get the handle to display one in a TImage using SHGetFileInfo?
I\'d like to select an icon from a
We found that the index of the file was not correct because the incorrect icon was shown during testing of the code posted by RRUZ. The GetIconFromFile method was setting the index baised on the image count. We changed GetIconFromFile to use the SFI index ( aIndex := SFI.iIcon ) and the correct icon was obtained. Apparently the shellimagelist is constantly changing so the index was incorrect.
Thanks to all to assisted. This seems like a very good piece of code now.
procedure GetIconFromFile( aFile: string; var aIcon: TIcon;SHIL_FLAG: Cardinal );
var
aImgList: HIMAGELIST;
SFI: TSHFileInfo;
aIndex: integer;
begin // Get the index of the imagelist
SHGetFileInfo( PChar( aFile ), FILE_ATTRIBUTE_NORMAL, SFI, SizeOf( TSHFileInfo ),
SHGFI_ICON or SHGFI_LARGEICON or SHGFI_SHELLICONSIZE or SHGFI_SYSICONINDEX or SHGFI_TYPENAME or SHGFI_DISPLAYNAME );
if not Assigned( aIcon ) then
aIcon := TIcon.Create;
// get the imagelist
aImgList := GetImageListSH( SHIL_FLAG );
// get index
//aIndex := Pred( ImageList_GetImageCount( aImgList ) );
aIndex := SFI.iIcon;
// extract the icon handle
aIcon.Handle := ImageList_GetIcon( aImgList, aIndex, ILD_NORMAL );
end;