Talk aboutDelphiApplication of resource files in programming
Chen
(i) Primary application:
A resource file is typically a file with an extension of Res. resource files are very common in VC, butDelphiThere is no introduction to resource files in their online help, in fact, they use their own resource compilation tool BRCC32.EXE (typically located \Delphi\ bin directory), we can completely make the same effect as VC files.
The greatest benefit of a resource file is the ability to compile some files that are called when necessary to produce a file. The greatest benefit of this is to protect the external files from corruption. For example, in a program you want to call a temporaryImage, the general practice is toImageUnder a certain path (usually the path where the main program is located), but if the user path mistakenly deletes yourImageFile, the program may not find the appropriate file and the error crashes. In addition, if you want to use a custom cursor, you need to use a resource file.
The use steps for the resource file are:
1. Writing RC script text
Write a file with the extension RC in Notepad or another text editor. For example:
Mycur cursor move.cur//JOIN CURSOR
Mypic Bitmap water.bmp//Add bitmap
Mywav WAVE happy.wav//Add sound
Myavi AVI Epoen. AVI//Join video
Myico ICON CJT. ICO//Join icon
The format is the actual file name, such as the name, type, in the resource file, such as the first row above defines a cursor named Mycur, with the actual name join Cursor move.cur.
2. Compiling the RC file into a res resource file
Copy the script file and the actual file to the directory where the Brcc32.EXE is located, and execute the DOS command. The format is: Brcc32 script file (carriage return), such as a script file for myfirst.rc, execute BRCC32 myfirst.rc (carriage return). If you are lazy, you can also create a new batch of processing files, the content is only one line: Brcc32 mufist.rc. (BecauseDelphiAfter installation, the search path is typically indicated in the automatic batch file. If the compilation succeeds, a file ending with res is generated, which is the resource file we need.
3. InDelphiAdd a resource file to the cell
Copy the generated res resource file to the path of your program, add a {$R mufirst.res} after the cell file {$R *DFM}, add the res file, and the compiled resource file is included in the executable file. If you have more than one resource file, you can also add it by using the same method.
4. InDelphiCalling a resource file in a program
Resource files inDelphiThe keywords in are hinstance. The following is a specific usage.
<1> Call of the cursor
First, define a constant with a value greater than 0 in the program, becauseDelphiThe default cursor is indexed by itself with a 0-negative 16来, so the cursor we draw should start at 1 on the surface of the index. Then add the following code in the Oncreat event of the window:
Screen.cursor[35]:=loadcursor (hinstance, ' mycur ');
Where 35 is a constant greater than 1, Mycur is the name of the cursor in the resource file. If you want to use a custom cursor on another control, such as a panel control, simply add the following code to the appropriate place in the program:
panel1.cursor:=35;
<2> calls to bitmaps
Create a new project, add a Timage control, and write the following code where you want it to appear:
Var Mymap:hbitmap;
Begin
Mymap:=loadbitmap (hinstance, ' mypic ');
Image1.picture.bitmap.handle:=mymap;
End
where "Mypic" is the name in the bitmap resource file.
Calls to the 〈3〉avi file
Create a new project, add a animate control and join where needed:
animater1.resname:= ' Myavi ';
Animater1. Active:=true;
Where Myavi is the name of the video file in the resource file.
〈4〉maximum calling a WAV file
Add the mmsystm unit to the uses to play the WAV file in the program. When playing Playsound (Pchar (' mywav '), Hinstance,sndsync or Snd_resource), where Mywav is the name of the sound file in the resource.
〈5〉 Join cursor
Adding the cursor is easier, as long as the res file is added to the unit file. Note, however, that the name should be "W". " WW "and so on, so that the first letter as far as possible, so as to avoid the main program icon order upside down. This way, when someone else is using your program, there are a number of options if they want to choose another icon.
Add:
1. Resource types In addition to the above types, you can also font files, string files, and so on.
2. Resource files can also be used under the console, not only in the standard graphical interface.
Let's try this out:
Create a new project, delete the unique form, and then modify the project file. Add a {$Apptype console}, add MMSystem to the uses clause, and delete the other reference units. Delete the statement between Begin and end. At this point, we can do the same as the Turbo Pascal program, and we can also invoke Windows APIs and resources. Join the resource file----{$R myfist.res}. Write between Begin and end:
Writeln (' demo program, press any key to start! ‘);
READLN;
PlaySound (Pchar (' mywav '), Hinstance,snd_sync or Snd_resource);
Writeln (' End of demo! ‘);
Run the program, will pop up a standard DOS window, press any key to play the sound file. Isn't it cool? I have downloaded a player, in its installation directory I found that there is a "DOS program", with the mouse double-click it will pop up a DOS window, show the DOS era unique drawing, and have background music! That's probably the way it's done.
3.Delphiitself with a tool called Image Editor, you can also edit the resource text, but compared with the method in this article, the following table can be drawn:
**************************************
Image Editor BRCC32
BMP only supports 16-bit color any color
Cursor black and white color any color
ICO supports only 16-bit color any color
AVI does not support support
WAV does not support support
Font
String does not support support
Other
****************************************
It says that the call is directly in the program itself. In fact, there are other uses of resource files. For example, if your program carries other files, it will be released when it is used.
Example: Myexe exefile ' ha1.exe '//script file
Here is the custom release function Extractres, which is used in this example:
Extractres (' Exefile ', ' myexe ', ' c:\new.exe ');
I saved the Ha1.exe to the C packing directory under the name of New.exe.
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
(ii) Intermediate application:
Above we already know how to put a set of BMP image from the resource file, but the BMP file is too large, JPG file application relatively more. So how to read the JPG image? Use the resource file to add a stream. Here's how:
(1) Myjpg JPEG my.jpg
(2) Var
Stream:tstream;
Myjpg:tjpegimage;
Begin
Stream:=tresourcestream.cceat (hinstance, ' myjpg ', ' JPEG ');
Try
Myjpg:=tjpegimage.create;
Try
Myjpg.loadfromstream (Stream);
IMAGE1.PICTURE.ASSIGNC (myjpg);
Finally
Myjpg.free;
End
Finally
Stream.free;
End
End
Read otherImageFiles are the same. For example, GIF animation file, of course, if you have a Gif.pas, this unit many sites have, you can find it yourself. In practical application I also found that the above code can be directly displayed in the resource file icon and BMP.
When it comes to graphics, you can actually useDelphiCreates, invokes, and calls the pure icon resource.DLLFor example, you can look at the Super King梁肇new directoryDll, many are purely icon resources only. Here's how:
(1): Create a Hicon.res file that is not duplicated here.
(2): Create a new text file ICON.DPR, which reads as follows:
Library Icon;
{$R Icon.res}
Begin
End.
UseDelphiYou can get the icon by opening the compilation.DLL.
(3): The actual invocation method is as follows:
......
Private
Hinst:thandle;
......
Var Hicon:thandle;
Begin
Hinst:=loadlibrary (' Icon.DLL‘);
If hinst=0 then Exit;
Hicon:=loadicon (Hinst,pchar (Edit1.text));
If hicon<>0 then Image1.picture.icon.handle:=hicon;
FreeLibrary (Hinst);
End
If your program wants to be used internationally for people with different languages, useDllWill be a good way to store character resources. BecauseDllNot like INI file can be arbitrarily modified, especially sometimes if you want to save some copyright informationDllIt's never been better. For example, you want to develop a "Chinese Simplified traditional translator" software, ready to provide GB32,BIG5 code and English three language menu to the user, then you can try to testDllTo save the character resource.
We need to build three ofDllThe first step is of course to write the RC file, take GB32 code as an example, the following:
/*mysc.rc*/
#define Ids_mainform_caption 1
#define Ids_btnopen_caption 2
#define Ids_btnsave_caption 3
#define Ids_btnbig5_caption 4
#define Ids_btngb32_caption 5
#define Ids_btnhelp_caption 6
#define IDS_HELP_SHELP 7
Stringtable
{
Ids_mainform_caption, "Chinese Simplified traditional translator"
Ids_btnopen_caption, "Open File"
Ids_btnsave_caption, "Save File"
Ids_btnbig5_caption, "Convert to Big5"
Ids_btngb32_caption, "Convert to Gb32"
Ids_btnhelp_caption, "Help"
Ids_help_shelp, "Enter the text or open the file and click on the button to convert!"
}
The second step is to compile the BRCC32 as res file and use the method above to getDllFile, the other twoDllGenerated in the same way. Use the following:
Create a new project and put five button:btnopen,btnsave,btnbig5,btngb32 and btnhelp on it. There is also a tcombobox:cbselect to choose the type of language.
The specific code is as follows:
Unit Unit1;
Interface
......
Private
shelp:string;
function searchlanguagepack:tstrings;
Procedure Setactivelanguage (languagename:string);
{Private declarations}
......
Implementation
Procedure Tform1.cbselectchange (Sender:tobject);
Begin
Setactivelanguage (Cbselect.text);//Call the appropriateDllThe file reads the corresponding character.
End
Procedure Tform1.formcreate (Sender:tobject);
Begin
CbSelect.Items.AddStrings (searchlanguagepack);//Search all the current directoryDllFile name
End
function TForm1.SearchLanguagePack:TStrings;
Var
Resultstrings:tstrings;
Doserror:integer;
Searchrec:tsearchrec;
Begin
Resultstrings: = tstringlist.create;
Doserror: = FindFirst (Extractfilepath (paramstr (0)) + ' *.DLL', Faanyfile, Searchrec);
While doserror = 0 do
Begin
Resultstrings.add (Changefileext (Searchrec.name, "));
Doserror: = FindNext (SEARCHREC);
End
FindClose (SEARCHREC);
Result: = resultstrings;
End
Procedure Tform1.setactivelanguage (languagename:string);
Var
Hdll:hmodule;
MYCHAR:ARRAY[0..254] of Char;
dllfilename:string;
Begin
Dllfilename: = Extractfilepath (paramstr (0)) + LanguageName + '.DLL‘;
If not fileexists (dllfilename) then Exit;
hDLL: = LoadLibrary (Pchar (Dllfilename));
Loadstring (hDLL, 1, MyChar, 254);
Self.caption: = MyChar;
Reads the character resource, 1 represents the 1 defined in the resource file
Loadstring (hDLL, 1, MyChar, 254);
Self.caption: = MyChar;
Loadstring (hDLL, 2, MyChar, 254);
Btnopen.caption: = MyChar;
Loadstring (hDLL, 3, MyChar, 254);
Btnsave.caption: = MyChar;
Loadstring (hDLL, 4, MyChar, 254);
Btnbig5.caption: = MyChar;
Loadstring (hDLL, 5, MyChar, 254);
Btngb32.caption: = MyChar;
Loadstring (hDLL, 6, MyChar, 254);
Btnhelp.caption: = MyChar;
Loadstring (hDLL, 7, MyChar, 254);
Shelp: = MyChar;
FreeLibrary (hDLL);
Application.title: = self.caption;
//------------------------
Btnopen.visible: = True;
Btnsave.visible: = True;
Btnbig5.visible: = True;
Btngb32.visible: = True;
Btnhelp.visible: = True;
//------------------------
End
Procedure Tform1.btnhelpclick (Sender:tobject);
Begin
Application.messagebox (Pchar (shelp), ' Http://lovejingtao.126.com ', mb_iconinformation);
End
End.
Perhaps you would say that this method is not as good as my own in the program directly define three specific values to facilitate. I even had to customize a structure myself.DLLIt's so troublesome. But what if your program uses a lot of characters? For example, the Windows operating system itself has simplified Chinese, Traditional Chinese, English and other versions, withDllThen just replace it directlyDLLInstead of opening the code every time you release a version. This can greatly reduce the amount of work and the chance of error. Here, say one more thing: The Windows system itself is a lotDllWith aImageand other resources, we can call directly in the program, so that our EXE can also reduce a lot! Of course, the smallest method is to generate technology in real time. The foreigner once wrote a 67KB program is to use this method.
How do I import a picture resource into a DLL and take it out? Go