Delphi detailed instructions on the use of Chinese source files)

Source: Internet
Author: User
Tags custom cursors

For more detailed tutorial, see: http://www.cnblogs.com/del/category/122614.html

Use of Chinese-funded sources in Delphi
I. Write RC Script text

Use notepad or another text editor to compile a file with the extension ". rc. The format is as follows:

AVI silent Animation

BITMAP files

CURSOR file

ICON file

WAVE audio files

All of the above are standard resource types. You can also customize a type for the resource, for example, "mytype ". However, there are some differences in the call method between the two (detailed descriptions are provided in the following example "accessing the bitmap in the resource file ).

2. Compile the RES resource file according to the RC Script File

Enter the following content in the command prompt:

Brcc32 FileName. rc // Note: brcc32.exe is in the DelphiX "Bin directory

3. Add source files to the Delphi Unit

Copy the generated RES resource file to the directory of the corresponding program, and add "{$ R * DFM}" after "{$ R FileName" in the unit file. res} ". After compilation, the resource file will be included in the executable file.

NOTE: If steps 2 and 3 are troublesome, you can simply add the RC file to the project. when compiling the Delphi project, the resource file is automatically compiled.

Iv. Resource file call example

(1) access the bitmap in the resource file

// RC: testBmp bitmap res "test.bmp

Image1.Picture. Bitmap. LoadFromResourceName (HInstance, 'res "test.bmp ');

// RC: testBmp BMP Type res "test.bmp

Var

ResStream: TResourceStream;

Begin

ResStream: = TResourceStream. Create (HInstance, 'testbmp ', 'bmp type ');

Image1.Picture. Bitmap. LoadFromStream (resStream );

ResStream. Free;

End;

Note the differences between the preceding two call methods.
2) access the icon in the resource file

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

// RC: testIcon res "test. ico

Application. Icon. Handle: = LoadIcon (HInstance, 'testicon ');

(3) Access the AVI animation in the resource file

// RC: testAvi avi res "test. avi

Animate1.ResName: = 'testavi ';

Animate1.Active: = True;

(4) Access JPEG images in resource files

To be able to process JPEG images, JPEG units must be referenced at the Interface.

Var

Jpg: tsf-image;

ResStream: TResourceStream;

Begin

Jpg: = tsf-image. Create;

ResStream: = TResourceStream. Create (HInstance, 'testjpg ', 'jpg type ');

Jpg. LoadFromStream (resStream );

Image1.Picture. Assign (jpg );

Jpg. Free;

ResStream. Free;

End;

Appendix: Application of Delphi programming Chinese source files: Preliminary Application

Resource files are generally files with the extension res. chinese source files in VC are widely used, But Delphi does not provide any introduction to resource files in its online help, in fact, using its own resource compilation tool BRCC32.EXE (usually in the "Delphi" BIN directory), we can make files with the same effect as VC.

The biggest benefit of a resource file is that it can compile some files called only when necessary with executable files to generate a file. the biggest benefit of doing so is to protect external files from destruction. for example, if you want to temporarily call an image in a program, the general practice is to put the image under a certain path (usually the path of the main program ), however, if the user path accidentally deletes your image file, the program may fail to find the corresponding file. in addition, if you want to make your own program interface beautiful, you also need to use resource files if you want to use custom cursors.

To use a resource file, follow these steps:

I. Write rc Script text
Use notepad or another text editor to compile a file with the rc extension. For example:

Mycur cursor move. cur // Add the cursor
Mypic Bitmap Water. BMP // Add Bitmap
Mywav WAVE happy.wav // Add sound
MyAVI avi epoen. AVI // Add the video
MyIco icon cjt. ICO // Add ICON

The format is the name in the resource file> type> actual file name. For example, the first line above defines a cursor named mycur. The actual name is move. cur.

