Delphiman
Delphiman

Reputation: 373

Fix Duplicate resource: Type 2 (BITMAP) and load file from resource file

I would just find an issue with yours. I have a resource file named "JBCompRes.Res". This file have two images that are used on components in certains buttons creation.

These images are used in more than one component, I created a specific folder in my framework where are stored all the resource files.

To load the image from the resource file I use the method: "LoadFromResourceName" as the code example below:

unit MyCalcEdit;

Uses SysUtils , MyResources ;

{$R JBCompRes.RES } // my resource file

{TCalcEdit}

TCalcEdit constructor Create ( AOwner : TComponent ) . ;
Begin
  Inherited ;
  BeepOnEnter : = False ;
  Style.BorderStyle : = ebs3D ;
  Properties.ButtonGlyph.LoadFromResourceName ( HInstance , CResCalculadoraBmp ) ;
end ;



unit MyDBCalcEdit;

Uses SysUtils , MyResources ;

{$R JBCompRes.RES } / / my resource file

{TDBCalcEdit}

constructor TDBCalcEdit.Create(AOwner : TComponent);
Begin
  Inherited ;
  BeepOnEnter : = False ;
  Style.BorderStyle : = ebs3D ;
  Properties.ButtonGlyph.LoadFromResourceName ( HInstance , CResCalculadoraBmp ) ;
end;

In units of the components, I need to reference the name of the resource file for the method to load resources work correct, but if I compile a project that delphi use these components is the message duplication of resources, as follows:

[DCC Warning] W1056 Warning: Duplicate resource: Type 2 (BITMAP), ID BB_NOBMP; File C:\MyFrameWorkTest\Resources\JBCompRes.RES resource kept; file C:\MyFrameWorkTest\Resources\JBCompRes.RES resource discarded.

Notice that my "JBCompRes.Res" is only in one place , in case the "C:\MyFrameWorkTest\ Resources\" folder .

I identified how to do it does not produce the problem , but if I do not I refer to the resource file in the unit where you want to call it a feature not found .

Any ideas on the issue .

Already grateful if you can help .

Upvotes: 1

Views: 1129

Answers (2)

Delphiman
Delphiman

Reputation: 373

After several tests and simulations have concluded that you can not share the same resource in different units without generated warnings of duplicate resources.

I may be wrong, but have not found a way to do this .

The only way I found was to create a specific resource file to use that unit .

For example , my JBCalcEdit component will have a JBCalcEditRes.Res. This file contain images are used internally. My component JBPrint will have a JBPrintRes.Res and so on.

Example working:

Unit UnJBCalcEdit;

Interface

Uses Classes;

Type
  .......

Implementation

Uses SysUtils;

Const
  CImgPadraoJBCalcEdit = 'ImgPadraoJBCalcEditBmp';

  {$R UnJBCalcEdit.RES} //meu arquivo de recurso

{ TJBCalcEdit }

Constructor TJBCalcEdit.Create(AOwner: TComponent);
Begin
  Inherited;
  BeepOnEnter := False;
  Style.BorderStyle := ebs3D;
Properties.ButtonGlyph.LoadFromResourceName(HInstance, CImgPadraoJBCalcEdit);
End;

Thus, it is guaranteed that the resource will not be repeated and will be used correctly .

Analyzing other complex components like DevExpress, they also work this way .

Regarts.

Upvotes: 0

David Heffernan
David Heffernan

Reputation: 613441

The compiler is telling you that you are linking two resources with the same name. And the code you present confirms that the compiler is correct.

You should change your code so that you only link the resource once.

I think you are confused a little. I suspect that you believe that you need to link the resource in every file that loads the resource. That is not the case. Resources are shared within a module. So long as the resource is linked once to the module, it can be loaded from as many different units as you like.

If I were you I would create a simple shared unit that contains constants defining the resource names, and includes all the $R directives that linked the resources. That shared unit would be the only place that linked resources with $R. In fact it looks as though you are already most of the way there with your unit named MyResources. You just need to move the $R directives down to that unit.

Upvotes: 2

Related Questions