Delphi Resource File Detailed

Source: Internet
Author: User

Delphi Resource File Detailed

First, Primer:

Now almost all Windows applications use icons, pictures, cursors, sounds, and so on, and we call them resources (Resource). The simplest way to use resources is to put the source files of these resources into the package to make it easier for the program to call when needed. Resources are part of the program, and the program will not be able to run away from the resource files. But it is non-executable code.

In order to manage resources better, Delphi provides a resource file of type. Res. It can integrate the resources needed in our program into a resource file (. RES) down. Compile the application directly into the executable program, as an integration of the application.

The benefits of doing this are: 1. Application execution is faster because locating resources takes less time than locating files on disk. 2. A variety of resources can be placed in a file, reducing the number of icons, pictures, cursors, sounds and other files. Do not be afraid of users in the process of careless and damage to the resource file so that the program does not work properly.

The disadvantage is that because the addition of the resource file increases the number of bytes compiled by the executable program, it can make the application look more and more swollen when the resource file is large.

The advantages of using resource files are significant and their drawbacks are outstanding. So in the actual application of the situation in accordance with the pros and cons, flexible use, this is no longer discussed in this article. The following is only a general summary of the use of resource files in Delphi system programming.

Second, create a resource file:

Note: Create a resource file name that is not the same as the project name, because Delphi creates a project automatically with the same resource files as the project name. It is also a good idea to save the resource file in the same folder as the project file.

1. First create a. RC Plain text file. The format is as follows:

Resource Identifier keyword resource file name

Format Description:

① Resource Identifier: the specific label in the program when the resource is invoked;

② Keyword: Identifies the resource file type, for example:

Wave: The resource file is a sound file;

Rcdata:jpeg file;

Avi:avi animation;

Icon: Icons file;

BITMAP: Bitmap file;

Cursor: Cursors file;

Rmi:rmi music files;

Midi:midi Music files

③ resource file Name: Added resource file name (with extension, you can make a full file name with the pathname);

④ Example:

Mywav WAVE "Filename.wav"

Mymid MIDI "C:\My Documents\My Music\canyon. MID "

Myavi AVI "Speedis.avi"

The resource file name in the example can be unquoted. Suppose we save the above three lines as sample.rc files.

2. Use the Borland Resource Compiler (BRCC32.EXE) to convert the. rc file into a. res file.

Enter the following command at the DOS command line:

C://Turn in C: drive

Cd\//Return root directory

Cd\program Files\borland\delphi7\bin//Enter the directory where BRCC32.EXE

BRCC32 sample.rc//Convert sample.rc to resource file Sample.res

Note Before conversion, you need to copy the file without the full pathname specified in the Sample.rc file into the current directory C:\Program Files\borland\delphi7\bin to ensure that it is transferred. Res file is not faulted. If the conversion process does not have an error, it succeeds.

Iii. Referencing resource files

Sample.res the resource file generated by the previous step into the same directory as the project to be built.

In order to access our resource files, we must tell Delphi to link our resource files to the application. Therefore, to add a compilation instruction in the source code to complete the above functions. This instruction must be immediately followed by the Window command, as in the following form:

{$R *. DFM}//delphi self-compiled instructions

{$R Sample.res}//instructions for newly added compiled resource files

Do not delete {$R *. DFM} directive, because this line of code tells the Delphi link to the resources under the window.

Iv. Invoking resource Files

1. Accessing bitmaps in a resource file (Bitmap)

If you want to access resources in your program, you must call some Windows API functions. bitmaps, cursors, and icons saved in the resource file can be accessed by calling the LoadBitmap, LoadCursor, and LoadIcon functions.

Here is an example of how to access a bitmap in a resource file and display it in a timage control.

Procedure Tfrmain.btncanvaspic (Sender:tobject);

Begin

Image1.Picture.Bitmap.Handle: =loadbitmap (HINSTANCE, ' resource identifier ');

End

Note: If the bitmap is not loaded successfully, the program still executes, but the image will no longer display. Depending on the return value of the LoadBitmap function, you can determine whether the load succeeds if the load succeeds if the return value is 0, if the load fails the return value is 0.

