delphi 嵌入java_如何将媒体文件嵌入到Delphi可执行文件(RC / .RES)中

delphi 嵌入java

Games and other types of applications which use multimedia files like sounds and animations must either distribute the extra multimedia files along with the application or embed the files within the executable.

使用诸如声音和动画之类的多媒体文件的游戏和其他类型的应用程序必须与应用程序一起分发额外的多媒体文件,或者将文件嵌入可执行文件中。

Rather than distribute separate files for your application's use, you can add the raw data to your application as a resource. You can then retrieve the data from your application when it is needed. This technique is generally more desirable because it can keep others from manipulating those add-in files.

可以分发原始数据作为资源,而不是分发单独的文件供应用程序使用。 然后,您可以在需要时从应用程序中检索数据。 通常更希望使用此技术,因为它可以阻止其他人操纵这些加载项文件。

This article will show you how to embed (and use) sound files, video clips, animations and more generally any kind of binary files in a Delphi executable. For the most general purpose, you'll see how to put an MP3 file inside a Delphi exe.

本文将向您展示如何在Delphi可执行文件中嵌入(和使用)声音文件,视频剪辑,动画以及更一般的任何类型的二进制文件 。 为了最一般的目的,您将看到如何将MP3文件放入Delphi exe中。

资源文件(.RES) ( Resource Files (.RES) )

In the "Resource Files Made Easy" article you were presented with several examples of the use of bitmaps, icons, and cursors from resources. As stated in that article we can use the Image Editor to create and edit resources that consist of such types of files. Now, when we are interested in storing various types of (binary) files inside a Delphi executable we'll have to deal with resource script files (.rc), the Borland Resource Compiler tool and other.

在“使资源文件变得容易 ”一文中,向您展示了使用资源中的位图,图标和光标的几个示例。 如该文章所述,我们可以使用图像编辑器来创建和编辑由此类文件组成的资源。 现在,当我们有兴趣在Delphi可执行文件中存储各种类型的(二进制)文件时,我们将不得不处理资源脚本文件(.rc), Borland Resource Compiler工具等。

Including several binary files in your executable consists of 5 steps:

在可执行文件中包括几个二进制文件,包括5个步骤:

  1. Create and/or collect all the files you wish to put in an exe.

    创建和/或收集您希望放入exe的所有文件。
  2. Create a resource script file (.rc) that describes those resources used by your application,

    创建一个资源脚本文件(.rc),该文件描述应用程序使用的那些资源,
  3. Compile the resource script file (.rc) file to create a resource file (.res),

    编译资源脚本文件(.rc)文件以创建资源文件(.res),
  4. Link the compiled resource file into the application’s executable file,

    将已编译的资源文件链接到应用程序的可执行文件中,
  5. Use individual resource element.

    使用单个资源元素。

The first step should be simple, simply decide what types of files you would like to store in your executable. For example, we will store two .wav songs, one .ani animations and one .mp3 song.

第一步应该很简单,只需确定要存储在可执行文件中的文件类型即可。 例如,我们将存储两首.wav歌曲,一首.ani动画和一首.mp3歌曲。

Before we move on, here are a few important statements concerning limitations when working with resources:

在继续之前,这里有一些关于使用资源时的局限性的重要声明:

  • Loading and unloading resources is not a time-consuming operation. Resources are part of the applications executable file and are loaded at the same time the application runs.

    加载和卸载资源不是一项耗时的操作。 资源是应用程序可执行文件的一部分,并且在应用程序运行的同时加载。
  • All the (free) memory can be used when loading/unloading resources. In other words, there are no limits on the number of resources loaded at the same time.

    加载/卸载资源时,可以使用所有(空闲)内存。 换句话说,对同时加载的资源数量没有限制。
  • Of course, resource files do double the size of an executable. If you want smaller executables, consider placing resources and parts of your project in a dynamic link library (DLL) or its more specialized variation.

    当然,资源文件的大小是可执行文件的两倍。 如果您需要较小的可执行文件,请考虑将项目的资源和部分放置在动态链接库(DLL)或更专业的版本中

