Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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.

like image 531
DavidB Avatar asked Oct 09 '11 14:10

DavidB


1 Answers

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.

like image 170
Roman R. Avatar answered Nov 08 '22 23:11

Roman R.