Reputation: 57
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.
Upvotes: 0
Views: 588
Reputation: 76693
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;
Upvotes: 5
Reputation: 22749
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;
Upvotes: 3