Dephi Chinese source file application

Source: Internet
Author: User

I. Introduction:

Currently, almost all Windows applications use icons, images, cursors, and sounds. We call them resources ).
The simplest way to use resources is to import the source files of these resources into the software package to facilitate calls when the program needs them. Resource is a program
If the program runs normally, the resource file cannot be removed. But it is not executable code.
To better manage resources, Delphi provides a. Res type resource file. It can take the resources required in our program
The source is integrated into a resource file (. Res. When compiling an application, it is directly compiled into an executable program and becomes an integral part of the application.
Fit.
The benefits of doing so are:
1. Because locating resources takes less time than locating files on a disk, application execution is faster.
2. Multiple resources can be placed in one file, reducing the number of icons, images, cursors, sounds, and other files. Do not be afraid that users are using
The program cannot run properly because the resource file is damaged accidentally during the process.

Disadvantages:
The addition of resource files increases the number of bytes of the compiled executable program. When the resource file is large, the application will change
It looks swollen.

The advantages and disadvantages of using resource files are significant. Therefore, in practical application, we must weigh the advantages and disadvantages and make it flexible.
This is not discussed in this article. Below is a general summary of how to use resource files in Delphi system programming.

Ii. Create a resource file:

Note:
The name of the created resource file must not be the same as the project name, because a resource with the same project name is automatically created during project creation by Delphi.
File. And it is best to save the resource file to the same folder as the project file.

1. Create a. RC plain text file. The format is as follows:

Resource Identifier keyword resource file name

Format description:
① Resource identifier: the specific identifier used to call resources in a program;
② Keyword: identifies the resource file type; for example:
Wave: The resource file is a sound file;
Rcdata: JPEG file;
Avi: AVI animation;
Icon: icon file;
Bitmap: a bitmap file;
Cursor: The cursor file;
RMI: RMI music file;
MIDI: MIDI music file
③ Resource file name: the name of the added resource file (the name can contain a full file name with the extension );
④ Example:
Mywav wave "filename. wav"
Mymid Midi "C: \ My Documents ents \ My music \ Canyon. Mid"
Myavi Avi "speedis. Avi"

In this example, the resource file name can be unenclosed by quotation marks. Suppose we save the preceding three rows as the sample. RC file.

2. Use the Borland resource compiler (brcc32.exe) to convert the. RC file to the. Res file.
Enter the following command in the doscommand line:
C: // switch to C: Drive
CD \ // return the root directory
CD \ Program Files \ Borland \ Delphi7 \ bin // enter the directory where brcc32.exe is located
Brcc32 sample. RC // converts sample. RC into the resource source file sample. Res.

Note: before conversion, copy the file without full path name specified in the sample. RC file to the current directory c: \ Program Files \
Borland \ Delphi7 \ bin to ensure that the file is converted to. Res without errors. If no error is reported during the conversion process, the operation is successful.

3. Reference resource files:

Put the resource file sample. Res generated in the previous step into the same directory of the project to be created.
To access our resource files, you must tell Delphi to link our resource files to the application. Therefore, in the source code
Add a compilation command to complete the above functions. This command must follow the window command in the following format:

{$ R *. DFM} // Delphi built-in Compilation instruction
{$ R sample. Res} // command for the newly added compiled resource file

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

4. Call resource files:

1. Access bitmap in the resource file)

To access resources in the program, you must call some Windows API functions. Bitmap, cursor, and Icon saved in the resource file
It can be accessed by calling loadbitmap, loadcursor, and loadicon functions. The following example shows how to access the bitmap in the resource file and display
In the 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 runs, but the image will no longer display the image. Based on loadbitmap
The Return Value of the function determines whether the load is successful. If the load is successful, the return value is non-0. If the load fails, the return value is 0.
Another method for accessing the display bitmap is as follows:

Procedure tfrmain. btnloadpicclick (Sender: tobject );
Begin
Image1.picture. bitmap. loadfromresourcename (hinstance, 'Resource identifier ');
End;

2. Access the cursor in the resource file

Screen. cursors [] is a cursor array. With the cursor file, we can add the custom cursor to this attribute. Because Mo
The recognized cursor index value in the array is 0, so unless you want to replace the default cursor, it is best to set the custom cursor index value to 1.

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

Place the icon in the resource file to dynamically change the application icon.

Procedure tfrmain. loadiconclick (Sender: tobject );
Begin
Application. Icon. Handle: = loadicon (hinstance, 'Resource identifier ');
End;

4. Access Avi in the resource file:

Add a tanimate control to the project (on the Win32 control panel) and add it as needed:
Procedure tfrmain. loadiconclick (Sender: tobject );
Begin
Animate1.resname: = 'myavi'; // resource ID
Animate1.active: = true;
End;

One result in practice: not all Avi resources can be played using the tanimate component and tested during programming. Encounter
If you cannot use the tanimate component to play an AVI resource, you can separate it from the resource file and use the corresponding playback component, such
Tmediaplayer. Delete the temporary files after use. For more information, see "7.

5. Access the JPEG in the resource file:

Add the JPEG unit to the interface's uses.

Procedure tform1.loadjpgclick (Sender: tobject );
VaR
Fjpg: tsf-image;
Fstream: tresourcestream;
Begin
Fjpg: = tsf-image. Create;
Fstream: = tresourcestream. Create (hinstance, 'Resource identifier', rt_rcdata );
Fjpg. loadfromstream (fstream );
Image1.picture. bitmap. Assign (fjpg );
End;

6. Access the wave in the resource file:

Add mmsystem to interface uses

Procedure tform1.loadwaveclick (Sender: tobject );
Begin
Playsound ('mywav ', hinstance, snd_async or snd_memory or snd_resource );
End;

In practice, the playsound () parameter snd_async indicates the asynchronous playback mode, and snd_sync indicates the synchronous playback mode.
(1) When asynchronous mode is adopted, WAV resources can be used as background music. However, when multiple asynchronous calls are made consecutively
Then, it may discard all the plays before the last call. The user's effect is only the voice of the last call.
(2) When the synchronization mode is adopted, the wav resources will exclusively occupy the resources, so that the user cannot apply
Perform any operations until the call ends. Therefore, if there is a large amount of Wav resources to be synchronized, the application interface will be stuck. However
This method can be used to execute multiple calls one by one consecutively.

7. Call Other resources:

You can separate the source files from the resource files and create a temporary physical file in the Application Path. Then you can
Use the corresponding components or methods to use the file. When the program exits, do not forget to delete the temporary file.
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 '); // extract from the resource file
Myres. Free;
...........
End;

Delete when the program exits:

If fileexists (tempdirectory + 'music1. Rmi ') then
Begin
Deletefile (tempdirectory + 'music1. Rmi ');
End;

V. Conclusion:
Bitmap, cursor and icon, AVI animation, JPEG, and wave files can be placed in resource files. We have introduced how
Add the source to the resource file and introduce how to dynamically access the resource file in Delphi. The Delphi compilation project will automatically create
Resource file with the same project name (if there are no other resources, the icon of the main window will be placed in this resource file ). But the most recommended
Do not change this resource file.

Dephi Chinese source file application

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.