Add resource file by code with Delphi

久未见 提交于 2019-12-13 04:24:01

问题


I have a custom res file "myres.res". In this moment I use it on my application, I have add {$R myres.res} under {$R *.res} line inside my DPR project file and it work very well.

Now I'd like creare a VCL component with a boolean property "UseCustomRes". If I set UseCustomRes=True I'd like add the res file when I compile my project but if I set UseCustomRes=False I don't want use res file when I compile my project.

Is this possible? I don't know if it possible and how it is possible.


回答1:


Resources are included by the linker based on the existence of special $RESOURCE directives. These directives cannot be switched based on the value of an object instance's property.

So, with the built in tooling there is now way to achieve what you desire. What you will need to do is to add a post-build step that modifies the output file by adding the resource if needed. A good example of a tool which does exactly this is madExcept.




回答2:


Picking a resource at runtime
If you want to use a resource (or not) based on a runtime variable you'll have to compile it in always, otherwise you'll lose the option of using it at runtime.

Whilst running you can access a resource using TResourceStream.

Here's an example:

procedure ExtractResource;
var
  rs: TResourceStream;
begin
  rs:= TResourceStream.Create(HInstance, 'NameOfResource', RT_RCDATA);
  try
    rs.Position:= 0;
    rs.DoSomethingWithTheResource...
  finally
    rs.Free;
  end;
end;

Here's the online help: http://docwiki.embarcadero.com/Libraries/XE2/en/System.Classes.TResourceStream
Note that the help entry for TResourceStream is a bit broken; it does not show all methods.
The missing methods are here: http://docwiki.embarcadero.com/Libraries/XE2/en/System.Classes.TStream_Methods

Picking a resource at compile time
Note that the {$R *.res} line includes any .res file in the current directory.
If you want to select a specific .res file, you'll have to exclude this line.
Conditional compilation is done using defines, like so:

 implementation
   {.R *.res}  //disable the *.res inclusion.
   {$IFDEF GetMyResource}
     {$R MyResource.res}   //will only be compiled if `GetMyResource` is defined
   {$ENDIF}
   {$R AlwaysIncludeThisResource.res} //will always be included.

You then define the symbol GetMyResource in the Conditional defines under project options, see here: https://stackoverflow.com/a/4530320/650492



来源:https://stackoverflow.com/questions/19480596/add-resource-file-by-code-with-delphi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!