unit IconManager;
interface
uses
Windows, SysUtils, Controls, ShellApi, Generics.Collections, Functions;
type
TIconManager =
class
private
FDirIndex: Integer;
FImages: TImageList;
FData: TDictionary<
string, Integer>;
function FGetIconIndex(Extension:
string): Integer;
public
constructor Create;
destructor Destroy;
override;
property DirIndex: Integer
read FDirIndex;
property Images: TImageList
read FImages;
property Icon[Extension:
string]: Integer
read FGetIconIndex;
end;
implementation
{ TIconManager }
constructor TIconManager.Create;
var
SysIL: uint;
SFI: TSHFileInfoW;
begin
FData := TDictionary<
string, Integer>.Create();
FImages := TImageList.Create(
nil);
SysIL := SHGetFileInfoW('
', 0, SFI, SizeOf(SFI), SHGFI_ICON
or SHGFI_SMALLICON
or SHGFI_SYSICONINDEX);
if SysIL <> 0
then
begin
FImages.Handle := SysIL;
end;
SHGetFileInfoW(PChar(GetSystemDir), 0, SFI, SizeOf(TSHFileInfoW), SHGFI_ICON
or SHGFI_SMALLICON);
FDirIndex := SFI.iIcon;
end;
destructor TIconManager.Destroy;
begin
FImages.Free;
FData.Free;
inherited;
end;
function TIconManager.FGetIconIndex(Extension:
string): Integer;
var
SFI: TSHFileInfoW;
begin
if not FData.ContainsKey(Extension)
then
begin
SHGetFileInfoW(PChar(Extension), 0, SFI, SizeOf(TSHFileInfoW), SHGFI_ICON
or SHGFI_SMALLICON
or SHGFI_USEFILEATTRIBUTES);
FData.Add(Extension, SFI.iIcon);
end;
Result := FData[Extension];
end;
end.