Search code examples
delphidelphi-2007

Recursively Import From Resource File Delphi 2007


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.


Solution

  • 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;