`
zyc1006
  • 浏览: 133877 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

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

    博客分类:
  • 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.

分享到:
评论

相关推荐

    Microsoft Visual C++ Build Tools.zip

    标题中的“Microsoft Visual C++ Build Tools.zip”表明这是一个压缩文件,其中包含了“Microsoft Visual C++ Build Tools.exe”可执行文件。这个可执行文件是Build Tools的安装程序,用于在用户的计算机上安装所需...

    Visual C++ 2010学习版.zip

    这个压缩包包含了Visual C++ 2010的学习资源,包括安装包和教程,帮助用户快速上手C++编程。 首先,我们来详细了解一下Visual C++ 2010。它是Microsoft Visual Studio 2010中的一个重要组成部分,支持C++标准模板库...

    microsoft visual c++ redistributable

    - Microsoft Visual C++ 2005(VC++ 9.0):引入了新的编程语言特性,如Lambda表达式和RAII(Resource Acquisition Is Initialization)改进,同时更新了C++标准库以符合C++03标准。 - Microsoft Visual C++ 2008...

    Visual C++ Visual C++ Visual C++

    7. **C++/CLI**:这是.NET框架的一部分,允许开发者使用C++语法来编写托管代码,与.NET环境无缝交互,创建跨平台的.NET应用程序。 8. **Windows SDK**:Visual C++通常与Windows SDK一起使用,提供了开发Windows...

    Visual+C+++技术内幕(第四版)+源码

    在Visual C++中,最重要的组成部分是MFC(Microsoft Foundation Classes),这是一个C++类库,用于简化Windows应用程序的开发。书中会详细介绍如何使用MFC来构建用户界面,处理消息,管理内存以及实现数据持久化。...

    Visual C++语言参考手册.zip

    6. **资源管理**:Visual C++允许直接在项目中管理资源,如对话框、图标、菜单等,通过RC文件和Resource Editor进行操作。 7. **多线程编程**:Visual C++支持多线程编程,提供了CWinThread类来创建和管理线程。...

    Visual C++ v56.exe

    Microsoft Visual C++ 2005 Redistributable无法删除,执行修复或重新安装时出现“The Feature You Are Trying to Use Is on a Network Resource That Is Unavailable”(您尝试使用的功能位于不可用的网络资源中)...

    MS Visual Studio C++ 开发资源版本信息设置

    这些信息通常存储在一个`.rc`(Resource Compiler)文件中,该文件是Visual Studio C++项目中的资源脚本,它定义了应用程序的各种资源,如对话框、图标、菜单和版本信息。 在"MS Visual Studio C++ 开发资源版本...

    Visual C++ 数据库系统开发完全手册.pdf

    Visual C++ 6.0作为一款功能全面的开发工具,为数据库系统开发提供了良好的支持,无论是传统的C语言开发还是基于MFC的C++开发,都能够在Visual C++ 6.0中找到适合的环境和工具。通过了解和熟练运用Visual C++ 6.0的...

    《Visual C++ 6.0实例教程》

    3. **用户界面设计**:使用Resource Editor创建和编辑资源,如对话框、菜单、图标和字符串表。学习如何通过消息映射机制处理用户事件。 4. **类库和对象**:学习如何定义和使用自定义类,以及如何利用MFC类库中的类...

    机械工业_Visual C++_Win32创建菜单资源实例.rar

    在Visual C++中,菜单资源通常通过资源编辑器进行创建。开发者可以在资源编辑器中添加、删除和修改菜单项,设置其文本、快捷键、图标以及关联的函数调用。 在创建菜单资源时,我们需要了解几个关键概念: 1. **...

    Visual C++课程设计案例精编(第二版) --第三章 系统进程管理器的开发

    // Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif // !defined(AFX_SHOWALLPROCESS_H__928B7E87_BDF8_4C4F_84D3_AF4D921C3CF8__INCLUDED_)

    Visual C++/DirectX9 3D游戏开发导引源码

    《Visual C++/DirectX9 3D游戏开发导引》是针对使用Microsoft的Visual C++编程环境和DirectX9图形库进行3D游戏开发的一本教程性资源。该资源可能包含一系列章节、实例代码、项目文件以及相关资料,旨在帮助开发者...

    解决无法删除microsoft visual c++运行库问题

    Microsoft Visual C++ 2005 Redistributable无法删除,执行修复或重新安装时出现“The Feature You Are Trying to Use Is on a Network Resource That Is Unavailable”(您尝试使用的功能位于不可用的网络资源中)...

    Visual C++制作自己的资源管理器.zip

    在本项目中,"Visual C++制作自己的资源管理器.zip" 涉及到的是使用Microsoft的Visual C++开发环境创建一个自定义的资源管理器应用程序。资源管理器是操作系统中用于浏览、组织和管理文件和文件夹的主要工具。通过这...

    Visual C++面向对象编程

    6. **MFC(Microsoft Foundation Classes)框架**:Visual C++中的MFC是一个库,它简化了Windows API的使用,提供了一套面向对象的类来构建Windows应用程序。通过MFC,开发者可以快速开发用户界面,处理消息循环和...

    深入编程内幕 ——VISUAL C++

    2. **资源视图(Resource View)**:查看和修改应用程序的资源,如对话框、菜单、图标等。 3. **输出窗口(Output Window)**:显示编译、链接和调试过程中的信息。 4. **即时窗口(Watch Window)**:监视变量的...

    VC 运行库 合集 Visual C++ Redistributab 2005 2208 2010 2013

    VC运行库合集是Microsoft为开发者和用户提供的关键组件,主要包含了Visual C++ Redistributable Packages的多个版本,包括2005、2008、2010和2013。这些运行库是运行由Visual C++编译器生成的许多应用程序所必需的,...

    《Visual C++6 宝典》随书光盘

    - Visual C++6.0内置的资源编辑器允许开发者直接在IDE中设计和编辑窗口、菜单、对话框、图标等资源。 - 这种可视化设计使得UI开发更加直观,减少手动编写资源脚本的工作。 8. **异常处理**: - C++的异常处理...

Global site tag (gtag.js) - Google Analytics