Another way to access the display bitmap is as follows:

Procedure Tfrmain.btnloadpicclick (Sender:tobject);

Begin

Image1.Picture.Bitmap.LoadfromResourceName (HINSTANCE, ' resource identifier ');

End

2. Accessing the cursor in a resource file

Screen.cursors[] is a cursor array, using the cursor file we can add a custom cursor to this property. Because the default cursor in the array has an index value of 0, it is best to set the custom cursor index value to 1 unless you want to replace the default cursor.

Procedure Tfrmain.btnusecursorclick (Sender:tobject);

Begin

SCREEN.CURSORS[1]: =loadcursor (HINSTANCE, ' resource identifier ');

Image1.cursor: = 1;

End

3. Access the icon in the resource file

Placing the icon in a resource file enables you to dynamically change the application icon.

Procedure Tfrmain.loadiconclick (Sender:tobject);

Begin

Application.Icon.Handle: = LoadIcon (HINSTANCE, ' resource identifier ');

End;

4. accessing AVI in a resource file

Add a Tanimate control (on the Win32 Control Panel) to the project and join it where needed:

Procedure Tfrmain.loadiconclick (Sender:tobject);

Begin

Animate1.resname: = ' Myavi '; Resource identification

Animate1.active: = True;

End;

A little summary in practice: Not all AVI resources can be played with tanimate components, to be tested when the program is programmed. If you encounter an AVI resource that cannot be played with the Tanimate component, you can detach it from the resource file and play it using the appropriate playback components such as Tmediaplayer. Remove the detached temporary files after use. Refer to the following "7. "Description.

5. accessing JPEG in a resource file

Add the JPEG unit to the interface uses.

Procedure Tform1.loadjpgclick (Sender:tobject);

Var

Fjpg:tjpegimage;

Fstream:tresourcestream;

Begin

Fjpg: = tjpegimage.create;

FStream: = Tresourcestream.create (HINSTANCE, ' resource identifier ', rt_rcdata);

Fjpg.loadfromstream (FStream);

Image1.Picture.Bitmap.Assign (fjpg);

End

6. Accessing wave in a resource file

Add MMSystem to interface's uses.

Procedure Tform1.loadwaveclick (Sender:tobject);

Begin

PlaySound (' mywav ', hinstance,snd_async or snd_memory or snd_resource);

End

A point in practice: The parameter Snd_async of PlaySound () represents the asynchronous playback method, and Snd_sync indicates the way to play synchronously. (1) When using asynchronous mode, WAV resources can be used as background music, but when there are multiple asynchronous calls, it is possible to discard all playback before the last call, and the user will get only the last sound of the call. (2) When synchronizing, the WAV resource will monopolize the resource, causing the WAV file not to play any action until the end of its call. So if there is a large synchronous WAV resource, it will cause the application interface to stall. However, it is possible to perform multiple invocations in a continuous manner without a leak.

7. Calls to other resources:

First, the file to be embedded in the RCDATA (Window specified in the custom format) format into the resource file, call the source file in the resource file through the "stream", create a temporary physical file exists in the application path, and then you can use the corresponding type of component or method to work with the file. Do not forget to delete the temporary file when the program exits.

For example:

Var

tmpdirectory:string;

Myres:tresourcestream;

Begin

Tmpdirectory: = Extractfilepath (paramstr (0));

If not fileexists (tmpdirectory + ' Music1.rmi ') then

Begin

Myres: = Tresourcestream.create (hinstance, ' music1 ', ' RMI ');

Myres. SaveToFile (tmpdirectory + ' music1.rmi ');//separated from the resource file

Myres. Free;

。。。。。。。。。。

End

Delete the program when it exits:

If FileExists (tempdirectory + ' Music1. RMI ') Then

DeleteFile (tempdirectory + ' Music1. RMI ');

Example:

1. Embed executable EXE file

Although Windows specifies that RCDATA be used as a custom format, we can also customize the format name, such as this example (RC file):

MyFile1 RCDATA "C:\Windows\system32\notepad.exe"

