Definition and use of string resources in Delphi

Source: Internet
Author: User

Strings are stored independently in the application. The application is loaded only when resources are used.
In addition to memory saving, strings can also be used for translation. Some Chinese software use strings. Edited word
Put the string in a text file, you can use: file-> New-> text in Delphi to edit the string file, word
The format of the string file is as follows:

Stringtable
Begin
1. "book"
2. "apple"
3. "desk"
4, "pen"
5. "computer"
End

After editing the string file, select Save As. Note that you should change the file type to the resource compilation file (. RC), this is not a resource file, it must be compiled to become a resource file (. res ). The compiling command is brcc32 at the DOS prompt, and its path is D: Program filesborlanddelphi4binrcc32.exe. For example, the above string resource compilation file name is strres. RC: Enter brcc32 mydirstrres at the DOS prompt. RC; after compilation, a file named strres is generated. res resource file, which can be used to access string resources. For more information, see the following example:

Unit teststr;

Interface

Uses
Windows, messages, sysutils, classes, graphics, controls, forms, dialogs, stdctrls;

Type
Tform1 = Class (tform)
Button1: tbutton;
Label1: tlabel;
Procedure button1click (Sender: tobject );
Procedure formcreate (Sender: tobject );
Private
Count: integer;
Public

End;

VaR
Form1: tform1;

Implementation

{*. DFM}
{Strres. Res}

Const
Wordcount = 5;

Procedure tform1.button1click (Sender: tobject );
VaR
Strword: string;
Begin
If count> wordcount then
Begin
Count: = 1;
End;
Strword: = loadstr (count );
Label1.caption: = strword;
Count: = count + 1;
End;

Procedure tform1.formcreate (Sender: tobject );
Begin
Label1.caption: = loadstr (1 );
Count: = 2;
End;

End.

The constant wordcount in the program is used to record the number of strings in the string resource file, and the variable count is used to record
String number. After the program runs, click the button1 button. Each word in the string resource file is displayed cyclically.
String.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>

Another article

<<<

Different from other resources, string resources cannot be directly edited. You must first compile a text file in the format, and then use
Compile it into source files in sequence. The following is a simple example. First, compile a file lb. RC in a text editor,
The content is as follows:

Stringtable

Begin

1, "start"

2. "quit"

End

Find the brcc32.exe file in the binsubdirectory of delphiand compile it. The command format is brcc32.
LB. RC: After compilation, a resource file lb. Res is generated. To use this resource file, you need to import the resource file in the cell file imple.
The starting part of mentation includes the resource file <$ R lb. Res>. In this example, the above string is used
The command button replaces the caption settings. The function used is the Windows API function loadstring, and the following is the instance used:

// In the formcreat process:

VaR
Txtcaption: array [0 .. 255] of char;
Begin
If loadstring (hinstance, 1, txtcaption, sizeof (txtcaption)> 0 then
Begin
Btnstart. Caption: = strpas (txtcaption );
End;
End;

// In the btnstartclick process:

VaR
Txtcaption: array [0 .. 255] of char;
Begin
If loadstring (hinstance, 2, txtcaption, sizeof (txtcaption)> 0 then

Btnstart. Caption: = strpas (txtcaption );

End;

In this way, various attributes can be changed while the program is running, without the need for a string to appear in the program. If you use another
The Delphi function loadstr will be simpler:

VaR
Txtcaption: string;
Begin
Txtcaption: = loadstr (2 );
If txtcaption <> ''then
Begin
Btnstart. Caption: = txtcaption;
End;
End;

Perhaps we can see from the above process that using 123 to identify a string is a little simple, it is easy
So what should I do? We can use a string method like C ++ to predefine an ID for each string,
For example:

Const

IDC-Start = 1;

IDC-exit = 2;

Of course, you should put it in a unit (similar to C ++. h file), and then uses it in the unit in use.
Yes. Is the application very nice?

Txtcaption: = loadstr (IDC-exit );

Definition and use of string resources in Delphi

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.