如何将资源编译到应用程序中并访问它们? [英] How I Compile Resources into my Application and Access them?

查看:106
本文介绍了如何将资源编译到应用程序中并访问它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何制作包含DLL和图像资源文件的单个executable软件包?

How can I make a single executable package that contains DLL and Image Resource Files?

那我如何在运行时从我的可执行文件中提取它们?

Then how do I extract them from my Executable at Runtime?

推荐答案

使用IDE(Delphi 2007或更高版本)的选项1:

您可以单击项目"菜单,然后选择资源...",可以将任何文件加载到其中.为了您的目的,这将是RC_DATA.

You can click the Project menu, then select Resources..., which you can load any file into. For your purpose this would be RC_DATA.

不带IDE的选项2

如果没有上述选项,则需要使用BRCC32(Borland资源编译器)从RC文件创建.RES文件,然后将其链接到您的应用程序.要在不使用IDE的情况下链接资源文件,请尝试以下操作:

If you do not have the above option, you will need to use the BRCC32 (Borland Resource Compiler) to create a .RES file from RC file, which you then link to your Application. To link Resource files without using the IDE, try the following:

例如,假设我们要添加几个DLL文件,并且DLL文件的名称为MyLib1.dll和MyLib2.dll,以添加此打开的记事本,然后键入以下内容:

Lets say for example we want to add a a couple of DLL files, and the name of the DLL files are MyLib1.dll and MyLib2.dll, to add this open Notepad, and type the following:

MYLIB1 RCDATA".. \ MyLib1.dll"

MYLIB1 RCDATA "..\MyLib1.dll"

MYLIB2 RCDATA".. \ MyLib2.dll"

MYLIB2 RCDATA "..\MyLib2.dll"

确保.. \ xxx.dll路径正确,因此显然您需要对其进行编辑.

Make sure the ..\xxx.dll paths are correct, so obviously you need to edit that.

现在您需要将其另存为.rc文件,因此请选择File> Save As ..(确保下拉过滤器为All Files .)并将其命名为MyResources.rc.现在,您需要使用资源控制台,通过以下控制台命令来生成Res文件:

Now you need to save this as a .rc file, so File>Save As..(make sure the dropdown filter is All Files .) and name it MyResources.rc. Now you need to use the Resource Compiler to generate the Res file, using this console command:

BRCC32 MyResources.RC

BRCC32 MyResources.RC

您可以使用命令提示符",开始"菜单>运行"> cmd.exe来编写该命令,或者在您的Delphi设置的bin文件夹中找到BRCC32.exe并将MyResource.RC文件拖到其中.

You can write that command by using the Command Prompt, Start Menu > Run > cmd.exe, alternatively you can find the BRCC32.exe inside the bin folder of your Delphi setup and drag the MyResource.RC file onto.

这将创建一个名为MyResources.RES的Res文件,您可以将其包含在Application的Main Delphi表单中,如下所示:

This will create a Res file named MyResources.RES which you can include inside the Main Delphi form of your Application, like so:

{$R *.dfm}
{$R MyResources.res} 

您可以使用以下方式提取资源:

procedure ExtractResource(ResName: String; Filename: String);
var
  ResStream: TResourceStream;
begin
  ResStream:= TResourceStream.Create(HInstance, ResName, RT_RCDATA);
  try
    ResStream.Position:= 0;
    ResStream.SaveToFile(Filename);
  finally
    ResStream.Free;
  end;
end;

这篇关于如何将资源编译到应用程序中并访问它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