Make good use of resource files

Source: Internet
Author: User

Resources are binary data structures stored in files with the extension. res. You can use the image editor
Create (imageeditor) or use other tools. For example, in radpackfordelphi provided by Borland
Resourceworkshop to create. Resource files usually store objects that can be accessed by applications at any time, including: icon,
There are nearly 10 cursor, bitmap, and Font types. Most resources can be stored in the disk normally until the program needs
You can use them to call in, which can greatly save memory resources. Similarly, resource files can also be shared, that is, multiple programs can
Sharing a resource file reduces code duplication between applications and greatly optimizes the program code.
Resource files used in applications have the advantages of independent creation, convenience, and modification at any time without any modification to the application code.
In general, all resources of an application are stored in a res file, and then the resources are stored in the compilation stage of Delphi.
The content of the source file is merged with the final EXE file. Therefore, the compiled res file does not have any effect and is only provided for reference.
When distributing an application, you do not have to distribute the res file to the end user. Of course, different resources can be placed in different resource files.
And the smaller the resource file, the faster the memory is to be transferred. Here, we also need to note that, after modifying the original resource file
The resources added to the original compiled EXE file will not be updated accordingly. Only after re-compilation can the new resources
Add it to the new EXE file.
The following two examples illustrate how to use resource files in Delphi (here we will not describe how to create resource files ).

1. How to use a custom cursor in Delphi.

When using Delphi for programming, there is a very important object-tscreen, which is used to manage and manipulate the screen during runtime.
Is not visible. Its cursor attribute is used to specify the cursor shapes of different components. It is known:

Property cursors [index: integer]: hcursor;

This read-only feature returns an hcursor type array consisting of the cursor supported by the application, which stores all
The identity code of the mouse cursor (hcursor is the handle of the cursor). The array subscript starts from 0 and has been predefined in Delphi.
For constants with different cursors, the value ranges from 0 to-17. You can directly specify the cursor used by the program. For example:

Form1.cursor: =-3 indicates that form1 uses the crcross cursor ).

You may find that the component's cursor attribute provides up to 18 common cursor values, which is not enough in many cases.
For example, when the cursor enters the shape of a panel time mark as a hand, the Panel cursor cannot meet this requirement,
In this case, we need to customize the cursor of a hand and assign it to the panel's cursor attribute. Follow these steps:

1) use the resource file editor (image editor or resource Workshop) to write resource files.

Defines a cursor constant. Note that this constant cannot conflict with the cursor constant provided in Delphi. In the formcreate event
Use the loadcursor function in windowsapi to load the custom cursor.

2) Assign the custom cursor to the panel's cursor attribute in the program.

The following describes how to assign the mouse and cursor of a custom hand to the cursor attribute of panel1. First, use imageeditor to create
A hand. Res file. Then write the program code as follows:

Implementation

{$ R *. DFM}

Const
Crhand = 2;

Procedure tform1.formcreate (Sender: tobject );
Begin
Screen. cursors [crhand]: = loadcursor (hinstance, 'hand ');
Panel1.cursor: = crhand;
End;

In addition to writing the above Code, we also need to do one of the work is to add the resource file to the project file, compile the command
In the {$ rfilename} pseudocommand, let's add a resource file. Let's take a look at the code of the project file.

Program pcur;

Uses
Forms,
Handin 'hand. pa' {form1 };

{$ R *. Res}
// Here * indicates that a resource file with the same name as the project file will be generated after the program is compiled. Here it is the pcur. Res file
// Points are automatically generated.

{$ Rhand. Res}
// This part is the custom resource file we added. Both resource files (Resources in hand. Res and // pcur. Res are attached
// After the executable file pcur. EXE.

Begin
Application. initialize;
Application. createform (tform1, form1 );
Application. Run;
End.

2. Application of resource files in animation Programming

The above example uses the cursor resource stored in the resource file. In the following example, we will use
To compile a small animation program. In this case, the animation is generated using the timer at a certain interval.
The timer event is generated when the icons stored in the resource file are sequentially drawn at the same place of the form. Canvas is used.
The draw method is declared as follows:

Proceduredraw (X, Y: integer; graphic: tgraphic );

The graphic parameters in this method can be bitmaps, icons, and metafiles. The specific implementation method is: Establish
The demo. Res file contains an icon named icon1 consumer con6. Add demo. Res to the original project code (for example
Method ).

The specific code of the animation program is as follows:

Unit Donghua;

Interface

Uses
Windows, messages, sysutils, classes, graphics, controls, forms,
Dialogs, extctrls;

Type
Tform1 = Class (tform)
Image1: timage;
Timer1: ttimer;
Procedure formcreate (Sender: tobject );
Procedure timer1timer (Sender: tobject );
Private
{Privatedeclarations}
Public
{Publicdeclarations}
End;

VaR
Form1: tform1;

Implementation

VaR
Wicon: array [0 .. 5] of ticon;
// Create an array storage icon file of the ticon type
Idx: integer;

{$ R *. DFM}

Procedure tform1.formcreate (Sender: tobject );
VaR
Iconname: string;
Piconame: pchar;
Begin
Piconame: = stralloc (7 );
// Create a pchar string
For idx: = 0 to 5 do
Begin
Wicon [idx]: = ticon. Create; // create a ticon object
Iconname: = 'icon '+ inttostr (idx );
Strpcopy (piconame, iconname); // convert string type to pchar type
Wicon [idx]. Handle: = loadicon (hinstance, piconame); // call the loadicon function in windowsapi
// Load the icon in the Resource
End;
Strdispose (piconame); // delete a pchar type
Form1.canvas. Draw (, wicon [1]); // call the draw method to draw icon1 on the form.
Idx: = 1;
Form1.setbounds (100,100,); // fixed form size and position
End;

Procedure tform1.timer1timer (Sender: tobject );
// Create an animation timer event
Begin
Idx: = idx + 1;
If idx = 6 then idx: = 1;
Form1.canvas. Draw (3, 3, wicon [0]);
Form1.canvas. Draw (3, 3, wicon [idx]);
End;

End.

The above can be used as a general animation program. We only need to modify different icons and increase the idx size to compile
The same animation application (there can also be other forms of graphics in the resource, such as bitmaps. In this case, the tbitmaps object needs to be dynamically created and
Is not a ticon object ). In the two examples, we use windowsapi functions, which are described in the Windows unit in
I will not go into details here. The above two examples are just a little bit of Application of resource files. To truly master the strong knowledge of resource files in programming
The main function is to constantly summarize it in practice.

Make good use of resource files

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.