Let's now see how to create a file that describes resources.

现在让我们看看如何创建一个描述资源的文件。

创建资源脚本文件(.RC) ( Creating a Resource Script File (.RC) )

A resource script file is a just a simple text file with the extension .rc that lists resources. The script file is in this format:

资源脚本文件只是一个扩展名为.rc的简单文本文件,其中列出了资源。 脚本文件的格式如下:


ResName2 ResTYPE2 ResFileName2
ResName2 ResTYPE2 ResFileName2
...
...
ResNameX ResTYPEX ResFileNameX
ResNameX ResTYPEX ResFileNameX
...
...

RexName specifies either a unique name or an integer value (ID) that identifies the resource. ResType describes the type of resource and the ResFileName is the full path and file name to the individual resource file.

RexName指定标识资源的唯一名称或整数值(ID)。 ResType描述资源的类型, ResFileName是单个资源文件的完整路径和文件名。

To create a new resource script file, simply do the following:

要创建新的资源脚本文件,只需执行以下操作:

  1. Create a new text file in your projects directory.

    在您的项目目录中创建一个新的文本文件。
  2. Rename it to AboutDelphi.rc.

    将其重命名为AboutDelphi.rc。

In the AboutDelphi.rc file, have the following lines:

在AboutDelphi.rc文件中,包含以下几行:


MailBeep WAVE "c:\windows\media\newmail.wav"
MailBeep WAVE“ c:\ windows \ media \ newmail.wav”
Cool AVI cool.avi
酷AVI cool.avi
Intro RCDATA introsong.mp3
RCDATA简介introsong.mp3

The script file simply defines resources. Following the given format the AboutDelphi.rc script lists two .wav files, one .avi animation, and one .mp3 song. All statements in a .rc file associate an identifying name, type and file name for a given resource. There are about a dozen predefined resource types. These include icons, bitmaps, cursors, animations, songs, etc. The RCDATA defines generic data resources. RCDATA let you include a raw data resource for an application. Raw data resources permit the inclusion of binary data directly in the executable file. For example, the RCDATA statement above names the application’s binary resource Intro and specifies the file introsong.mp3, which contains the song for that MP3 file.

脚本文件仅定义资源。 按照给定的格式,AboutDelphi.rc脚本列出了两个.wav文件,一个.avi动画和一首.mp3歌曲。 .rc文件中的所有语句都关联给定资源的标识名称,类型和文件名。 大约有十二种预定义的资源类型。 这些包括图标,位图,光标,动画,歌曲等。RCDATA定义了通用数据资源。 RCDATA使您可以包括应用程序的原始数据资源。 原始数据资源允许将二进制数据直接包含在可执行文件中。 例如,上面的RCDATA语句为应用程序的二进制资源Intro命名,并指定文件introsong.mp3,其中包含该MP3文件的歌曲。

Note: make sure you have all the resources you list in your .rc file available. If the files are inside your projects directory you don't have to include the full file name. In my .rc file .wav songs are located *somewhere* on the disk and both the animation and MP3 song are located in the project's directory.

注意:确保在.rc文件中列出的所有资源都可用。 如果文件在您的项目目录中,则不必包含完整的文件名。 在我的.rc文件中,.wav歌曲位于磁盘上的“某处”,并且动画和MP3歌曲均位于项目的目录中。

创建资源文件(.RES) ( Creating a Resource File (.RES) )

To use the resources defined in the resource script file, we must compile it to a .res file with the Borland's Resource Compiler. The resource compiler creates a new file based on the contents of the resource script file. This file usually has an .res extension. The Delphi linker will later reformat the .res file into a resource object file and then link it to the executable file of an application.

要使用资源脚本文件中定义的资源,我们必须使用Borland的资源编译器将其编译为.res文件。 资源编译器根据资源脚本文件的内容创建一个新文件。 该文件通常具有.res扩展名。 Delphi链接器稍后会将.res文件重新格式化为资源对象文件,然后将其链接到应用程序的可执行文件。

