Visual C ++. Net Chapter 3 ends

xiaoxiao2021-03-06  85

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.

Give Win32 programmer

There is no doubt that the standard Windows Winmain and window processes have been hidden in the application framework. You will be behind this book, but we will see these functions when you analyze the MFC frameworks and classes. In this whole, you may be very doubtful WM_PAINT messages will happen. You want your window to draw actions to respond to Windows messages, you also expect to get the handle of the device context in the PAINTSTRUCT structure of the Windows BEGIN function.

It will complete these hard work in the application framework and serve the device context in the onDraw virtual function (from the object indicator). In the description of the second chapter, the true virtual function is in the window class in the MFC library. It is a rare. MFC library message mapping function is distributed from the handle of the application frame. Most of the Windows message. The programmer of Mfc1.0 has defined the OnPAint message mapping function for the source window class. From MFC2.5, still, OnPaint is mapped to the CView class, and the function is called by the onDraw polymorphism. Why? Because ONDRAW needs to support the printer to fully support the display. Both Opaint and Onprint call overdraw, thus make a write code to support the printer at the same time And displays .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 Repesents 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 rotow 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 state

#include "afxres.rc"

Which Bring in Some MFC Library Resources Common To All Applications. Thase

Resources include String, Graphical Buttons, And Elements Needed for Printing and for OLE.

Preview Explorer

Since you have completed the app, it is now when you quickly understand the resource editor. Although the application's resource script EX03A.RC is an ASCII file, use the text editor to modify it is not a good note, that is the resource editor work.

EX03A.RC content

The resource file determines that many applications "Outer Officer and Sensory". EX03A.RC contains the list of Windows in Table 3-2 in Table 3-2.

Table 3-2 Resources of Windows included in the MFC application

Resource note

Accelerator contains the definition of the key to the simulation menu and toolbar selection.

Dialog (dialog box) includes a style of dialog that is very content. There is only the About dialog in EX03A.

Icon (icon) draws icons (16x16 pixels and 32x32 pixels). For example, you see the icon of the Windows Browser and Application About dialog. EX03A used MFC's logo

Application icon.

Manifest (Type List) contains applications in runtime type information

MENU (menu) depicts the top-level menu of the application and the connection shortcut menu

String Table contains the partial payment but does not include a part of the C source code.

Toolbar describes a row button directly under the menu.

Version (version information) includes program description, version number, language, etc.

Except for some resources above. EX03A.RC also contains these statements.

#include "afxres.h" and more declarations included in this file #include "afxres.rc"

Some of the resources of a few MFC libraries are all public applications. These include strings, graphics buttons, OLE, and printed elements.

Note if you're Using The Shared DLL Version of The MFC Library, The Common Resources Arend 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 will not 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 change number 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.

.

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.

Note: If you use a DLL version in the MFC library, the public resource is stored in the MFC DLL.

The EX03A.RC file also includes this statement:

#include "resource.h"

This statement that some #define generated constant, including IDR_MAINFRAME (this is an identifier menu, icon, string, and acceleration table), IDR_EX03ATYPE (this is identifying the default document icon, we will use this program) and IDD_ABOUTBOX (this is identifying the About conversation Box). These resource.h files are indirectly included in the source code file for the application. If you use the Resource Editor to add a number of constants (symbols), these identification will eventually display in the resource.h file. If you are going Carefulness to edit this file in text mode, because your change may be independent of the next time you use the resource editor.

Running Resource Editor dialog

The Resource Editor dialog allows you to establish and edit the resource dialog. Refer to the following steps to run the editor:

1. Open the project's resource file. Select a resource view from the View menu. If you expand these items, you will see the following resource view window:

2. Check the application's resources. Now you have some resource. When you select resources and double-click it, another window opens your choice. If you open the dialog, the control panel will appear. If you don't Do it, click the toolbar button on the left of VS.NET.

3. Change the IDD_ABOUTBOX dialog and make a little change to the EX03A for a little change.

You can change the size, move the OK button, change your text, etc.

