![]() |
![]() |
|||||
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
|
Q: Add string tables to a BCB projectAnswer: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 RC text file that contains your stringtable, then you add the RC file to your project and load the strings from code. Step 1: Creating the RC fileCreate 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 RC file extension (RES.RC). Enter your stringtable into the RC file. The format goes like this: #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 The keywords STRINGTABLE, BEGIN, and END are necessary. The string ID's IDS_STRING1, IDS_STRING2, and IDS_STRING3 identify each string. These string ID's are #define'ed in RES.H 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. Step 2: Creating the a resource header fileThe 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 RC file and your source files. Create another text file that will serve as the resource include file. Save the file with a .H extension (RES.H). Edit the file so it resembles the segment below. Each string resource ID from the RC file should contain a corresponding #define in the header file. #ifndef RES_H #define RES_H #define IDS_STRING1 (100) #define IDS_STRING2 (101) #define IDS_STRING3 (102) #endifStep 3: Add the RC file to your BCB project 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 RC file and click OK. Use the Project Manager to view the nodes of the project, and verify that the RC file is in the list. Step 4: Load the strings in codeThe code below demonstrates three methos for loading a stringtable string into an AnsiString object. Make sure that you insert a #include for the resource header file so that your code can see the #define statements for the string resources. To use this code, place three labels onto the mainform of your program. #include <vcl\vcl.h> #pragma hdrstop #include "Unit1.h" #include "res.h" // include string resource IDs //--------------------------------------------------------------------------- #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { // Load the string using the API LoadString function char buf[255]; ::LoadString(HInstance, IDS_STRING1, buf, 255); Label1->Caption = buf; // Load the string using the static AnsiString LoadStr method str = AnsiString::LoadStr(IDS_STRING2); Label2->Caption = str; // Load the string directly into the label Label3->Caption = AnsiString::LoadStr(IDS_STRING3); } Note: LoadString is an API function. The first parameter is the HINSTANCE 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 LoadString will copy the string into. The last parameter is the size of the buffer. Note: The LoadStr method of AnsiString is a static member function that loads the string table string and returns it in an AnsiString object. LoadStr is probably the best method of loading a string resource, since it will handle the details of allocating enough space to hold the string. There is also a FmtLoadStr method that you might want to check into. It allows you to format the string as you load it in. Note: Notice that IDS_STRING3 contains a \xA9 in its string. This inserts the copyright symbol (C) into the string. Note: 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 #define'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 #define value. This is an efficiency issue with the operating system. | ||||||
All rights reserved. |