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.
Steps for using the resource source file:
1. zookeeper RC checks this file
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
Music1 file1 moon. MP3 // MP3 file
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.
Note: Brcc32.exe is in the Delphi7 \ Bin folder.
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.
Note: In {$ R mufirst. res}, mufirst. res is the source file name generated by your website.
4. Call the resource file 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 an 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> use streaming to use MP3 data source files
The following is the replacement:
Unit unit1;
Interface
Uses
Windows, messages, sysutils, variants, classes, graphics, controls, forms,
Dialogs, stdctrls, mmsystem;
Type
Tform1 = Class (tform)
Button1: TButton;
Button2: TButton;
Button4: TButton;
Procedure Button1Click (Sender: TObject );
Procedure Button2Click (Sender: TObject );
Procedure Button4Click (Sender: TObject );
Private
{Private declarations}
Public
{Public declarations}
End;
Var
Form1: TForm1;
Implementation
{$ R *. dfm}
{$ R MyMusic. res}
Procedure TForm1.Button4Click (Sender: TObject );
Var res: TResourcestream;
Filename: string;
Begin
Filename: Your 'moonregion ';
Res: = TResourcestream. Create (0, 'music1', Pchar ('file1 '));
If fileexists('moonshanghai') = false then
Res. SaveToFile (filename); // generate the current file (optional)
Setfileattributes (pchar (ExtractFilePath (Application. ExeName) + 'moonhangzhou'), FILE_ATTRIBUTE_hidden); // The generated temporary file will be hidden.
MCIsendstring (Pchar ('open' + filename + 'Type mpegvideo alias ww '), nil, 0, 0 );
MCIsendstring ('play ww ', nil, 0, 0 );
Res. Free;
Deletefile (filename );
End;
End.
<6> Add 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.