Using res files skillfully in Delphi

Source: Internet
Author: User
Tags command line

In the Delphi project, there is usually a file name that is the same as the title of the project. Res file, which is used to save resources such as the application icon, corresponding to this file, must contain the compile directive "{$R *.res}" in the project file, telling the compiler to compile with the same as the project's main file name. Res file.

Similarly, if you need to include any file type in a resource form in your application, you can also take advantage of it. Res file. This article will show you how to use res files to include files within the Delphi program.

One created. Res file

Here is an example of a set of audio files. First write the file "mymusic.rc" with any text editor, such as Notepad. The format and contents of the document are as follows:

SRC1 FILE1 OnlyYou.mp3
SRC2 FILE2 YesterdayOnceMore.mp3
SRC3 FILE3 MoonRiver.mp3

where each row represents a declaration of a resource, the declaration of each resource contains three parameter definitions, the first parameter is the name of the resource, the second parameter is the type description of the resource (three classes are defined in this example) and the third parameter is the file name. Parameters must be separated by spaces.

After writing the "mymusic.rc" file, associate it with the above three. The MP3 file is placed in the same folder (to avoid the hassle of setting the path). Then run the application brcc32.exe on the following command line (located at ...). \delphi6\bin): Brcc32 mymusic.rc Note that this command can only be executed on a command-line basis. If the system's PATH environment variable does not contain Delphi's running path, then you will need to brcc32.exe with Mymusic.rc, three. The MP3 file is placed in the same path. When you run the above command line, you will get the file "Mymusic.res" in the same path, which is a self-contained, complete resource file that follows the Windows standard and can be used in a variety of development environments such as VB, VC, Cbuilder, and so on.

The second is declared contained in the program. Res file

You just need to add the following compilation instructions to a unit in your project (preferably a unit where you need to use these resources to make your application more readable):

$R Mymusic.res}

{

Although the compilation indicates that it can be located anywhere in the cell, it is best to make your source code structurally sound, with the compile instructions "{$R *.DFM}" included in each cell containing the form file. The compilation instructions here explicitly tell the compiler that the resource file "Mymusic.res" needs to be included in the application when compiling the application.

Three get resources at run time

These two-step work is only the basis (but must be), the use of resources is the fundamental purpose. The following procedure will tell you how to restore the resources (files) contained in your application.

procedure MusicResToFile(const ResName, ResType,FileName: string);
var
Res: TResourceStream;
begin
Res := TResourceStream.Create(HInstance,ResName, PChar(ResType));
Res.SaveToFile(FileName); //将资源保存为文件,即还原文件
//你还可以进行其它的流操作,在内存中实现对资源利用而不必另存为文件
Res.Free;
end;

Examples of calls to this procedure are:

Musicrestofile (' SRC2 ', ' FILE2 ', ' Thesecondmusic.mp3 ');

It is worth mentioning that, using this method, you can make your own installer, which contains only one executable file that contains all the content (files) that will be installed in the form of resources. In addition, if you need to play external audio files (such as background music, sound cues, etc.) in your application, you can also include these external files in your application in this way, thereby preventing users from accidentally deleting these audio files and causing the application to be incomplete.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.