Delphi Use resources file all the strategy

Source: Internet
Author: User

Reprinted from: http://www.cnblogs.com/nokiaguy/archive/2008/05/16/1201100.html

In general, the use of Delphi design program, is the string, image, and other resources directly using the VCL provided by Delphi Control to *.DFM, so that the convergence of the modification of these resources is inconvenient, if the resources are referenced more than once, these resources are loaded into memory when the program starts, system resources are very expensive. Therefore, this requires a new resource-referencing file: A resource file. The resource file compiles some resources, such as strings, images, etc., and then references the compiled resource file in the program, and finally compiles the executable file with the source program. Because the resources in the resource file are loaded when needed, it saves system resources, and if you want to do an internationalized version of the system, you just need to change the resource files and recompile. The following is a detailed description of Delphi in the establishment and use of resource files.

I. Establishment of resource documents

The source file for the resource file is an ordinary text file with the extension *.rc. After the contents of the resource file are written to the file, call Brcc32.exe to compile it and generate the *.res file. Brcc32.exe is in Delphi's Bin directory.

Second, string resources

The string resource actually corresponds to a string and a numeric index, and when the string resource is loaded, the numeric index corresponding to the string resource needs to be provided. The string resource file begins with stringtable and encloses the resource definition with a pair of begin and and. The format of the string resource is: Numeric index, resource string.
String.rc File Contents
Stringtable
Begin
1001, "New"
1002, "Save"
1003, "Off"
1004, "Help"
1005, "Exit"
1006, "Resource file Test"
End
Then call the command: Brcc32.exe string.rc, Generate String.res, and finally add {$R String.res} to the program. The *.rc files described below need to be compiled and referenced using a similar method.

There are two ways to load a string resource in Delphi.
1. Use the Windows API function LoadString. Use this function to define a char array, which is called in the following form.

LoadString (HINSTANCE, 1001, buffer, 100); Maximum length of 100:buffer array, 1001: Numeric index

2. Use Delphi's own function Loadstr. This function only needs a numeric index to get the corresponding string resource. The invocation is in the following form.

Button2. Caption: = Loadstr (1002);

iii. Resources of bitmap

Bitmap resources are much simpler to define. Only three items are defined for each bitmap resource: The bitmap identifies the bitmap type bitmap file name (these three items are separated by a space or tab) where the bitmap identity is similar to the numeric index of the string resource, except that the bitmap identifier can be a string. The bitmap type is bitmap.

There are two common methods for invoking bitmap resources.
1. Use the Loadfromresourcename method of the bitmap.
2. Use the Windows API function LoadBitmap. The code examples for this approach are as follows:
BMP: = Tbitmap.create;
Bmp. Handle: = LoadBitmap (hinstance, ' bmp ');
Rect. Left: = 0;
Rect. Top: = 0;
Rect. Right: = Image2. Width;
Rect. Bottom: = Image2. Height;
Rect1. Left: = 0;
Rect1. Top: = 0;
Rect1. Right: = BMP. Width;
Rect1. Bottom: = BMP. Height;
Image2. Canvas.copyrect (Rect, BMP. Canvas, Rect1);
The first method is relatively simple, but the second method works better if it is stretched. It is also possible to load the icon and cursor files in a similar way, where the load icon uses LoadIcon, which loads the cursor using LoadCursor.

iv. JPEG Resources

The Mount JPEG resource does not have a Windows API, but you can use the Tresourcestream class provided by Delphi. When you define a resource file, the resource type uses Rcdata. The sample code is as follows:
JPG: = tjpegimage.create;
Rstream: = Tresourcestream.create (hinstance, ' jpg ', rt_rcdata);
Jpg. Loadfromstream (Rstream);
Image3. Picture.Bitmap.Assign (jpg);

v. Loading arbitrary files

Resource files can store not only known types of resources, such as strings, bitmap, JPEG, etc., but also any type of file. In addition to strings, other types of resource files have a similar format, but vary from resource types. In addition to the known resource types, you can customize the resource type. JPEG resources can be used without rcdata and JPEG, but should be handled as follows when calling.
Rstream: = Tresourcestream.create (hinstance, ' jpg1 ', ' JPEG ');

Vi. loading resources from other programs

In the above example the resources are loaded from the current EXE, but sometimes need to load resources from another EXE or DLL instead of itself. For this requirement, just change hinstance to LoadLibrary (EXE or DLL name) in the above example.
Rstream: = Tresourcestream.create (LoadLibrary (' Project1.exe '), ' jpg1 ', ' JPEG ');
After loading, you can use Tresourcestream's savetofile to save the resource as a file, or to do other processing.

Delphi Use resources file all the strategy

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.