MyFile2 myres "C:\Windows\System32\calc.exe"

{The Notepad.exe is specified as RCDATA format, and calc.exe is specified as a custom Myres format}

This example embeds Notepad and calculator in the resource, and then extracts and invokes:

Unit Unit1;

Interface

Uses

Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,

Dialogs, Stdctrls;

Type

TForm1 = Class (Tform)

Button1:tbutton;

Button2:tbutton;

Procedure Button1Click (Sender:tobject);

Procedure Button2click (Sender:tobject);

End

Var

Form1:tform1;

Implementation

{$R *.DFM}

Procedure Tform1.button1click (Sender:tobject);

Var

Rs:tresourcestream;

Begin

RS: = Tresourcestream.create (hinstance, ' MyFile1 ', rt_rcdata);

Rs. SaveToFile (' C:\temp\pad.exe ');

WinExec (' C:\temp\pad.exe ', 1);

Rs. Free;

End

Procedure Tform1.button2click (Sender:tobject);

Var

Rs:tresourcestream;

Begin

RS: = Tresourcestream.create (hinstance, ' MyFile2 ', ' myres ');

Rs. SaveToFile (' C:\temp\sum.exe ');

WinExec (' C:\temp\sum.exe ', 1);

Rs. Free;

End

End.

2. Embed text files

(1) Definition and generation of resource files:
Edit the resource definition file myres.rc, which reads as follows:
Txt1 MyText Test1.txt
Txt2 MyText Test2.txt
Then, the myres.rc is added to the project, will automatically add the statement {$R ' myrec.res '} at the beginning of the prj file. In the future compile the program will automatically generate the resource file Myrec.res, and add resources to the EXE file. Which mytext me to our custom resource types, HTML1 and HTML2 are the corresponding text files Test1.txt and test2.txt resource identification names, which are used to distinguish different resources later when invoking resources. Once the EXE file is generated, myrec.rc, Myrec.res, and Test1.txt and test2.txt are useless and do not have to be published with the EXE file.

(2) Resource invocation:
Use a piece of code to illustrate (debug through Delphi7.0), mainly involved in the flow of memory operations, Tresourcestream.create () is a stream creation function. You can restore resources directly to a text file. You can also read the stream into the BUF array, use the Read method of the stream, and assign the BUF to the string variable.
Procedure Tform1.button1click (Sender:tobject);
Var
Res:tresourcestream;
BUF:ARRAY[0..10000] of Char;
stxt:string;
Begin
Res: = Tresourcestream.create (hinstance, ' txt1 ', PChar (' mytext '));

Save the resource as a file, that is, restore the file
Res.savetofile (' text1.txt ');

You can also stream operations and take the contents of a text file into a string variable
Res. Read (buf, Res. Size);
STXT: = BUF; Convert the Pchar type to a string type
ShowMessage (STXT); For the ShowMessage function, ShowMessage (BUF) is also correct
Res.free;
End

###############################################################################

Delphi Learn to use resource files-embed and extract any type of file

The following are the resource formats supported by Windows:

Rt_cursor = Makeintresource (1);

Rt_bitmap = Makeintresource (2);

Rt_icon = Makeintresource (3);

Rt_menu = Makeintresource (4);

Rt_dialog = Makeintresource (5);

rt_string = Makeintresource (6);

Rt_fontdir = Makeintresource (7);

Rt_font = Makeintresource (8);

Rt_accelerator = Makeintresource (9);

Rt_rcdata = Types.rt_rcdata; Makeintresource (10);

rt_messagetable = Makeintresource (11);

difference = 11;

Rt_group_cursor = Makeintresource (DWORD (rt_cursor + difference));

Rt_group_icon = Makeintresource (DWORD (Rt_icon + difference));

Rt_version = Makeintresource (16);

Rt_dlginclude = Makeintresource (17);

Rt_plugplay = Makeintresource (19);

Rt_vxd = Makeintresource (20);

Rt_anicursor = Makeintresource (21);

Rt_aniicon = Makeintresource (22);

Delphi Resource File Detailed

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.