Visual C++.NET第三章结束

类别:编程语言 点击:0 评论:0 推荐:
For Win32 Programmers Rest assured that the standard Windows WinMain and window procedure functions are hidden away inside the application framework. You’ll see those functions later in this book, when we examine the MFC library frame and application classes. In the meantime, you’re probably wondering what happened to the WM_PAINT message. You’d expect to do your window drawing in response to this Windows message, and you’d expect to get your device context handle from a PAINTSTRUCT structure returned by the Windows BeginPaint function. It so happens that the application framework has done all the dirty work for you and served up a device context(in object pointer form) in the virtual function OnDraw. As explained in Chapter 2, true virtual functions in window classes are an MFC library rarity. MFC library message map functions dispatched by the application framework handle most Windows messages. MFC 1.0 programmers always defined an OnPaint message map function for their derived window classes. Beginning with version 2.5, however, OnPaint was mapped in the Cview class, and that function made a polymorphic call to OnDraw. Why? Because OnDraw needs to support the printer as well as the display. Both OnPaint and OnPrint call OnDraw, thus enabling the same drawing code to accommodate both the printer and the display. 给WIN32的程序员 毫无疑问标准的WINDOWS WINMAIN和窗口过程已经隐藏在应用框架中了.你将在本书的后面,但我们剖析MFC框架和类时候你将看到这些函数.在此其间,你或许非常疑惑为什么会发生 WM_PAINT消息.你希望你的窗口绘制动作能响应WINDOWS的消息,你还期望在WINDOWS BEGIN函数的PAINTSTRUCT结构中得到设备上下文的句柄. 它将在应用框架中完成这些辛苦的工作并在ONDRAW虚函数中的设备上下文服务(在来自于对象指示器).在第二章的说明中,真正的虚函数在MFC库中的窗口类中是稀有的.MFC库消息映射函数分发的是来自于应用框架的句柄大部分是WINDOWS消息.MFC1.0的程序员已经为源窗口类定义了ONPAINT消息映射函数.从MFC2.5开始,仍然,ONPAINT映射到CVIEW类中,并且函数是被ONDRAW多态调用的.为什么呢?因为 ONDRAW需要支持打印机同时也要充分支持显示器.两个OPAINT和ONPRINT都调用ONDRAW,因而使一写代码能同时支持打印机和显示器. A Preview of the Resource Editors Now that you have a complete application program, it’s a good time for a quick look at the resource editors. Although the application’s resource script, Ex03a.rc, is an ASCII file, modifying it with a text editor is not a good idea. That’s the resource editors’ job. The Contents of Ex03a.rc The resource file determines much of the Ex03a application’s “look and feel.” The file Ex03a.rc contains (or points to) the Windows resources listed in Table 3-2. Table 3-2 Windows Resources Contained in MFC Applications Resource Description Accelerator Includes definitions for keys that simulate menu and toolbar selections. Dialog Includes layout and contents of dialog boxes. Ex03a has only the About dialog box. Icon Represents icons (16-by-16-pixel and 32-yb32-pixel versions), such as the application icon you see in Windows Explorer and in the application’s About dialog box. Ex03a uses the MFC logo for its application icon. Manifest Contains the run-time type information for the application. Menu Represents the application’s top-level menu and associated shortcut menus. String table Includes strings that are not part of the C++ source code Toolbar Represents the row of buttons immediately below the menu. Version Includes program description, version number, language, and so on. In addition to the resources listed above, ex03a.rc contains these statements #include “afxres.h” and further in the file contains this statement #include “afxres.rc” which bring in some MFC library resources common to all applications. These resources include string, graphical buttons, and elements needed for printing and for OLE. 预览资源管理器 既然你完成了应用程序,现在是你快速了解资源编辑器的时候了.虽然应用程序的资源脚本EX03A.RC是个ASCII文件,用文本编辑器去修改它不是个好的注意,那是资源编辑器的工作. Ex03a.rc的内容 资源文件决定了许多应用程序”外官和感官”.Ex03a.rc包含了(或指向)表3-2中的WINDOWS的资源列表. 表3-2 MFC应用程序所包含的WINDOWS的资源 资源 说明 Accelerator (加速器) 包含模拟菜单和工具栏选择的键的定义. Dialog(对话框) 包括对话框的样式很内容.EX03A中只有ABOUT对话框. Icon(图标) 描绘图标(16x16象素和32x32象素).例如你看到的WINDOWS浏览 器和应用程序ABOUT对话框的图标.EX03A用的是MFC的logo来做 应用程序的图标. Manifest(类型清单) 包含应用程序在运行时类型信息 Menu(菜单) 描绘为应用程序的顶级菜单和关连的快捷菜单 String table(字符串表) 包含字付串但不包括C++源代码的部分. Toolbar(工具栏) 描述为直接在菜单下的一行按钮. Version(版本信息) 包括程序说明,版本号,语言等等 除上面的一些资源以外.ex03a.rc还包含这些声明. #include “afxres.h”和更多的声明包含在这个文件里 #include “afxres.rc” 还有些少数MFC库的资源是全应用程序公共的.这些包括字符串,图形按钮,OLE和打印时需要的元素. Note If you’re using the shared DLL version of the MFC library, the common resources are stored inside the MFC DLL. The Ex03a.rc file also contains this statement: #include “resource.h” This statement brings in some #define constants, including IDR_MAINFRAME(which identifies the menu, icon, string list, and accelerator table), IDR_EX03ATYPE(which identifies the default document icon, which we won’t use in this program), and IDD_ABOUTBOX(which identifies the About dialog box). This same resource.h file is included indirectly by the application’s source code files. If you use a resource editor to add more constants(symbols), the definitions will ultimately show up in resource.h . Be careful if you edit this file in text mode because your changes might be removed the next time you use a resource editor. Running the Dialog Resource Editor The Dialog resource editor allows you to create or edit dialog box resources. To run the editor, follow these steps: 1. Open the project’s RC file. Choose Resource View from the View menu. If you expand each item, you’ll see the following in the Resource View window: 2 Examine the application’s resources. Now take some time to explore the individual resources. When you select a resource by double-clicking on it, another window opens with tools appropriate for the selected resource. If you open a dialog resource, the control palette should appear. If doesn’t , click the Toolbox button on the left side of Visual Studio .NET. 3 Modify the IDD_ABOUTBOX dialog box. Make some changes to the About Ex03a dialog box. You can change the size of the widow by dragging the right and bottom borders, move the OK button, change the text, and so forth. Simply click on an element to select it, and then right-chick to change its properties. 4 Rebuild the project with the modified resource file. In Visual C++.NET, choose Build form the Build menu. Notice that no actual C++ recompilation is necessary. Visual C++ .NET saves the edited resource file, and then the Resource Compiler (rc.exe) processes Ex03a.rc to produce a compile version, Ex03a.res, which is fed to the linker. The linker runs quickly because it can link the project incrementally. 5 Test the new version of the application. Run the Ex03a program again, and then choose About form the application’s Help menu to confirm that your dialog box was changed as expected. 注意: 如果你使用的是MFC库中共享的DLL版本,那么公共的资源存储在MFC DLL中. EX03A.RC文件也包括这个声明: #include “resource.h” 这个声明一些#define产生的常数,包括 IDR_MAINFRAME(这个是识别菜单,图标,字符串和加速表),IDR_EX03ATYPE(这个是识别缺省文档图标,我们将使用这个程序)和IDD_ABOUTBOX(这个是识别About对话框的).这些resource.h文件都间接的包含应用程序的源代码文件中.如果你使用资源编辑器增加许多常数(符号),这些识别将最终显示在resource.h文件中.如果你要在文本模式下编辑此文件要小心,因为,你的改变可能离下次你用资源编辑器无关. 运行资源编辑器对话框 资源编辑器对话框允许你建立和编辑资源对话框.参照以下步骤运行编辑器: 1. 打开项目的资源文件.从视图菜单中选择资源视图.如果你展开这些项,你将看到以下资源视图窗口: 2. 检查应用程序的资源.现在抽些时间探索个别资源.当你选择资源并双击它时,另一个窗口打开你选择的资源.如果你打开对话框资源,控件面板将出现.如果你不这么做,单击VS.NET左边的工具栏按钮. 3. 更改IDD_ABOUTBOX对话框,给EX03A关于对话框做少许改变. 你能通过拖动窗口右边和底边来改变大小,移动OK按钮,改变文本等等.单击某个元素可以选中,右击可以调出属性对话框. 4. 重新编译项目的资源文件.在VC++.NET中,选择生成菜单的生成.注意,无意义的C++重编译是必要的.VC++.NET保存了编辑的资源文件,然后资源编译器处理EX03A.RC产生编译版本,EX03A.RES,那是反馈到连接.连接运行的很快因为它能增量连接到项目. 5. 测试新版本的应用程序.在次运行EX03A的程序,从应用程序的帮助菜单选择关于以确认对华框的修改如你期望的那样. Win32 Debug Target vs. Win32 Release Target When you build your application, you can choose one of two targets: debug and release. These are the two default targets generated by the MFC Application Wizard. The default project settings are summarized in Table3-4. Table 3-4 MFC Application Wizard Default Project Settings Option Release Build Debug Build Source Code Debugging Disabled Enabled for both compiler and linker MFC Diagnostic Macros Disabled(NDEBUG defined) Enabled(_DEBUG defined) Library Linkage MFC Release library MFC Debug libraries Compiler Optimization Speed optimization(not No optimization Available in Learning Edition (faster compile) You develop your application in Debug mode, and then you rebuild in Release mode before delivery. The Release build EXE will be smaller and faster, assuming you’ve fixed all the bugs. You select the configuration from the build target window in the toolbar, as shown in Figure 1-2 in Chapter 1. By default, the Debug output files and intermediate files are stored in the project’s Debug subdirectory and the Release files are stored in the Release subdirectory. You can change these directories on the General property page in the Configuration Properties folder, which you can access in the project’s Property Pages dialog box. You can create your own custom configurations if you need to, by choosing Configuration Manager from Visual C++.NET ’s Build menu. Win32调试方式与Win32发布方式 当你要生成你的应用程序,你可以在俩种方式中选一种:调试和发布.这两钟缺省的方式是在MFC应用向导产生的.项目的缺省设置总结在表3-4 表3-4MFC应用程序向导缺省的项目设置 选项 发布生成 调试生成 源代码级调试 关闭 编译和连结都可用 MFC诊断宏 关闭(NDEBUG定义) 可用(_NDEBUG定义) 库联接 MFC发布库 MFC调试库 编译优化 速度优化(在学习版中不可用) 没有优化(快速编译) 在调试模式下开发你的应用程序,而后在你要发布之前重现编译.发布生成的EXE文件将是最小和最快的,你应该修正了所有的错误.你选择的配置是来之于窗口工具栏的生成方式,如图第一章第1-2图.按缺省值,调试输出的文件和中间文件都存放在项目的调试子目录下发布文件存放在发布子目录下.你可以访问项目属性页对话框时通过在配置属性文件夹的通用属性页中改变这些目录. 如果你需要你可以通过VC++.NET的生成菜单选择配置管理器来建立完全属于你自己的配置文件. Understanding Precompiled Headers When the MFC Application Wizard generates a project, it generates switch settings and files for precompiled headers. You must understand how the make system processes precompiled headers in order to manage your projects effectively. Note Visual C++.NET has two precompiled header “systems”: automatic and manual. Automatic precompiled headers, which are activated by the /Yx compiler switch, store compiler output in a “database” file. Manual precompiled headers are activated by the /Yc and /Yu switch settings and are central to all the MFC Application Wizard-generated projects. Precompiled headers represent compiler “snapshots” taken at a particular line of source code. In MFC library programs, the snapshot is generally taken immediately after the following statement: #include “StdAfx.h” The file StdAfx.h contains #include statements for the MFC library header files. The file’s contents depend on the options you select when you run the MFC Application Wizard, but the file always contain these statements: #include #include If you’re using compound documents, StdAfx.h also contains this statement: #include And if you’re using Automation or ActiveX controls, it contains: #include If you’re using Internet Explorer 4.0 Common Controls, StdAfx.h contains this statement: #include Occasionally, you’ll need other header files—for example, the header for template-based collection classes that is accessed by this statement: #include The source file StdAfx.cpp contains only this statement: #include “StdAfx.h” 理解预编译报头 当MFC应用程序向导产生项目时,它产生了开关设置和预编译头.你必须了解系统处理的预编译头并在你的项目中有效的管理它们. 注意 VC++.NET有两中预编译头”系统”:自动和手动的.自动预编译头,通过/Yx编译开关起动,存储在编译输出的”数据库”文件中.手动预编译头通过/Yc和/Yu开关起动的也是重要的全部是在MFC应用程序向导产生的项目. 预编译头文件描述的是特殊源代码的编译器”快照”.在MFC程序中,这个快照通常直接在以下声明之后: #include “StdAfx.h” StdAfx.h文件包含了MFC库的头文件的#include声明是.这个文件的内容是由你在应用程序向导中选择的选项决定的.但是它始终包含以下这些声明: #include #include 如果你使用了混合文档,StdAfx.h也包含这个声明: #include 同时如果你使用了自动化和ActiveX控件,它包含: #include 如果你使用了IE4.0的通用控件,StdAfx.h包含这个声明: #include 偶尔,你将需要其它的头文件—例如,这个头文件是访问基于模板集合类的,这个声明如下: #include 源代码文件StdAfx.cpp仅包含此声明: #include “StdAfx.h” This statement is used to generate the precompiled header file in the project directory. The MFC library headers included by StdAfx.h never change, but they do take a long time to compile. The compiler switch /Yc, used only with StdAfx.cpp, causes the creation of the precompiled header(PCH) file. The switch /Yu, used with all the other source code files, causes the use of an existing PCH file. The switch /Fp specifies the PCH filename that would otherwise default to the project name (with the PCH extension) in the target’s output files subdirectory. Figure 3-1 illustrates the whole process. The MFC Application Wizard sets the /Yc and /Yu switches for you, but you can make changes if you need to. It’s possible to define compiler switch settings for individual source files. If you select only StdAfx.cpp in the C/C++ folder in the project’s Property Pages dialog box, you’ll see the /Yc setting on the Precompiled Headers property page. This overrides the /Yu setting that is defined for the target. Be aware that PCH files are big—10MB is typical. If you’re not careful, you’ll fill up your hard disk. You can keep things under control by periodically cleaning out your project’s Debug directories, or you can use the /Fp compiler option to reroute PCH files to a common directory. Two Ways to Run a Program Visual C++.NET lets you run your program directly (by pressing CTRL+F5) or through the debugger (by pressing F5). Running your program directly is much faster because Visual C++.NET doesn’t have to load the debugger first. If you know you don’t want to see diagnostic messages or use breakpoints, start your program by pressing CTRL+F5. 这个声明产生的预编译头文件在项目目录里.MFC头文件包括在StdAfx.h里从未更改,但是编译器要花很长时间.开关/Yc仅用于StdAfx.cpp文件,用于创造预编译头文件(PCH).开关/Yu,用于其他所有的源代码文件,只使用现有的PCH文件.开关/Fp需要指定PCH的文件名,将目标输出文件子目录的其它缺省值给项目名(用PCH的扩展).图3-1图解了全部过程. MFC应用程序向导设置/Yc和/Yu开关给你,你可以根据你的需要改变它.它可能给个别的源文件定义编译开关设置.如果你在项目的属性对话框中的C/C++文件夹中仅选择了StdAfx.cpp文件,你将在预编译头属性页中看到/Yc设置.这个重要的/Yu设置定义了目标. 要知道PCH文件是很大的,10M当时很典型的.如果你不小心,你将填满你的硬盘.你要定期的清理你的调试目录才可以使事情得已控制.或者你用/Fp编译选项重新定义PCH文件的公共目录. 两个方法来运行程序 VC++.NET能让你直接运行程序(通过CTRL+F5)或者通过调试器运行(通过F5).直接运行程序是很快的因为VC++.NET不需要加载调试器.如果你不需要看诊断信息或者使用断点,你通过按CTRL+F5来开始你的程序.

本文地址:http://com.8s8s.com/it/it26744.htm