Visual C++ 中的资源 / Resource in visual C++

Visual C++ 中的资源 / Resource in visual C++

Table title
 FileID0803 
 Create08-06-06 PM
 AuthorJoephia 
 LastUpdate08-06-06PM 

摘要:
  1. 使用Visual C++ 中的资源
  2. Windows中的资源类型
  3. 编译资源和代码页的相关支持
  4. 地方化资源文件
  5. 地方化对话框
正文:

使用 Visual C++ 中的资源 / Using LoadResource

在下面的代码中, IDR_DATA1 是资源的 ID , RT_RCDATA 是资源的类型 , 在目录中显示为 Data (没有引号) , 如果是自定义类型则类型名会有引号 , 则 RT_RCDATA  应该换成例如 "BIN" (目录中显示为 "BIN" 有引号)

    HINSTANCE ghInstApp = AfxGetInstanceHandle();
    HRSRC hRsrc = FindResource(ghInstApp, MAKEINTRESOURCE(IDR_DATA1),RT_RCDATA);
    ASSERT(hRsrc);
    DWORD len = SizeofResource(ghInstApp,hRsrc); 
    ASSERT(len);
    HGLOBAL hGlobal = LoadResource(ghInstApp,hRsrc); 

    CFile file("abc.txt", CFile::modeCreate | CFile::modeWrite);
    file.Write(hGlobal,len);
    file.Close();


 
在Visual c++ 的资源编辑器中有这样的目录
 

XXXXXX resource
    Accelerator
    "BIN"
        IDB_BIN1
    Bitmap
        IDB_BITMAP1
    Cursor
        IDC_CURSOR1
    Data
        IDR_DATA1
    Dialog
        IDD_DIALOG1
    Html
        IDR_HTML1
    Icon
        IDR_ICON1
    Menu
        IDR_MENU1
    String Table
        String Table
    ToolBar
        IDR_TOOLBAR1
    Version
        VS_VERSION_INFO

其中带引号的是系统未定义的类型,不带引号的则是已经定义的类型
定义如下

        

#define RT_CURSOR           MAKEINTRESOURCE(1)          //Cursor
#define RT_BITMAP           MAKEINTRESOURCE(2)          //Bitmap
#define RT_ICON             MAKEINTRESOURCE(3)          //Icon
#define RT_MENU             MAKEINTRESOURCE(4)          //Menu
#define RT_DIALOG           MAKEINTRESOURCE(5)          //Dialog
#define RT_STRING           MAKEINTRESOURCE(6)          //String Table
#define RT_FONTDIR          MAKEINTRESOURCE(7)          //未知
#define RT_FONT             MAKEINTRESOURCE(8)          //Font
#define RT_ACCELERATOR      MAKEINTRESOURCE(9)          //Accelerator
#define RT_RCDATA           MAKEINTRESOURCE(10)         //Data
#define RT_MESSAGETABLE     MAKEINTRESOURCE(11)         //未知

#define DIFFERENCE          11
#define RT_GROUP_CURSOR     MAKEINTRESOURCE((DWORD)RT_CURSOR + DIFFERENCE)          //未知
#define RT_GROUP_ICON       MAKEINTRESOURCE((DWORD)RT_ICON + DIFFERENCE)            //未知
#define RT_VERSION          MAKEINTRESOURCE(16)         //Version
#define RT_DLGINCLUDE       MAKEINTRESOURCE(17)         //未知
#if(WINVER >= 0x0400)
#define RT_PLUGPLAY         MAKEINTRESOURCE(19)         //PLUGPLAY
#define RT_VXD              MAKEINTRESOURCE(20)         //VXD
#define RT_ANICURSOR        MAKEINTRESOURCE(21)         //ANICURSOR
#define RT_ANIICON          MAKEINTRESOURCE(22)         //ANIICON
#endif /* WINVER >= 0x0400 */
#define RT_HTML             MAKEINTRESOURCE(23)         //Html

Windows Resource Files

You should place every element of the user interface that needs to be localized in a Windows resource file, including pictures, strings, messages, menus, dialog boxes, and version information. The table below lists the individual resource elements defined by Windows.

Individual Resource Files Defined by Windows

Resource TypeElementFile FormatComment/ Description
RT_CURSORCursor.CUR#include in .RC file
RT_BITMAPBitmap or toolbar.BMP#include in .RC file
RT_ICONIcon.ICO#include in .RC file
RT_MENUMenu or pop up menu.RC#include in .RC file
RT_DIALOGDialog.DLG or .RC#include .DLG file in .RC file
    
RT_STRINGString.RC 
RT_FONTDIRFont.FNT 
RT_FONTFont.FNT 
RT_ACCELERATORSAccelerator.RC 
RT_RCDATAUser-defined resource.RCCan use for constants or application specific structures
    
RT_MESSAGETABLEMessages.MC#include compiled message table in .RC file
    
RT_GROUP_CURSORCursorN/AGenerated internally by resource compiler to provide Windows with information about cursor's resolution and type
    
