📄 faq47.htm
字号:
<HTML>
<HEAD>
<TITLE>Add string tables to a BCB project</title>
<META NAME="Author" CONTENT="Harold Howe">
</HEAD>
<BODY BGCOLOR="WHITE">
<CENTER>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH="640">
<TR>
<TD>
<H3>
Add string tables to a BCB project
</H3>
<P>
Many BCB programmers complain that C++Builder doesn't come with a graphical tool for
working with stringtable resources. However, this isn't a serious drawback because
stringtables are easy to create by hand. The process goes like this: first you
create an <TT>RC</TT> text file that contains your stringtable, then you add the
<TT>RC</TT> file to your project and load the strings from code.
</P>
<B>Step 1: Creating the <TT>RC</TT> file</B>
<P>
Create a new text file using the editor of your choice. I used BCB's Object
Repository to handle this by selecting the text object under the File | New menu
command. Save the file using an <TT>RC</TT> file extension (<TT>RES.RC</TT>).
Enter your stringtable into the <TT>RC</TT> file. The format goes like this:
</P>
<PRE>
#include "res.h"
STRINGTABLE
BEGIN
IDS_STRING1 "Good Day, and welcome to String 1";
IDS_STRING2 "Good Day, and welcome to String 2";
IDS_STRING3 "Beauty, eh? \xA9 1998";
END
</PRE>
<P>
The keywords <TT>STRINGTABLE</TT>, <TT>BEGIN</TT>, and <TT>END</TT> are necessary.
The string ID's <TT>IDS_STRING1</TT>, <TT>IDS_STRING2</TT>, and <TT>IDS_STRING3</TT>
identify each string. These string ID's are <TT>#define</TT>'ed in <TT>RES.H</TT>
which is the next step I'll cover. Of course, the strings contained within
the quotes are the actual strings that we want to load and display.
</P>
<B>Step 2: Creating the a resource header file</B>
<P>
The string ID's from Step 1 must be assigned unique integer values. The best place
to assign the integer values is inside a header file that can be included by both
the <TT>RC</TT> file and your source files. Create another text file that will serve as the
resource include file. Save the file with a <TT>.H</TT> extension (<TT>RES.H</TT>).
Edit the file so it resembles the segment below. Each string resource ID from the
<TT>RC</TT> file should contain a corresponding <TT>#define</TT> in the header file.
</P>
<pre>
<font color="green">#ifndef RES_H</font>
<font color="green">#define RES_H</font>
<font color="green">#define IDS_STRING1 (100)</font>
<font color="green">#define IDS_STRING2 (101)</font>
<font color="green">#define IDS_STRING3 (102)</font>
<font color="green">#endif</font>
</pre>
<B>Step 3: Add the <TT>RC</TT> file to your BCB project</B>
<P>
This is easy enough. Select the Project | Add To Project menu option from the BCB
IDE. This menu option brings up a File Open dialog. Select the <TT>RC</TT> file
and click OK. Use the Project Manager to view the nodes of the project, and verify
that the <TT>RC</TT> file is in the list.
</P>
<B>Step 4: Load the strings in code</B>
<P>
The code below demonstrates three methos for loading a stringtable string into an
<TT>AnsiString</TT> object. Make sure that you insert a <TT>#include</TT> for the
resource header file so that your code can see the <TT>#define</TT> statements for
the string resources. To use this code, place three labels onto the mainform of
your program.
</P>
<pre>
<font color="green">#include <vcl\vcl.h></font>
<font color="green">#pragma hdrstop</font>
<font color="green">#include "Unit1.h"</font>
<font color="green">#include "res.h" </font><font color="navy">// include string resource IDs</font>
<font color="navy">//---------------------------------------------------------------------------</font>
<font color="green">#pragma resource "*.dfm"</font>
TForm1 <b>*</b>Form1<b>;</b>
<font color="navy">//---------------------------------------------------------------------------</font>
<b>__fastcall</b> TForm1<b>:</b><b>:</b>TForm1<b>(</b>TComponent<b>*</b> Owner<b>)</b>
<b>:</b> TForm<b>(</b>Owner<b>)</b>
<b>{</b>
<font color="navy">// Load the string using the API LoadString function</font>
<b>char</b> buf<b>[</b><font color="blue">255</font><b>]</b><b>;</b>
<b>:</b><b>:</b>LoadString<b>(</b>HInstance<b>,</b> IDS_STRING1<b>,</b> buf<b>,</b> <font color="blue">255</font><b>)</b><b>;</b>
Label1<b>-></b>Caption <b>=</b> buf<b>;</b>
<font color="navy">// Load the string using the static AnsiString LoadStr method</font>
str <b>=</b> AnsiString<b>:</b><b>:</b>LoadStr<b>(</b>IDS_STRING2<b>)</b><b>;</b>
Label2<b>-></b>Caption <b>=</b> str<b>;</b>
<font color="navy">// Load the string directly into the label</font>
Label3<b>-></b>Caption <b>=</b> AnsiString<b>:</b><b>:</b>LoadStr<b>(</b>IDS_STRING3<b>)</b><b>;</b>
<b>}</b>
</pre>
<P>
<B>Note:</B> <TT>LoadString</TT> is an API function. The first parameter is the
<TT>HINSTANCE</TT> of the application that contains the string resources. The
second parameter is the resource ID of the string that you want to load. The
third parameter is a pointer to a C style buffer that <TT>LoadString</TT> will
copy the string into. The last parameter is the size of the buffer.
</P>
<P>
<B>Note:</B> The <TT>LoadStr</TT> method of <TT>AnsiString</TT> is a static member
function that loads the string table string and returns it in an AnsiString object.
<TT>LoadStr</TT> is probably the best method of loading a string resource, since
it will handle the details of allocating enough space to hold the string.
</P>
<P>
There is also a <TT>FmtLoadStr</TT> method that you might want to check into. It
allows you to format the string as you load it in.
</P>
<P>
<B>Note:</B> Notice that <TT>IDS_STRING3</TT> contains a <TT>\xA9</TT> in its
string. This inserts the copyright symbol (C) into the string.
</P>
<P>
<B>Note:</B> According to Rector and Newcomer's Win32 API book, you should try to
assign the string resource IDs in consective order. This just means that the first
string in the stringtable should be <TT>#define</TT>'ed as the lowest number in
the header file, and that IDs should gradually increase until you get to the last
string. The last string should have the highest <TT>#define</TT> value. This is an
efficiency issue with the operating system.
</P>
</TD> </TR>
</TABLE>
</CENTER>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -