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

http://blog.csdn.net/Joephia/archive/2008/06/06/2517159.aspx




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

 

Table title
 FileID 0803 
 Create 08-06-06 PM
 Author Joephia 
 LastUpdate 08-06-06PM 

 

摘要:

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

正文:

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

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

  1. HINSTANCE ghInstApp = AfxGetInstanceHandle();  
  2. HRSRC hRsrc = FindResource(ghInstApp, MAKEINTRESOURCE(IDR_DATA1),RT_RCDATA);  
  3. ASSERT(hRsrc);  
  4. DWORD len = SizeofResource(ghInstApp,hRsrc);   
  5. ASSERT(len);  
  6. HGLOBAL hGlobal = LoadResource(ghInstApp,hRsrc);   
  7.   
  8. CFile file("abc.txt", CFile::modeCreate | CFile::modeWrite);  
  9. file.Write(hGlobal,len);  
  10. file.Close();  


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

  1. XXXXXX resource  
  2.     Accelerator  
  3.     "BIN"  
  4.         IDB_BIN1  
  5.     Bitmap  
  6.         IDB_BITMAP1  
  7.     Cursor  
  8.         IDC_CURSOR1  
  9.     Data  
  10.         IDR_DATA1  
  11.     Dialog  
  12.         IDD_DIALOG1  
  13.     Html  
  14.         IDR_HTML1  
  15.     Icon  
  16.         IDR_ICON1  
  17.     Menu  
  18.         IDR_MENU1  
  19.     String Table  
  20.         String Table  
  21.     ToolBar  
  22.         IDR_TOOLBAR1  
  23.     Version  
  24.         VS_VERSION_INFO  

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

 

  1. #define RT_CURSOR           MAKEINTRESOURCE(1)          //Cursor  
  2. #define RT_BITMAP           MAKEINTRESOURCE(2)          //Bitmap  
  3. #define RT_ICON             MAKEINTRESOURCE(3)          //Icon  
  4. #define RT_MENU             MAKEINTRESOURCE(4)          //Menu  
  5. #define RT_DIALOG           MAKEINTRESOURCE(5)          //Dialog  
  6. #define RT_STRING           MAKEINTRESOURCE(6)          //String Table  
  7. #define RT_FONTDIR          MAKEINTRESOURCE(7)          //未知  
  8. #define RT_FONT             MAKEINTRESOURCE(8)          //Font  
  9. #define RT_ACCELERATOR      MAKEINTRESOURCE(9)          //Accelerator  
  10. #define RT_RCDATA           MAKEINTRESOURCE(10)         //Data  
  11. #define RT_MESSAGETABLE     MAKEINTRESOURCE(11)         //未知  
  12.  
  13. #define DIFFERENCE          11  
  14. #define RT_GROUP_CURSOR     MAKEINTRESOURCE((DWORD)RT_CURSOR + DIFFERENCE)          //未知  
  15. #define RT_GROUP_ICON       MAKEINTRESOURCE((DWORD)RT_ICON + DIFFERENCE)            //未知  
  16. #define RT_VERSION          MAKEINTRESOURCE(16)         //Version  
  17. #define RT_DLGINCLUDE       MAKEINTRESOURCE(17)         //未知  
  18. #if(WINVER >= 0x0400)  
  19. #define RT_PLUGPLAY         MAKEINTRESOURCE(19)         //PLUGPLAY  
  20. #define RT_VXD              MAKEINTRESOURCE(20)         //VXD  
  21. #define RT_ANICURSOR        MAKEINTRESOURCE(21)         //ANICURSOR  
  22. #define RT_ANIICON          MAKEINTRESOURCE(22)         //ANIICON  
  23. #endif /* WINVER >= 0x0400 */  
  24. #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 Type Element File Format Comment/ Description
RT_CURSOR Cursor .CUR #include in .RC file
RT_BITMAP Bitmap or toolbar .BMP #include in .RC file
RT_ICON Icon .ICO #include in .RC file
RT_MENU Menu or pop up menu .RC #include in .RC file
RT_DIALOG Dialog .DLG or .RC #include .DLG file in .RC file
       
RT_STRING String .RC  
RT_FONTDIR Font .FNT  
RT_FONT Font .FNT  
RT_ACCELERATORS Accelerator .RC  
RT_RCDATA User-defined resource .RC Can use for constants or application specific structures
       
RT_MESSAGETABLE Messages .MC #include compiled message table in .RC file
       
RT_GROUP_CURSOR Cursor N/A Generated internally by resource compiler to provide Windows with information about cursor's resolution and type
       
RT_GROUP_ICON Icon N/A Generated internally by resource compiler to provide Windows with information about icon's resolution and type
       
RT_VERSION Version information .RC  
       
RT_DLGINCLUDE Header file that contains menu and dialog box #define statements .RC Used 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.

你可能感兴趣的:(C++,c,windows,C#,Microsoft)