0

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.

T.J.
  • 57
  • 7
  • 1
    What you mean by _recursively going_ trough the res file?, I fail to see any recursion possible here. – jachguate Feb 01 '13 at 22:24
  • 1
    Perhaps *iteratively* (?) or something like it would be a more appropriate word?.. – Sertac Akyuz Feb 01 '13 at 22:26
  • 1
    Just pull them out in a loop and add them to your image list – David Heffernan Feb 01 '13 at 22:27
  • 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. Feb 02 '13 at 02:11
  • @T.J., so do you want to load all the files of the `RT_BITMAP` resource type into an image list ? – TLama Feb 02 '13 at 02:31

2 Answers2

5

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;
TLama
  • 75,147
  • 17
  • 214
  • 392
3

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;
ain
  • 22,394
  • 3
  • 54
  • 74