gpt4 book ai didi

delphi - 替代 SHGetFileInfoW 函数

转载 作者:行者123 更新时间:2023-12-02 15:24:48 26 4
gpt4 key购买 nike

我在使用 SHGetFileInfoW 函数时遇到问题。

这是一个相当慢的过程,启动时的首次读取(初始化)需要 100 毫秒。

在 MSDN 中,它应该从线程读取,而不是主线程,因为它可能会卡住进程。

我想使用其他功能(如果有的话)来读取图标。

另一件事。如何读取大图标,目前我最多可以读取 32x32 (SHGFI_LARGEICON)

谢谢!

实际代码:

procedure TForm1.LoadIcons;
var
Info: TShFileInfo;
Icon: TIcon;
Flags: UINT;
FileName: PAnsiChar;

begin
FileName := '.txt';
Flags := SHGFI_USEFILEATTRIBUTES or SHGFI_ICON or SHGFI_LARGEICON;
Icon := TIcon.Create;
try
SHGetFileInfo(FileName, FILE_ATTRIBUTE_NORMAL, Info,
SizeOf(Info), Flags);
Icon.Handle := Info.hIcon;
Image1.Picture.Assign(Icon);
Image1.Refresh;
finally
DestroyIcon(Info.hIcon);
Icon.Free;
end;
end;

最佳答案

您可以从注册表中找到给定文件扩展名的 DefaultIcon 并使用 ExtractIconExHere is an example

我不知道它是否比SHGetFileInfo更快

编辑:

我已经(从示例中)提取了从扩展中获取图标的部分。它实际上工作得非常快。还可以进一步优化。(我稍微修改了代码):

// find the icon for a certain file extension in the registry
function TForm1.RegistryIconExtraction(Extension : string): integer;
var
RegKey : TRegistry;
IconPos : integer;
AssocAppInfo : string;
ExtractPath, FileName : string;
IconHandle, PLargeIcon, PSmallIcon : HICON;
AnIcon : TIcon;

begin
Result := 0; // default icon

if Extension[1] <> '.' then Extension := '.' + Extension;

RegKey := TRegistry.Create(KEY_READ);
try
// KEY_QUERY_VALUE grants permission to query subkey data.
RegKey.RootKey := HKEY_CLASSES_ROOT; // set folder for icon info lookup
if RegKey.OpenKeyReadOnly(Extension) then // extension key exists?
try
AssocAppInfo := RegKey.ReadString(''); // read app key
RegKey.CloseKey;
except
Exit;
end;
if ((AssocAppInfo <> '') and // app key and icon info exists?
(RegKey.OpenKeyReadOnly(AssocAppInfo + '\DefaultIcon'))) then
try
ExtractPath := RegKey.ReadString(''); // icon path
RegKey.CloseKey;
except
Exit;
end;
finally
RegKey.Free;
end;

// IconPos after comma in key ie: C:\Program Files\Winzip\Winzip.Exe,0
// did we get a key for icon, does IconPos exist after comma seperator?
If ((ExtractPath <> '') and (pos(',', ExtractPath) <> 0)) then
begin

// Filename in registry key is before the comma seperator
FileName := Copy(ExtractPath, 1, Pos(',', ExtractPath) - 1);
// extract the icon Index from after the comma in the ExtractPath string
try
IconPos := StrToInt(copy(ExtractPath, Pos(',', ExtractPath) + 1,
Length(ExtractPath) - Pos(',', ExtractPath) + 1));
except
Exit;
end;

IconHandle := ExtractIconEx(PChar(FileName), IconPos, PLargeIcon, PSmallIcon, 1);

If (PLargeIcon <> 0) then
begin
AnIcon := TIcon.Create;
AnIcon.Handle := PLargeIcon;

Image1.Picture.Assign(AnIcon);
Image1.Refresh;

AnIcon.Free;
end;

DestroyIcon(PLargeIcon);
DestroyIcon(PSmallIcon);
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
t1, t2: DWORD;
begin
t1 := GetTickCount;
RegistryIconExtraction('.txt');
t2 := GetTickCount;
Memo1.Lines.Add(IntToStr(t2-t1));
end;

EDIT2:示例代码现在符合 Vista/Win7 UAC 标准。

关于delphi - 替代 SHGetFileInfoW 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8852692/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com