Delphi Resource document production and use of detailed

Source: Internet
Author: User

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:
You can isolate the source files in the resource file, create a temporary physical file that exists under the application path, and then use the file in the appropriate type of component or method. 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 ');
V. Concluding remarks
bitmaps, cursors and icons, AVI animations, JPEG, and wave files can be placed in a resource file. Above we introduce how to add resources to the resource file, and introduce the method of dynamically accessing the resource file in Delphi. The Delphi compilation Project automatically creates a resource file with the same project name (the main window icon is placed in the resource file if there is no other resource). However, it is advisable not to change this resource file.
Vi. References:
1. Http://www.delphiok.com/bcjq/all/114.htm from Delphi Skills Network;
2. http://www.delphibbs.com/delphibbs/dispq.asp?lid=2358589;
http://www.delphibbs.com/delphibbs/dispq.asp?lid=2369429;
http://www.delphibbs.com/delphibbs/dispq.asp?lid=2370212, from the Monopoly forum
Special to the above website and monopoly Wind8bell, confused, Miaofeng, app2001 and other express thanks!

Can make an rc file yourself, such as sample.rc, RC file is actually a resource file description text,
You can create it by using the Notepad program. You can then enter some of the resources we want to define, such as:
Men BITMAP C:\bitmap\men.bitmap
ARJ Exefile C:\arj.exe
MOV AVI C:\mov.avi
Then use BRCC32 to compile the RC file into a sample.res (real resource file).
In Delphi's engineering file, use the $R compile instructions to let Delphi include resources into the EXE file.
{$R Sample.res}
This allows us to invoke resources in this single execution file. Examples are as follows:
Exefile:
Procedure Extractres (ResType, Resname, resnewname:string);
Var
Res:tresourcestream;
Begin
Res: = Tresourcestream.create (HInstance, Resname, Pchar (restype)); Res.savetofile (Resnewname);
Res.free;
End
Avi:
Procedure Loadavi;
Begin
{AVI1 is a tanimate class}
Avi1.resnam

http://blog.csdn.net/zang141588761/article/details/52367357

Delphi Resource document production and use of 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.