4. Re-compiling the project's resource file. In VC . Net, select the generation of generated menu. Note that meaningless C recompile is necessary .vc . Net saves the edited resource file, then the resource compiler handles EX03a. RC generates compilation versions, EX03A.Res, which is feedback to the connection. The connection runs soon because it can be incremented to the project.

5. Test the new version of the application. Run the Ex03a program, from the application's help menu to confirm the changes to the Chinese box, such as what you expect.

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 SettingSoption 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 ...................

You can create your owned to, by Choosing Configuration Manager from Visual C . Net 'S Build Menu.

Win32 debugging method with Win32 release method

When you want to generate your app, you can choose one of both: debug and release. These two clocks default ways are generated in the MFC application wizard. The default setting of the project is summarized in Table 3-4

Table 3-4MFC Application Wizard default project settings

Options release generation debug generation

Source code-level debugging closes compilation and links available

MFC Diagnostic Macro (NDEBUG Definition) available (_NDebug Definition)

Ling joint MFC distribution library MFC debug library

Compilation optimization speed optimization (not available in learning version) is not optimized (fast compilation)

Develop your app in debug mode, then reproduce compilation before you want to release. Publishing the EXE file will be the smallest and fastest, you should fix all the errors. The configuration you choose is to come on the window. The toolbar is generated, as shown in Chapter 1, 1-2. Press the default value, debug output files and intermediate files are stored in the project's debug subdirectory released in the release subdirectory. You can visit The Project Properties Page dialog When you change these directories in the General Properties page of the Configuration Properties folder. If you need you can select the Configuration Manager through the VC . Net Generation Menu Select Configuration Manager to create a fully belonging to your own profile.

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 Repesent 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"

Understanding pre-compiled headers

When the MFC Application Wizard generates an item, it produces a switch settings and precompiled heads. You must understand the precompiled heads of the system processed and manage them in your project.

Note VC . NET has two pre-compiled head "systems": automatic and manual. Automatic precompiled head, starting through / YX compilation switch, stored in the "database" file of compilation and output. Manual precompiled head via / YC and The / yu switch starts is also important to all of the projects generated in the MFC application wizard.

The pre-translated head file describes the compiler "snapshot" of special source code. In the MFC program, this snapshot is usually directly after the following statement:

#include "stdafx.h"

The STDAFX.H file contains the #include declaration of the header file of the MFC library is. The content of this file is determined by the options you selected in the Application Wizard. But it always contains the following statements:

#include

#include

If you use a mixed document, stdafx.h also contains this statement:

#include

At the same time, if you use the Automation and ActiveX controls, it contains:

#include

If you use IE4.0's general control, stdafx.h contains this statement:

#include

Occasionally, you will need other headers - for example, this header file is an access based on the template collection class, this statement is as follows: #include

Source code file stdafx.cpp only contains this statement:

#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 Illustrate 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 does not 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.

The pre-compiled head file generated by this statement is in the project directory. MFC header files include never changed in stdafx.h, but the compiler takes a long time. Switch / YC is only used for STDAFX.cpp files for creating pre- Compiling Head File (PCH). Switch / YU, for all other source code files, only existing PCH files. Switches / FP needs to specify the file name of the PCH, give other default values ​​of the target output file subdirectory The project name (extension with the PCH). Figure 3-1 illustrates all procedures.

The MFC application wizard settings / YC and / yU switches are given to you, you can change it according to your needs. It may define compilation switch settings for individual source files. If you are in the C / C folder in the Properties dialog box Only the stdafx.cpp file is selected, you will see / YC settings in the Pre-Describe Person Properties page. This important / yU setting defines the target.

To know that the PCH file is very large, 10M is very typical. If you are not careful, you will fill your hard drive. You have to clean your debug directory to make things control. Or you with / fp Compile option to redefine public directory of the PCH file.

Two ways to run the program

VC . Net allows you to run programs directly (via Ctrl F5) or run through debugger (via F5). Direct running program is very fast because of VC . Net does not need to load debugger. If you don't need to see diagnostic information or Use breakpoints, you start your program by pressing Ctrl F5.

转载请注明原文地址:https://www.9cbs.com/read-107901.html

New Post(0)