The Borland's Resource Compiler command line tool is located in the Delphi Bin directory. The name is BRCC32.exe. Simply go to the command prompt and type brcc32 then press Enter. Since the Delphi\Bin directory is in your Path the Brcc32 compiler is invoked and displays the usage help (since it was called with no parameters).

Borland的Resource Compiler命令行工具位于Delphi Bin目录中。 名称为BRCC32.exe。 只需转到命令提示符并键入brcc32,然后按Enter。 由于Delphi \ Bin目录位于您的路径中,因此将调用Brcc32编译器并显示使用帮助(因为没有参数就调用了该帮助)。

To compile the AboutDelphi.rc file to a .res file execute this command at the command prompt (in the projects directory):

要将AboutDelphi.rc文件编译为.res文件,请在命令提示符下(在projects目录中)执行以下命令:

BRCC32 AboutDelphi.RC
BRCC32关于Delphi.RC

By default, when compiling resources, BRCC32 names the compiled resource (.RES) file with the base name of the .RC file and places it in the same directory as the .RC file.

默认情况下,在编译资源时,BRCC32用.RC文件的基本名称命名已编译资源(.RES)文件,并将其放置在与.RC文件相同的目录中。

You can name the resource file anything you want, as long as it has the extension ".RES" and the filename without the extension is not the same as any unit or project filename. This is important because, by default, each Delphi project that compiles into an application has a resource file with the same name as the project file, but with the extension .RES. It's best to save the file to the same directory as your project file.

您可以将资源文件命名为任意名称,只要它的扩展名为“ .RES”,且不带扩展名的文件名与任何单位或项目的文件名都不相同。 这很重要,因为默认情况下,每个编译成应用程序的Delphi项目都具有一个与项目文件同名的资源文件,但扩展名为.RES。 最好将文件保存到项目文件所在的目录中。

包括(链接/嵌入)资源到可执行文件 ( Including (Linking/Embedding) Resources to Executables )

After the .RES file is linked to the executable file, the application can load its resources at run time as needed. To actually use the resource, you'll have to make a few Windows API calls.

将.RES文件链接到可执行文件后,应用程序可以根据需要在运行时加载其资源。 要实际使用该资源,您必须进行一些Windows API调用。

In order to follow the article, you'll need a new Delphi project with a blank form (the default new project). Of course add the {$R AboutDelphi.RES} directive to the main form's unit. It's finally time to see how to use resources in a Delphi application. As mentioned above, in order to use resources stored inside an exe file we have to deal with API. However, several methods can be found in the Delphi help files that are "resource" enabled.

为了阅读本文,您需要一个带有空白表格的新Delphi项目(默认的新项目)。 当然,将{$ R AboutDelphi.RES}指令添加到主窗体的单元中。 现在终于是时候看看如何在Delphi应用程序中使用资源了。 如上所述,为了使用exe文件中存储的资源,我们必须处理API。 但是,可以在启用了“资源”的Delphi帮助文件中找到几种方法。

For example, take a look at the LoadFromResourceName method of a TBitmap object. This method extracts the specified bitmap resource and assigns it TBitmap object. This is *exactly* what LoadBitmap API call does. As always Delphi has improved an API function call to suit your needs better.

例如,看一下TBitmap对象的LoadFromResourceName方法。 此方法提取指定的位图资源并为其分配TBitmap对象。 这正是LoadBitmap API调用的功能。 与往常一样,Delphi改进了API函数调用,以更好地满足您的需求。

Now, add the TMediaPlayer component to a form (name: MediaPlayer1) and add a TButton (Button2). Let the OnClick event look like:

现在,将TMediaPlayer组件添加到窗体(名称:MediaPlayer1)并添加一个TButton(Button2)。 让OnClick事件看起来像:

One minor *problem* is that the application creates an MP3 song on a user machine. You could add a code that deletes that file before the application is terminated.

一个小问题是应用程序在用户计算机上创建MP3歌曲。 您可以添加一个代码,以在终止应用程序之前删除该文件。

提取*。??? ( Extracting *.??? )

