Skip to content

Instantly share code, notes, and snippets.

@ccy
Last active January 15, 2017 12:17
Show Gist options
  • Save ccy/9887916 to your computer and use it in GitHub Desktop.
Save ccy/9887916 to your computer and use it in GitHub Desktop.
Get icon for known file type
uses Winapi.ShellAPI;
function GetIconByFileExt(aFileExt: string): TBytes;
var C: TIcon;
N: SHFILEINFO;
S: TBytesStream;
begin
C := TIcon.Create;
try
Win32Check(Succeeded(
SHGetFileInfo(
PChar(aFileExt),
FILE_ATTRIBUTE_NORMAL,
N,
SizeOf(N),
SHGFI_ICON or SHGFI_SMALLICON or
SHGFI_SYSICONINDEX or SHGFI_USEFILEATTRIBUTES
)
));
C.Handle := N.hIcon;
S := TBytesStream.Create;
try
C.SaveToStream(S);
Result := S.Bytes;
finally
S.Free;
end;
Win32Check(DestroyIcon(N.hIcon));
finally
C.Free;
end;
end;
var S: TStream;
begin
S := TBytesStream.Create(GetIconByFileExt('pdf'));
try
Image1.Picture.Icon.LoadFromStream(S);
finally
S.Free;
end;
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment