Clever Use of res files in Delphi

Source: Internet
Author: User
In a Delphi project, there is usually a file name that is the same as the main file name of the project name. res file, which is used to save resources such as application icons. Corresponding to this file, the project file must contain the Compilation instruction "{$ R *. res} "tells the compiler that the file name must be the same as the main file name of the project during compilation. res file. Similarly, if you need to include any file type in the application as a resource, you can also use the. Res file. This article will show you how to use res files to include files in the Delphi Program.

1. Create a. Res File
A group of audio files is used as an example. First, use Notepad and other text editors to compile the file "mymusic. RC ". The file format and content are as follows:

Src1 file1 onlyyou.pdf
Src2 file2 yesterdayoncemore.mp3
Src3 file3 moonriverlinks

Each row declares a resource. Each resource Declaration contains three parameter definitions. The first parameter is the resource name, the second parameter is the type description of the resource (this example defines three types), and the third parameter is the file name. Parameters must be separated by spaces.
After compiling the "mymusic. RC" file, place it in the same folder as the above three. MP3 files (to avoid the trouble of setting the path ). Run the application brcc32.exe (located in .. "DELPHI6" bin) in the following command line: brcc32 mymusic. Rc. Note that this command can only be executed in the command line mode. If the pathenvironment variable of the system does not include the delphiruntime, you can also place brcc32.exe and mymusic. RC and three. MP3 files in the same path. After running the preceding command line, the file "mymusic. res ", which is an independent and complete resource file. Its format follows the Windows Standard. Therefore, it can be used in various development environments such as VB, Vc, and cbuilder.

2. Declare the. Res file in the program
You only need to add the following compilation instructions to a certain unit in the project (preferably the unit you need to use these resources to make your application more readable:

$ R mymusic. Res}
{

Although this Compilation instruction can be at any position of the Unit, it is best to place it in parallel for the Compilation instruction "{$ R * To make your source code structure reasonable *. DFM} (each unit containing a Form file contains the line of compilation instructions ). The compilation instructions here clearly tell the compiler that the resource file "mymusic. Res" must be included in the application during compilation.

3. obtain resources at runtime
The above two steps are only the Foundation (but must be necessary), and the use of resources is the fundamental purpose. The following process will show you how to restore the resources (Files) contained in the application.

Procedure musicrestofile (const resname, restype, filename: string );
VaR
Res: tresourcestream;
Begin
Res: = tresourcestream. Create (hinstance, resname, pchar (restype ));
Res. savetofile (filename); // Save the resource as a file, that is, restore the file
// You can perform other stream operations to use resources in the memory instead of saving them as files.
Res. Free;
End;

Example of calling this process:

Musicrestofile ('src2', 'file2', 'thesecondmusic. mp3 ');

It is worth mentioning that with this method, you can create your own installer, which only contains one executable file, the executable file contains all the content (Files) to be installed as resources ). In addition, if you need to play external audio files (such as background music and sound prompts) in your application, you can also include these external files in the application by using this method, this prevents incomplete applications caused by accidental deletion of these audio files.

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.