Of course, every other type of a binary file can be stored as a RCDATA type. The TRsourceStream is designed specially to help us extract such file from an executable. The possibilities are endless: HTML in an exe, EXE in exe, empty database in an exe, and so and so forth.

当然,其他所有类型的二进制文件都可以存储为RCDATA类型。 TRsourceStream是专门为帮助我们从可执行文件中提取此类文件而设计的。 可能性无穷无尽:exe中HTML,exe中的EXE,exe中的空数据库等等。

翻译自: https://www.thoughtco.com/inside-the-delphi-exe-1058211

delphi 嵌入java

FlatStyle .........\vcl_flatstyle .........\.............\vcl_flatstyle7 .........\.............\..............\Packages .........\.............\..............\........\FlatStyle_D5.cfg,382,2001-07-01 .........\.............\..............\........\FlatStyle_D5.dcu,4531,2001-07-01 .........\.............\..............\........\FlatStyle_D5.dof,1240,2001-07-01 .........\.............\..............\........\FlatStyle_D5.dpk,2301,2001-07-01 .........\.............\..............\........\FlatStyle_D5.res,1536,2001-07-01 .........\.............\..............\........\FlatStyle_D6.cfg,390,2002-08-29 .........\.............\..............\........\FlatStyle_D6.dcu,5009,2002-08-29 .........\.............\..............\........\FlatStyle_D6.dof,1421,2002-08-29 .........\.............\..............\........\FlatStyle_D6.dpk,2250,2001-07-01 .........\.............\..............\........\FlatStyle_D6.dsk,2462,2001-07-01 .........\.............\..............\........\FlatStyle_D6.res,1536,2001-07-01 .........\.............\..............\........\FlatStyle_D7.cfg,438,2004-10-26 .........\.............\..............\........\FlatStyle_D7.dcu,6183,2004-10-26 .........\.............\..............\........\FlatStyle_D7.dof,2023,2004-10-26 .........\.............\..............\........\FlatStyle_D7.dpk,2250,2001-07-01 .........\.............\..............\........\FlatStyle_D7.dsk,2568,2002-09-09 .........\.............\..............\........\FlatStyle_D7.res,1536,2001-07-01 .........\.............\..............\Source .........\.............\..............\......\DFS.INC,8673,2001-06-29 .........\.............\..............\......\FlatArrow.res,372,2001-06-29 .........\.............\..............\......\FlatGraphics.dcu,1710,2002-09-09 .........\.............\..............\......\FlatGraphics.pas,2168,2001-06-29 .........\.............\..............\......\FlatSound.res,71508,2001-06-29 .........\.............\..............\......\FlatUtilitys.dcu,11988,2002-09-09 .........\.............\..............\......\FlatUtilitys.pas,11687,2001-06-29 .........\.............\..............\......\HSLUtils.dcu,3209,2002-09-09 .........\.............\..............\......\HSLUtils.pas,4174,2001-06-29 .........\.............\..............\......\TFlatAnimationUnit.dcu,15066,2002-09-09 .........\.............\..............\......\TFlatAnimationUnit.pas,12001,2001-06-29 .........\.............\..............\......\TFlatAnimWndUnit.dcu,9884,2002-09-09 .........\.............\..............\......\TFlatAnimWndUnit.pas,6336,2001-06-29 .........\.............\..............\......\TFlatButtonUnit.dcu,28259,2002-09-09 .........\.............\..............\......\TFlatButtonUnit.pas,24710,2001-06-29 .........\.............\..............\......\TFlatCheckBoxUnit.dcu,20650,2002-09-09 .........\.............\..............\......\TFlatCheckBoxUnit.pas,15453,2001-06-29 .........\.............\..............\......\TFlatCheckListBoxUnit.dcu,27040,2002-09-09 .........\.............\..............\......\TFlatCheckListBoxUnit.pas,25105,2001-06-29 .........\.............\..............\......\TFlatColorComboBoxUnit.dcu,27520,2002-09-09 .........\.............\..............\......\TFlatColorComboBoxUnit.pas,19775,2001-06-29 .........\.............\..............\......\TFlatComboBoxUnit.dcu,21667,2002-09-09 .........\.............\..............\......\TFlatComboBoxUnit.pas,13785,2001-06-29 .........\.............\..............\......\TFlatDesignRegister.dcu,6690,2001-07-01 .........\.............\..............\......\TFlatDesignRegister.pas,5137,2001-07-01 .........\.............\..............\......\TFlatEditUnit.dcu,17400,2002-09-09 .........\.............\..............\......\TFlatEditUnit.pas,10760,2001-06-29 .........\.............\..............\......\TFlatGaugeUnit.dcu,10185,2002-09-09 .........\.............\..............\......\TFlatGaugeUnit.pas,7556,2001-07-01 .........\.............\..............\......\TFlatGroupBoxUnit.dcu,14451,2002-09-09 .........\.............\..............\......\TFlatGroupBoxUnit.pas,10964,2001-06-29 .........\.............\..............\......\TFlatHintUnit.dcu,15818,2002-09-09 .........\.............\..............\......\TFlatHintUnit.pas,12646,2001-06-29 .........\.............\..............\......\TFlatListBoxUnit.dcu,25421,2002-09-09 .........\.............\..............\......\TFlatListBoxUnit.pas,22127,2001-06-29 .........\.............\..............\......\TFlatMaskEditUnit.dcu,16638,2002-09-09 .........\.............\..............\......\TFlatMaskEditUnit.pas,9655,2001-06-29 .........\.............\..............\......\TFlatMemoUnit.dcu,14856,2002-09-09 .........\.............\..............\......\TFlatMemoUnit.pas,8473,2001-06-29 .........\.............\..............\......\TFlatPanelUnit.dcu,10308,2002-09-09 .........\.............\..............\......\TFlatPanelUnit.pas,4805,2001-06-29 .........\.............\..............\......\TFlatProgressBarUnit.dcu,12657,2002-09-09 .........\.............\..............\......\TFlatProgressBarUnit.pas,11030,2001-06-29 .........\.............\..............\......\TFlatRadioButtonUnit.dcu,20512,2002-09-09 .........\.............\..............\......\TFlatRadioButtonUnit.pas,15276,2001-06-29 .........\.............\..............\......\TFlatRegister.dcr,11732,2001-06-29 .........\.............\..............\......\TFlatRegister.dcu,3463,2002-09-09 .........\.............\..............\......\TFlatRegister.pas,1324,2001-06-29 .........\.............\..............\......\TFlatScrollbarUnit.dcu,33164,2002-09-09 .........\.............\..............\......\TFlatScrollbarUnit.pas,31957,2001-06-29 .........\.............\..............\......\TFlatScrollbarUnit.res,9996,2001-06-29 .........\.............\..............\......\TFlatSoundUnit.dcu,3106,2002-09-09 .........\.............\..............\......\TFlatSoundUnit.pas,1820,2001-06-29 .........\.............\..............\......\TFlatSpeedButtonUnit.dcu,24834,2002-09-09 .........\.............\..............\......\TFlatSpeedButtonUnit.pas,22668,2001-06-29 .........\.............\..............\......\TFlatSpinButtonUnit.dcu,14577,2002-09-09 .........\.............\..............\......\TFlatSpinButtonUnit.pas,9502,2001-06-29 .........\.............\..............\......\TFlatSpinEditUnit.dcu,27146,2002-09-09 .........\.............\..............\......\TFlatSpinEditUnit.pas,16383,2001-06-29 .........\.............\..............\......\TFlatSplitterUnit.dcu,15292,2002-09-09 .........\.............\..............\......\TFlatSplitterUnit.pas,10697,2001-06-29 .........\.............\..............\......\TFlatTabControlUnit.dcu,21602,2002-09-09 .........\.............\..............\......\TFlatTabControlUnit.pas,19674,2001-06-29 .........\.............\..............\......\TFlatTitlebarUnit.dcu,10441,2002-09-09 .........\.............\..............\......\TFlatTitlebarUnit.pas,6386,2001-06-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值