RT_GROUP_ICONIconN/AGenerated internally by resource compiler to provide Windows with information about icon's resolution and type
    
RT_VERSIONVersion information.RC 
    
RT_DLGINCLUDEHeader file that contains menu and dialog box #define statements.RCUsed by resource editing tools; Visual C++ uses its own mechanism tools;

编译资源和代码页的相关支持 / Compiling Resources

Compiling resource files using Visual C++ 2 requires clicking the Build button on the toolbar. As shown in Figure 4-2, located in the first section of this chapter, compiling resource files using the Win32 SDK requires three separate tools: the first tool you use is the RC compiler, which turns your .RC file into a .RES file; you then use the CVTRES tool to convert the .RES file into an object file; and finally, you link the object file to the program executable using LINK32.

In one special case, you must currently use the SDK tools: if you have resources in multiple languages that span more than one Windows code page. Suppose, for example, that you expand your English-language application to include Greek, Hungarian, and Russian user interface translations. The standard code page for English is 1252. The standard code page for Greek is 1253, the standard code page for Hungarian is 1250, and the standard code page for Russian is 1251. (See Appendix H.) Because the resource compiler can handle only one code page at a time, you need a separate .RC file for each of these languages. The translators need to see the localized text as it will appear in the final product, so your translators will edit the Greek file on a system that supports Greek characters, the Hungarian file on a system that supports Hungarian characters, and the Russian file on a system that supports Russian characters. (On Microsoft Windows 95, a single system can support all of these characters.) When you receive the translated files, you run the RC compiler on each file separately and link the results using CVTRES, as shown below.

RC /r /c1252 ENGLISH.RC
RC /r /c1253 GREEK.RC
RC /r /c1250 HUNGARIAN.RC
RC /r /c1251 RUSSIAN.RC

copy ENGLISH.RES GREEK.RES HUNGARIAN.RES RUSSIAN.RES TEMP.RES
CVTRES -o TEMP.RES

You can install the code page information required by the /c flag by changing the language setting in Control Panel and supplying the requested floppy, CD-ROM, or network location. You need to install the information only once.

All Win32 resource strings are compiled into Unicode format, but the .RC file format is still expressed in the current default Windows character set*. You cannot edit a resource file containing English, Hungarian, and Russian text in Visual C++ 2 because Microsoft Windows NT 3.x supports only one default Windows code page at a time. Future versions of Visual C++ will probably take advantage of the multilingual features of Windows 95 (described in Chapter 6) or provide some other mechanism for supporting resource files in multiple character sets. The Win32 resource compiler can process files encoded in Unicode, but you would need to create such a file using a Unicode-enabled editor.

Porting existing resources to Win32 is straightforward. The Win32 resource compiler understands Windows 3.1 resource files, which is useful if you are porting your Windows 3.1 application to 32 bits. Also, Win32 console applications conveniently use Win32 resources, and you can share Win32 resources with Macintosh versions of your Windows-based applications by using the Visual C++ 2 Cross-Development Edition for the Macintosh. The Windows Portability Library resource compiler will automatically map strings contained in resource files from Windows character sets to the Macintosh character set, which is considerably different outside the ASCII range.

* RCDATA strings remain in ASCII unless they are L-quoted. Escape sequences are interpreted as Unicode when strings are L-quoted.

本地化资源文件 / Localizing Resource Files

在不同地区我们使用的程序界面是不一样的,如果要使程序迅速改变成适合当地语言的界面,下面的一小段内容可能会帮到你

If you plan to develop localization tools, you should be aware of the Win32 functions BeginUpdateResource, UpdateResource, and EndUpdateResource, which can be used to replace resource data directly in executables. For details about these functions, see the section titled "Adding, Deleting, and Replacing Resources" in Volume 2 of the Microsoft Win32 Programmer's Reference, available from Microsoft Press.

地方化对话框 / Localizing Dialogs

通用的对话框,例如打开,保存,另存为,打印和查找不需要再进行地方化,而在地方化对话框最费时的是调整对话框的大小,控件位置,尤其是频繁使用对话框的应用程序,这时应该考虑尽量减少对RESIZE属性的支持,并限制对话框的最小大小.这里推荐使用AWK script or other parsing tool,可以很容易的获取 x y cx cy 的值,并做直观的修改

Glossary

  • Common dialogs: Standard dialog boxes defined by Windows for operations found in numerous applications, such as Open, Save As, Print, and Find. Applications can call common dialog API functions directly instead of having to supply a custom dialog template and dialog procedure.

The most time-consuming part of localizing dialog boxes is resizing them, particularly for applications that will ship in numerous languages and that contain a large number of dialog boxes. You can minimize the amount of resizing necessary by creating your native-language dialog boxes with as much room to spare as you feel comfortable leaving. Extend text frames as far as possible to allow text to grow when it is translated. For example, in Figure 4-4 above, the frame surrounding the Sample text field in the About dialog box extends to the right until it reaches the OK button.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值