0
votes

I have created a resource file for a Delphi 2007 application. The resource files contains 10 Bitmap entries. I was wondering if there was a way to load all of the bitmaps into an Imagelist by recursively going through the resource file or do I have to pull them out one at a time.

Thanks in advance.

2
What you mean by recursively going trough the res file?, I fail to see any recursion possible here.jachguate
Perhaps iteratively (?) or something like it would be a more appropriate word?..Sertac Akyuz
Just pull them out in a loop and add them to your image listDavid Heffernan
I was thinking that I didn't want to identify all of the file names. I was not sure if I could bring it into a list like a text file going line by line until I reached the end of the file.T.J.
@T.J., so do you want to load all the files of the RT_BITMAP resource type into an image list ?TLama

2 Answers

5
votes

To add all RT_BITMAP resource type images from the current module to an image list I would use this:

uses
  CommCtrl;

function EnumResNameProc(hModule: HMODULE; lpszType: LPCTSTR; lpszName: LPTSTR;
  lParam: LONG_PTR): BOOL; stdcall;
var
  BitmapHandle: HBITMAP;
begin
  Result := True;
  BitmapHandle := LoadBitmap(HInstance, lpszName);
  if (BitmapHandle <> 0) then
  begin
    ImageList_Add(HIMAGELIST(lParam), BitmapHandle, 0);
    DeleteObject(BitmapHandle);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  EnumResourceNames(HInstance, RT_BITMAP, @EnumResNameProc,
    LONG_PTR(ImageList1.Handle));
end;
3
votes

I'm guessing that with "recursively going through the resource file" you want to ask is it possible to load the resources without knowing their name. For that there is class of API functions which allow you to enumerate resources in given module. See the "Resource Overviews, Enumerating Resources" topic for more info on that.

However, since you embedd the bitmaps into the exe yourself it is much easier to give them names which allow easy iteration, ie, in RC file:

img1 BITMAP foo.bmp
img2 BITMAP bar.bmp

Here name "pattern" is img + number. Now it is easy to load the images in a loop:

var x: Integer;
    ResName: string;
begin
  x := 1;
  ResName := 'img1';
  while(FindResource(hInstance, PChar(ResName), RT_BITMAP) <> 0)do begin
     // load the resource and do something with it
     ...
     // name for the next resource
     Inc(x);
     ResName := 'img' + IntToStr(x);
  end;