(1) Primary application: Resource files are generally filed as RES files. The resource files are very common in VC, but Delphi does not make any introduction to the resource file in its online help, in fact, using its own resources Compilation tool brcc32.exe (typically located in / delphi / bin directory), we can make a document as VC. The biggest benefit of resource files can compile some files that are called when necessary, generate a file. The biggest advantage is that the external file is from damage. For example, in a program you want to temporarily call one Picture, general practice is to put the picture in a certain path (usually the path where the main program is located), but if the user path is mistaken to delete your image file, it may not make the program can't find the corresponding file and crash. In addition, if You think your own program is beautiful, you want to use some custom cursors, but also use the resource file. The use of the resource file is: 1. Write the RC script text written with a notebook or other text editor to write an extension-name RC. For example: Mycur Cursor Move.cur // Add Code MyPic Bitmap Water.BMP // Add Bits MYWAV WAVE HAPPY.WAV // Add Sound MYAVI AVI EPOEN.AVI // Add Video Myico Icon Cjt.ico // Add Icon Format For the name in the resource file -> Type -> actual file name, for example, the first line is defined in the cursor named mycur, the actual name is joined the cursor Move.cur. 2. Compile the RC file into the RES resource file will be scripted The files and actual files are copied to the directory where BRCC32.exe is located, and the DOS command is executed. The format is: BRCC32 script file (Enter), such as a script file for MyFirst.RC, execute Brcc32 myfirst.rc (Enter). If you are a lazy, you can create a batch of processes, and only one line: brcc32 mufist.rc. (Because the Delphi installation will specify the search path in the automatic batch file.) If the compilation is successful, one will generate The end of the res file, this file is the resource file we need. 3. Add a resource file to the Delphi unit to copy the generated RES resource file to the path you of your program, add a sentence in unit file {$ r * DFM}, add a sentence {$ r mufirst.res}, will res file Add, the compiled resource file is already included in the executable. If you have multiple resource files, please join. 4. Calling the resource file resource file in the Delphi program in delphi. The keyword is Hinstance. The specific usage is given below. <1> Coke first defines a constant greater than 0 in the program, because Delphi itself uses 0- Negative 16 to index the default cursor, so the cursor we have developed should start from the surface 1. The following code then adds the following code in the Window on the oncreat event: Screen.Cursor [35]: = loadingCursor (Hinstance, 'mycur'); where 35 is a constant greater than 1, Mycur is the name in the resource file. If you want to use custom cursors on other controls, such as the Panel control, simply add the following code at the appropriate place: Panel1.cursor: = 35; <2> Calling a new project, add a TIMAGE control, Write the following code to write: var mymap: hbitmap; begin mymap: = loadingbitmap (hinstance, 'mypic'); image1.picture.bitmap.handle: = mymap; end; where "MyPic" is in a bitmap resource file name.
<3> Avi file calls new project, add an Animate control, join: AnImater1.Resname: = 'myavi'; Animater1.Active: = true; where Myavi is the name of the video file in the resource file. <4> Call the WAV file to add the MMSYSTM unit in the USES to play the WAV file in the program. Play Sound (Pchar ('MyWav'), Hinstance, Sndsync or Snd_Resource; where MyWAV is the name in the resource. <5> The joining cursor is easier to join the cursor, just add the RES file to the unit file. However, it is important to note that the name is best taken "W". "WW", etc., so that the first letter is tried to by the main program, so as to avoid the icon sequence of the main program. In this way, there will be a lot of choice if others want to choose other icons when using your program. Supplement: 1. Resource Type In addition to the above type, you can font file, string file, etc. 2. Resource files can not only be used under the standard graphics interface. Let's take a look at: New project, remove the only form, and then modify the project file. Add a sentence {$ apptype console}, add MMSystem in the USES clause and delete other reference units. Delete the statement between Begin and End. At this point, we can call the Turbo Pascal, and you can call the Windows API and resources. Join the resource file ---- {$ r myfist.res}. Write down between BeGin and End: WriteLn ('Demo, press any key!'); Readln; Plays (Pchar ('mywav'), hinstance, snd_sync or snd_resource; Writeln ('Demo end!'); Run the program, a standard DOS window will pop up, press any key to play the sound file. Is it very cool! I have downloaded a player. I found a "DOS program" in its installation directory. Double-click it with the mouse to pop up a DOS window, display the DOS era unique drawing, and background music! It may be done with this method. 3. Delphi itself comes with a tool called Image Editor, which can also edit the resource text, but compared to the method of this article, it can be obtained: **************** ************************** Image Editor Brcc32 BMP only supports 16 color arbitrary color cursor black and white two-color free color ICO only support 16 color arbitrary color AVI does not support support WAV does not support the support font string does not support other ******************************************************************** * The above is the call directly in the program itself. In fact, there are other usage of resource documents. For example, in your program carries other files, release it when you use it.
For example: Myexe Exefile 'ha1.exe' // Scripting The following is the custom release function ExtractRES, this example is used as follows: extractres ('exefile', 'myexe', 'c: /new.exe'); just put HA1 .EXE Save to the C-drive root directory with new.exe. Function TForm1.extractres (Restype, ResName, ResName: String): Boolean; Var Res: TresourceStream; Begin Try Res: = TRESOURSTREAM.CREATE (Hinstance, ResName , PCHAR (RESTYPE)); try res.savetofile (result: = true; finally res.free; end; eXcept results: = false; end; --------------- -------------------------------------------------- ----- (2) Intermediate Application: So we already know how to read a BMP image from the resource file, but the BMP file is too large, the JPG file application is relatively. So how to read JPG images Come out? It can be accessed with a resource file. The specific method is as follows: (1) Myjpg jpeg my.jpg (2) Var Stream: TSTREAM; Myjpg: tjpegimage; begin stream: = TRESOURSTREAM.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; reading other image files is also the same. For example, GIF animation file, of course The premise is that you have a gif.pas, this unit has a lot of sites, you can find it yourself. In practical applications, I also found that the above code can directly display ICON and BMP in the resource file. Say the graphic processing, actually created with Delphi, call the pure icon resource DLL. For example, you can take a look at Super Sacreda The DLL under the directory is a pure icon resource. The specific method is as follows: (1): Create a Hicon.Res file, no longer repeat. (2): New text file icon.dpr, the content is as follows: library icon; {$ r icon.res} begin end. With Delphi Open the compile to get icon.dll. (3): The actual calling method is as follows: ... private hinst: thandle; ... var Hicon: thandle; begin hinst: = loadingLibrary ('icon.dll '); If hic: = loading (Hinst, Pchar (Edit1.Text)); if Hicon <> 0 Then Image1.Picture.icon.Handle: = Hicon; FreeElibrary (Hinst); End; if Your program wants to use people to use people with different languages to use DLL to store character resources. Because the DLL can be modified casually, especially if you want to save some copyrights, especially if you want to save some copyrights. If the information, it is better to use a DLL.
For example, you are ready to develop a "Chinese Character Simple Body Translator" software, ready to provide GB32, BIG5 code and English menu to users, then you can try DLL to save character resources. We need to build three DLLs. First Rc file writing step of course, give Gb32 code, for example, as follows: /*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 script interpreter" IDS_BtnOpen_Caption, "open file" IDS_BtnSave_Caption, "save file" IDS_BtnBig5_Caption, "converted to Big5" IDS_BtnGb32_Caption, "converted into Gb32" IDS_BtnHelp_Caption, "help" IDS_Help_Shelp, "enter text or open After the file, click the button to convert it as needed! "} The second step is that the BRCC32 is compiled as a res file to get a DLL file with the above method, and the other two DLLs are generated in the same way. Below to apply: New project, put The upper five Button, BtNSave, btnbig5, btngb32 and btnhelp. There is also a TcomboBox: CBSELECT is used to select language type. 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 phase Dll files should be reading the corresponding character end; procedure TForm1.FormCreate. (Sender: TObject); begin CbSelect.Items.AddStrings (SearchLanguagePack); // search directory of all current Dll file names 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; // Read characters resources, 1 Represents 1 LoadString defined in the resource file (HDLL, 1, MyChar, 254); Self .Caption: = mychar; LOADSTRING (HDLL, 2, Mychar, 254); btnopen.caption: = mychar; loadingString (HDLL, 3, Mychar, 254); btnsave.caption: = mychar; loadingString (hdll, 4, mychar, 254); btnbig5.caption: = mychar; LOADSTRING (HDLL, 5, MyChar, 254); btngb32.caption: = mychar; loadingString (HDLL, 6, Mychar, 254); btnhelp.caption: = mychar; loadingString (HDLL, 7, mychar, 254); shelp: = mychar; freeelibrary (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 Appl Ication.MessageBox (Pchar (shelp), 'http://lovejingtao.126.com', MB_ICONInformation); End; End. This method is not as good as I have directly defined three specific values in the program. It is convenient. Even my own custom structure is good, don't use DLL so troublesome. But if your program is to use, there are many characters. For example, the Windows operating system, itself has simple Chinese, traditional Chinese, English, etc., use the DLL, just replace the DLL directly, without having to open a version every release. Code is modified once.
This can greatly reduce the opportunity of work and error. Say it, then say more: The Windows system itself has a lot of DLLs with pictures, we can call directly in the program, so that our EXE can also Reduce a lot! Of course, the smallest method is to generate a real-time technology. Foreigners have written a 67kb program to use this method. Interested friends can go to http://go4.163.com/lovejingtao/ha1.exe download. -------------------------------------------------- -------------------- (3) Advanced Application: Delphi is a very efficient development tool, but it has a shortcoming that the generated EXE file is too large. A program is that there is only one empty window volume and 286KB. If the voice volume is written directly with the API, it is too cumbersome, and it is too cumbersome to see the interface effect. It is not a visual development. In fact, not "fish and bear The palm is uncomfortable. We can easily achieve this purpose easily. Before you start, we need a tool to edit the resource file. This type of tool is much more, such as resource workshop is very good. If you can't find it It is also possible to use the VC editor. Below we will show how to create a window resource file with VC. Run VC, open menu File -> New, will have a number of selection pages. We choose Files -> ResourceTemplate, fill in the right of the file fill in Demo, Location Select the save path, then click the button OK to return to the VC development environment. Select Menu INSERT -> Resource, there will be a resource type selection box. We will move your mouse to Dialog. No need to expand, click on the new NEW, then return the VC development environment and there is a form with only the shutdown button and two Button. Select the form, right click to select the last Properties, one setting will appear Window, change the ID to "Mainform" (Note: Different from the property setting method of other controls added below, the ID of the main window must write the double quotes and must be uppercase. Otherwise, the program will not find resources. Program I will run again.) CAPTION is changed to "Installer", at this time, see the title of the window becomes "installer", select the STYLES's Minimize Box, more styles's center hook to run the program The location of the time. Of course, you can also set its coordinates. Other reserved default values. Back to the development environment, select a Stati in the control box C Text, an Edit Box, a Button and a Group Box Add to the form. Put them all in their own hobby. Then modify their properties one by one. The method is to right click to select the last one. Item Properties, modified in the properties box that appears. The properties are as follows: Group box's CAPTION property is emptied, and Static Text's CAPTION attribute is changed to "Please select the installation directory:", the ID Box ID is changed to 10001. The first Button The ID is 10002, the CAPTION property is "Select", the second Button ID is 10003, the CAPTION property is "installation", the third Button ID is 10004, the CAPTION property is "exit". In order to make the program more perfect, we Add a menu IDR_MENU1 to it. Select Insert -> Resource -> Menu, we only add a "file -> exit" here, where "Exit" ID is 10005. Then in the main window attribute Menu Set to IDR_MENU1. In order to make the program more beautiful, we add a small icon, this will also be the icon of our program. Select Insert -> Resource -> Icon -> Import, select an icon file. And set it to "mainicon"
(Note: Must write double quotes and letters to uppercase, add a Picture control to the window and set its properties Type: icon, Image drop-down Select the icon MAINICON. If you want to add some information to the program It is also possible. Select Insert -> Resource -> Version. Here we have completed a simple "installer" form design. In fact, we can call it in Delphi now. We first Save "Labor Achievements". Select File -> Save As, select "32-bit resource file (.res)" in the file type to save to "demo.res", file size is approximately 2.65KB. New an extension Text file named DPR MyDemo.dpr, type code: Uses Windows, Messages; {$ r demo.res} Function MainDialogProc (dlgwin: hwnd; dlgMessage: uint; dlgwparam: wparam; dlglparam: lparam): integer; stdcall; Begin Result: = 0; Case DLGMESSAGE OF WM_CLOSE: Begin PostquitMessage (0); EXIT; End; End; End; Begin Dialogbox (Hinstance, 'Mainform', 0, @MAINDIALOGPROC); END. Open it once with Delphi. Produce an exe of a size of 19kb. Is it very small! In fact, you even use a line of code to show it, but the program cannot be turned off. Uses windows; {$ r demo.res} function mainogproc: integer; begin Result: = 0; End; Begin DialogBox (Hinstance, 'Mainform', 0, @MAINDIALOGPROC); END. The above program is just an empty window, now let's write the code response to press the corresponding button response. Complete The code is as follows: Program MyDemo; Uses Windows, Messages, Shlobj; Const ID_EDit = 10001; ID_SELET = 10002; ID_SETUP = 100 03; ID_Quit = 10004; ID_Exit = 10005; {$ R Demo.Res} var MainWin: HWND; function My_Gettext: string; var Textlength: Integer; Text: PChar; s: string; begin TextLength: = GetWindowTextLength (GetDlgItem (MainWin, ID_EDIT); getMem (Text, TextLength 1); getWindowText (Getdlgitem (mainwin, id_edit), text, textLength 1); s: = text; freemem (text, textLength 1); result: = s; end; Function getMyname: String; Var i, j: integer; begin j: = 3; for i: = 1 to length (paramstr (0)) DO if paramstr (0) = '/' THEN J: = i; result: = Copy (Paramstr (0), J 1, Length (paramstr (0)) - J); END;
function SelectDirectory (handle: hwnd; const Caption: string; const Root: WideString; outDirectory: string): Boolean; var lpbi: _browseinfo; buf: array [0..MAX_PATH] of char; id: ishellfolder; eaten, att: cardinal ; rt: pitemidlist; initdir: pwidechar; begin result: = false; lpbi.hwndOwner: = handle; lpbi.lpfn: = nil; lpbi.lpszTitle: = pchar (caption); lpbi.ulFlags: = BIF_RETURNONLYFSDIRS BIF_EDITBOX; SHGetDesktopFolder ( ID); initdir: = pwchar (root); id.parsedisplayName (0, nil, initdir, eaten, rt, att); lpbi.pidlroot: = rt; getmem (lpbi.pszdisplayname, max_path); try results: = shGetpathFromidList ( shbrowseforfolder (lpbi), buf); except freemem (lpbi.pszDisplayName); end; if result then begin directory: = buf; if length (directory) <> 3 then directory: = directory '/'; end; end; function MainDialogProc (DlgWin: hWnd; DlgMessage: UINT; DlgWParam: WPARAM; DlgLParam: LPARAM): integer; stdcall; var MyIcon: HICON; Sdir: string; begin Result: = 0; case DlgMessage of WM_INITDIALOG: begin MyIcon: = L oadIcon (hInstance, 'MainIcon'); SetClassLONG (DlgWin, GCL_HICON, MyIcon); MainWin: = DlgWin; end; WM_Close: begin PostQuitMessage (0); Exit; end; WM_COMMAND: case LOWORD (DlgWParam) of ID_Selet: begin if SelectDirectory (DLGWIN, 'Please select the installation directory', '', sdir). SendMessage (Getdlgientm (DLGWIN, ID_EDIT), WM_SETTEXT, 0, LPARAM (PCHAR (SDIR))); end; id_setup: begin if my_gettext = '' Then Begin MessageBox (DLGWIN, "first select the installation folder! ',' Information ', MB_ICONITION MB_OK); EXIT; End; CopyFile (Pchar (Pramstr (0)), Pchar (My_Gettext getMyname), False; MessageBox (DLGWIN , 'Installation!', 'Information', MB_ICONITION MB_OK);