Load resources (Images) on more projects

414 Views Asked by At

I have a component. In the component, I need to load dynamic TImageList.

This way I use today, but in all my projects, I need to add all the image, one at a time.

function NavigatorImages: TImageList;
var
   vIcon: TBitmap;
   I: Integer;
begin
   if FNavigatorImages = nil then
   begin
      vIcon := TBitmap.Create;
      try
         FNavigatorImages := TImageList.Create (nil);
         FNavigatorImages.Height := 16;
         FNavigatorImages.Width := 16;
         for I := 0 to 11 do
         begin
            vIcon.LoadFromResourceName (HInstance, 'IMAGE_' + IntToStr(I));
            FNavigatorImages.AddMasked (vIcon, vIcon.Canvas.Pixels[0,        FNavigatorImages.Height - 1]);
         end;
      finally
         vIcon.Free;
      end;
   end;
   Result := FNavigatorImages;
end;

How do I create a single resource file, Which for example, contains multiple images can be loaded into the component without adding resources in each project. You have some way?

1

There are 1 best solutions below

1
GabrielF On

You'll have to add it to every project you want to use it, but there are ways to make the task easier.

  1. Create the .res file using a .rc file and the brcc tool:

    brcc32 -fo output.res input.rc
    
  2. Add the same .res file to every project you want to use those images (Project >> Add to Project or just edit .dpr to add {R 'path/filename.res'}. If you have a common unit to all these projects (which is the case if that component you talk about is a custom one), you could also add the .res file to that unit and the resources will be loaded to all of them.

  3. It's also worth noting that if you add your resources specifying both the .res and the .rc ({$R 'output.res' 'input.rc'}), Delphi will compile your resource file automatically for you (I couldn't determine exactly when it decides to do that, but if you compile your projects a couple of times, the resources files will certainly get compiled too).

Sample .rc file:

IMAGE_1 BITMAP Path\Image1.bmp

The second parameter is the Resource-Definition Statement.

Of course, if any of those images change, you'll have to recompile the .rc file manually.