We already know how to read a BMP image from the resource file, but the BMP file is too large.
There are many. How can I read JPG images? You can add the source file to the stream. The specific method is as follows:
(1) myjpg JPEG my. jpg
(2) var
Stream: tstream;
Myjpg: t‑image;
Begin
Stream: = tresourcestream. cceat (hinstance, 'myjpg ', 'jpeg ');
Try
Myjpg: = tsf-image. Create;
Try
Myjpg. loadfromstream (Stream );
Image1.picture. assignc (myjpg );
Finally
Myjpg. Free;
End;
Finally
Stream. Free;
End;
End;
Reading other image files is the same. For example, if you have a GIF. Pas file, there are many units.
All websites are available. You can search for them by yourself. In actual application, I also found that the above Code can directly display the icons and BMP in the resource file.
When it comes to graphic processing, you can also use Delphi to create and Call DLL resources of pure icons. For example, you can look at the super solution.
Many DLL files are pure ICON resources. The specific method is as follows:
(1): Create a hicon. Res file, which is not repeated here.
(2) create a text file icon. DPR with the following content:
Library icon;
{$ R icon. Res}
Begin
End.
Open and compile with Delphi to get the icon. dll.
(3): The actual call 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 is to be used internationally by people in different languages, it will be a good way to store character resources with DLL. Because
The DLL is not as changeable as the INI file, especially if you want to save some copyright information, it is better to use the DLL.
. For example, you are preparing to develop a "simplified Chinese character and Traditional Chinese translator" software and provide the gb32, big5, and English menus to users,
Then you can try to use DLL to save character resources.
We need to create three DLL. The first step is to write the RC file. Take the gb32 code as an example. The content is 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, "simplified Chinese character traditional translator"
Ids_btnopen_caption, "Open File"
Ids_btnsave_caption, "saving files"
Ids_btnbig5_caption, "convert to big5"
Ids_btngb32_caption, "convert to gb32"
Ids_btnhelp_caption, "help"
Ids_help_shelp, "Enter text or open the file and click the button as needed to convert it! "
}
The second step is to compile brcc32 into the res file and use the above method to obtain the DLL file. The other two DLL files are generated using the same method. Below
To apply:
Create a new project with five buttons: btnopen, btnsave, btnbig5, btngb32, and btnhelp.
Tcombobox: cbselect is used to select the language type.
The Code is as follows:
Unit unit1;
Interface
......
Private
Shelp: string;
Function searchmediaagepack: tstrings;
Procedure setactivelanguage (agename: string );
{Private Declarations}
......
Implementation
Procedure tform1.cbselectchange (Sender: tobject );
Begin
Setactivelanguage (cbselect. Text); // call the corresponding DLL file to read the corresponding characters.
End;
Procedure tform1.formcreate (Sender: tobject );
Begin
Cbselect. Items. addstrings (searchmediaagepack); // search for the names of all DLL files in the current directory
End;
Function tform1.searchmediaagepack: 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 character resources. 1 indicates 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, MySQL char, 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.
You may say that this method is not as convenient as defining three specific values in the program. Even I customize it myself
A structure is good, so it is not as troublesome to use DLL. But what if your program uses many characters? For example, Windows Operating System
System, including simplified Chinese, traditional Chinese, and English versions. If you use a DLL, you only need to replace the DLL directly, instead of having to release one version each.
Open the code to modify it once. This can greatly reduce the workload and opportunities for errors. Speaking of this, let's talk about windows.
Many DLL resources such as images are included in the system. We can call them directly in the program, so that our EXE can be reduced a lot!
Of course, the best method is real-time generation technology. A foreigner once wrote a program of KB. This method is used. Interested friends can
Download the package at http://go4.163.com/lovejingtao/ha1.exe.
Resource file-intermediate application