使用 Windows Shell 获取字体文件图标:
代码 :
var
psfDeskTop : IShellFolder;
psfFont : IShellFolder;
pEnumList : IEnumIdList;
pidFont : PItemIdList;
pidChild : PItemIdList;
FontPath : array[0..MAX_PATH - 1] of Char;
IconFile : array[0..MAX_PATH - 1] of Char;
pchEaten, dwAttributes, ItemsFetched : ULONG;
ExtractIcon : IExtractIcon;
IconIndex : Integer;
Flags : DWORD;
Icon : TIcon;
LH, SH : HICon;
begin
FillChar(FontPath, sizeof(FontPath), #0);
//get C:\Windows\Fonts
SHGetSpecialFolderPath(0, FontPath, CSIDL_FONTS, False);
SHGetDesktopFolder(psfDeskTop);
psfDeskTop.ParseDisplayName(0, nil, FontPath, pchEaten, pidFont,
dwAttributes);
//get font folder's interface
psfDeskTop.BindToObject(pidFont, nil, IID_IShellFolder, psfFont);
//Enumerate
psfFont.EnumObjects(0, SHCONTF_FOLDERS or SHCONTF_NONFOLDERS or
SHCONTF_INCLUDEHIDDEN, pEnumList);
ItemsFetched := 0;
while pEnumList.Next(1, pidChild, ItemsFetched) = NO_ERROR do
begin
psfFont.GetUIObjectOf(0, 1, pidChild, IID_IExtractIconW, nil,
Pointer(ExtractIcon));
Flags := 0;
LH := 0;
SH := 0;
if Assigned(ExtractIcon) then
begin
IconIndex := 0;
Icon := TIcon.Create;
ExtractIcon.GetIconLocation(0, @IconFile, MAX_PATH, IconIndex,
Flags);
if (IconIndex < 0) or ((Flags and GIL_NOTFILENAME) = 0) then
ExtractIconEx(@IconFile, IconIndex, LH, SH, 1)
else
ExtractIcon.Extract(@IconFile, IconIndex, LH, SH, MakeLong(32,
16));
//get font file icon's handle LS for large icon , SH for small icon
//do something u want
end;
end;
end;