Last active
January 15, 2017 12:17
-
-
Save ccy/9887916 to your computer and use it in GitHub Desktop.
Get icon for known file type
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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