简体   繁体   English

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

[英]How to load a single icon from the resources of another file using Delphi?

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: 我现在使用此代码来检索图标的句柄并将其与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: 它仅适用于一个问题:CreateIconfromResource函数给我任何以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. 我知道CreateIconfromResource旨在使它们处于相同的分辨率,这就是我正在寻找另一个功能的原因。 Thank you for your help. 谢谢您的帮助。

Use CreateIconFromResourceEx instead of CreateIconFromResource . 使用CreateIconFromResourceEx而不是CreateIconFromResource

CreateIconFromResourceEx lets you provide desired width/height, while CreateIconFromResource is using default system mertics for those (such as explained for LR_DEFAULTSIZE ): CreateIconFromResourceEx允许您提供所需的宽度/高度,而CreateIconFromResource使用默认的系统mertics(如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. 如果cxDesired或cyDesired值设置为零,则使用光标或图标的系统度量值指定的宽度或高度。 If this flag is not specified and cxDesired and cyDesired are set to zero, the function uses the actual resource size. 如果未指定此标志且cxDesired和cyDesired设置为零,则该函数使用实际资源大小。 If the resource contains multiple images, the function uses the size of the first image. 如果资源包含多个图像,则该函数使用第一个图像的大小。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM