如何使用Delphi从另一个文件的资源加载单个图标?

发布于 2024-12-09 01:41:30 字数 1032 浏览 1 评论 0原文

我想加载一个图标(来自另一个文件),该图标没有嵌入多个图标(它不是图标组)。 我不知道它的大小。 我现在使用此代码来检索图标的句柄并将其与 TIcon.Handle 一起使用:

function ResourceToIconHandle(hFile: hModule; IDname: PChar): HICON;
var
   hGicon1,
   hLoadIcon1: THandle;
   pGIcon1: Pointer;
begin
   hGicon1 := FindResource(hFile, IDName, RT_ICON);
   if hGicon1 <> 0 then
   begin
      hLoadIcon1 := LoadResource(hFile, hGicon1);
      pGicon1 := LockResource(hLoadIcon1);
      Result := CreateIconfromResource(pGicon1,
           SizeofResource(hFile, hGicon1),
           True,
           $00030000);
   end;
end;

是的,它只是代码的一部分(如果您愿意,我将显示全部)。 它只适用于一个问题:CreateIconfromResource 函数给我任何拉伸为 32x32 的图标:

1

但我想以原始分辨率获取图标: 2

我知道 CreateIconfromResource 的设计目的是让它们具有相同的分辨率,这就是我正在寻找另一个函数的原因。 感谢您的帮助。

I want to load an icon (from another file) which doesn't have multiple icons embedded in it (it's not an icon group).
I don't know its size.
I use now this code to retrieve the handle of the icon and use it with a TIcon.Handle:

function ResourceToIconHandle(hFile: hModule; IDname: PChar): HICON;
var
   hGicon1,
   hLoadIcon1: THandle;
   pGIcon1: Pointer;
begin
   hGicon1 := FindResource(hFile, IDName, RT_ICON);
   if hGicon1 <> 0 then
   begin
      hLoadIcon1 := LoadResource(hFile, hGicon1);
      pGicon1 := LockResource(hLoadIcon1);
      Result := CreateIconfromResource(pGicon1,
           SizeofResource(hFile, hGicon1),
           True,
           $00030000);
   end;
end;

Yes, it's only a part of the code (if you want I'll show all).
It works with only one problem: CreateIconfromResource function is giving me any icon streched at 32x32:

1

But I want to get the icons at their original resolution:
2

I know that CreateIconfromResource is designed to get them at the same resolution, that's why I'm looking for another function.
Thank you for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青瓷清茶倾城歌 2024-12-16 01:41:30

使用 CreateIconFromResourceEx 而不是 CreateIconFromResource

CreateIconFromResourceEx 允许您提供所需的宽度/高度,而 CreateIconFromResource 使用默认系统指标(例如 LR_DEFAULTSIZE 中的解释):

使用系统指标值指定的宽度或高度
光标或图标,如果 cxDesired 或 cyDesired 值设置为
零。如果未指定此标志并且 cxDesired 和 cyDesired
设置为零,该函数使用实际资源大小。如果
资源包含多个图像,该函数使用的大小
第一张图片。

Use CreateIconFromResourceEx instead of CreateIconFromResource.

CreateIconFromResourceEx lets you provide desired width/height, while CreateIconFromResource is using default system mertics for those (such as explained for LR_DEFAULTSIZE):

Uses the width or height specified by the system metric values for
cursors or icons, if the cxDesired or cyDesired values are set to
zero. If this flag is not specified and cxDesired and cyDesired are
set to zero, the function uses the actual resource size. If the
resource contains multiple images, the function uses the size of the
first image.

您的好友蓝忘机已上羡 2024-12-16 01:41:30

Roman R. 可能是对的,但我还补充说,您必须在设置 TIcon 对象的句柄之前设置其正确的尺寸。

Roman R. is probably right, but I also add that you must set proper dimensions of TIcon object before setting its Handle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文