开发者

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

开发者 https://www.devze.com 2023-04-11 14:40 出处:网络
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 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 n开发者_JAVA百科ow 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:

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

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

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

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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消