By the file extension, we get the icon associated with file type and place it in Timage.
1 2 3 4 5 6 7 8 9 10 11 12 |
Uses Winapi.ShellAPI; ... var fileExt : string; icoSize: Integer; begin fileExt:= '.html'; icoSize:= SHGFI_SMALLICON; Img1.Picture.Icon.Handle:= getIconByExtension(fileExt, icoSize); end; |
Options
filePath - path to the file
icoSize - icon size
- SHGFI_SMALLICON - 16x16
- SHGFI_LARGEICON - 32x32
Result
A handle to the icon, or 0 on failure.
1 2 3 4 5 6 7 8 9 10 11 |
function getIconByExtension(ext: string; icoSize: integer): HICON; var shfi: TShFileInfo; icon: TIcon; test_file: array[1..510] of Char; begin try StrPCopy(@test_file, ext + #0); SHGetFileInfo(@test_file, 0, shfi, SizeOf(shfi), SHGFI_ICON or icoSize or SHGFI_USEFILEATTRIBUTES); Result:= shfi.hIcon; except Result:= 0; end; end; |