tags:

views:

143

answers:

2

Hi guys,

I', trying to load an icon from a res file into an image list. I created the res file with the delphi ImageEditor.

And this way I'm trying to load the icon:

  //if ImageList1.ResourceLoad(rtIcon, 'TEXT_BOLD', clWhite) then
  if imagelist1.GetResource(rtIcon, 'TEXT_BOLD', 0, [lrDefaultColor], clRed) then
    showmessage('loaded')
  else
    showmessage('not loaded');

Both ways doesn't work. Any ideas? Thanks!

A: 

I solved it with using the windows functions directly:

...
var 
  myicon : Ticon;
  Hd: THandle;
begin

  Hd := LoadImage(HInstance, 'TEXT_BOLD', IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR);
  myicon := TIcon.Create;
  myicon.ReleaseHandle;
  myicon.Handle := Hd;
  FImageList.AddIcon(myicon);

end;
...
ben
Rob's answer is a better way. Your code isn't doing any error checking at all, and isn't freeing the icon you create.
Ken White
+3  A: 

The ResourceLoad and GetResource methods load the entire image list from the single specified image resource. The intent is that you would have a single bitmap holding all the images meant to go in the list. The control then slices it up into separate tiles based on the image list's configured width and height.

With that in mind, you might have expected the image list to simply load your icon and be left with just that single image in the list. But image lists are only allowed to load bitmap resources. They won't load icon resources. (The resource-type parameter is there to leave open the possibility of some future expansion of its capabilities.) See the ImageList_LoadImage API function for details.

It looks like you didn't want to load the entire image list anyway. It appears you wished to append an icon to the list of images already in the list. In that case, your method calling LoadImage is fine. TIcon knows how to load things from resources itself, so your code can be a little more streamlined:

myicon := TIcon.Create;
try
  myicon.LoadFromResourceName(HInstance, 'TEXT_BOLD');
  FImageList.AddIcon(myicon);
finally
  myicon.Free;
end;
Rob Kennedy