2. Compile the rc file into a res resource file
Copy the script file and the actual file to the directory where Brcc32.EXE is located and run the doscommand. The format is: Brcc32 script file (Press ENTER). For example, if there is a script file named myfirst. rc, execute Brcc32 myfirst. rc (Press ENTER. If you are a lazy person, you can also create a batch of processing files with only one line: Brcc32 mufist. rc. (because after Delphi is installed, it usually specifies the search path in the automatic batch file .) If the compilation is successful, a file ending with res will be generated, which is the resource file we need.

3. Add source files to the Delphi Unit
Copy the generated res resource file to the path of your program, and add {$ R mufirst to the unit file {$ R * DFM. res}, add the res file. After compilation, the resource source file is included in the executable file. If you have multiple resource files, add them in sequence as described above.

4. Call resource files in the Delphi Program
The keyword of the resource file in Delphi is hinstance. The specific usage is given below.

1. Call the cursor
First, define a constant with a value greater than 0 in the program. Because Delphi uses 0-negative 16 to index the default cursor, the cursor we set should start from 1 on the surface. Then add the following code to the Oncreat event in the window:

Screen. cursor [35]: = Loadcursor (hinstance, 'mycur ');

35 is a constant greater than 1, and mycur is the name of the cursor in the resource file. To use a custom cursor on another control, such as a Panel control, add the following code as appropriate:

Panel1.cursor: = 35;

2. Bitmap call

Create a new project, add a Timage control, and write the following code where you want to display it:

Var mymap: Hbitmap;
Begin
Mymap: = LoadBitmap (hinstance, 'mypic ');
Image1.picture. Bitmap. Handle: = mymap;
End;

"Mypic" is the name in the bitmap resource file.

3. Call the AVI file
Create a project, add an Animate control, and add the following items as needed:

Animater1.resname: = 'myavi ';
Animater1.Active: = true;

MyAVI is the name of the video file in the resource file.

4. Call WAV Files
Add the mm1_m unit to uses to play the WAV file in the program. Playsound (pchar ('mywav '), hinstance, sndsync or snd_resource) during playback. mywav indicates the name of the audio file in the resource.

5. Add the cursor
It is easy to add the res file to the cell file. However, it is recommended that the name be "W". "WW" and so on, so that the first letter is as backward as possible to avoid reversing the icon order of the main program. In this way, when others use your program, there are many options if they want to select other icons.

Supplement:
1. In addition to the above types, the resource type also supports font files and string files.

2. Resource files can be used in both the standard GUI and the console.

Let's test it:
Create a project, delete a unique Form, and modify the project file. Add {$ Apptype console}, add mmsystem to the uses clause, and delete other reference units. Delete the statements between Begin and end. At this point, we can use the same program as the Turbo PASCAL program and call windows APIs and resources. Add the resource file ---- {$ R myfist. res. Write down between Begin and end:

Writeln ('demo program, press any key to start! ');
Readln;
Playsound (pchar ('mywav '), hinstance, snd_sync or snd_resource );
Writeln ('demo ended! ');

When you run the program, a standard DOS window pops up. Press any key to play the audio file. Is it COOL! I have downloaded a player. I found a "DOS program" under its installation directory. Double-click it and a DOS window will pop up, showing the unique drawing in the DOS era, and background music! This method may be used.

3. Delphi itself comes with a tool called Image Editor, which can also edit resource text. However, compared with the method in this article, the following table can be obtained:

Image Editor Brcc32
BMP Only 16-bit colors are supported. Any color
Cursor Black and white Any color
ICO Only 16-bit colors are supported. Any color
AVI Not Supported Supported
WAV Not Supported Supported
String Not Supported Supported

The above is a direct call to the program itself. In fact, there are other usage of resource files. For example, when your program carries other files, it will be released.
Example: myexe exefile 'ha1.exe '// script file

The following is the custom release function ExtractRes, which is used in this example as follows:

ExtractRes ('existfile', 'myexe ', 'c: "new.exe ');

Save ha1.exewith new.exe as the name to the C root directory.

Function TForm1.ExtractRes (ResType, ResName, ResNewName: string): boolean;
Var
Res: TResourceStream;
Begin
Try
Res: = TResourceStream. Create (Hinstance, Resname, Pchar (ResType ));
Try
Res. SavetoFile (ResNewName );
Result: = true;
Finally
Res. Free;
End;
Except
Result: = false;
End;
End;

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.