VC ++. NET learning notes (Chapter 3)

xiaoxiao2021-03-06  67

3 getting started with the MFC Application Wizard

Chapter 2 introduced the MFC library's document-view architecture. This hands-on chapter will show you how to build a functioning MFC library application while insulating you from the complexities of the class hierarchy and object interrelationships. You'll work with only one document- view program element:. the view class that is closely associated with a window For the time being, you can ignore elements such as the application class, the frame window, and the document Of course, your application will not be able to save. ITS Data To Disk and Won't Support Multiple Views, But Part III of this Book Will Give You Plenty Opportunity To Explore Using Those Features.

Starting from the MFC Application Wizard

The second chapter introduces the MFC's document - view architecture. This chapter will show you and have to teach you how to build an app with the MFC function. Of course, the complex class hierarchical relationship is not seen. You will build a only There is a document - the program: This view class is the closed window. Temporary, you can ignore things such as application classes, frame windows, and documents. Of course, your application must be able to save data and support more Views, to the third part of this book will explore and use these features.

Because resources are so important in Microsoft Windows-based applications, you'll use Resource View in this chapter to visually explore the resources of your new program. The chapter will also give you some hints on setting up your Windows environment for maximum build speed and Optimal debugging output.

Because resources are very important in Windows-based applications, you will use resource manager in this chapter to truly experience resources in your program. This chapter will also provide some settings to maximize your Windows environment. The fastest, the debugging environment is best.

Note To compile and run the examples presented in this chapter and in the following chapters, you must have Microsoft Windows NT4.0, Windows 2000, or Windows XP installed, plus all the Microsoft Visual C . NET components. Be sure that Visual C . NET 's executable, include, and library directories are set correctly. (you can change the directories by choosing Options from the Tools menu and clicking the Projects folder.) If you have any problems with the steps presented, please refer to your Visual C .NET Documentation and readme files for Troubleshooting Instructions. Note When you compile and execute this chapter and the following sections, you must install Microsoft Visual C . Net components in Microsoft Windows NT4.0 Windows2000, or Windows XP. Note that you The execution of Visual C . Net must be set correctly, the directory where the library file is located. (You can change and select other directories through the Tools menu and click Project Folder). If you encounter any problems, please find Visual C . NET documents and readme files are for help.

What's a view?

From a user's standpoint, a view is an ordinary window that the user can size, move, and close in the same way as any other Windows-based application windows. From the programmer's perspective, a view is a C object of a class derived from the MFC library Cview classes. Like any C object, the view object's behavior is determined by the member functions (add data members) of the class-both the application-specific functions in the derived class and the standard functions inherited from the base classes.

What is a view?

With the user's position, the view is the normal window to adjust the size, move, and close. From the programmer's perspective, the view is just a C object from the MFC class CVIEW class. Any C object is similar, and the view object is a member function such as an application-specific function composed of the source class and inheriting the standard functions of the base class.

With Visual C . NET, you can produce interesting applications for Windows by simply adding code to the derived view class that the MFC Application Wizard code generator produces. When your program runs, the MFC library application framework constructs an object of the derived view class and displays a window that is tightly linked to the C view object. As is customary in C programming, the view class code is divided into two source modules the header file (H) and the implementation file (CPP). using Visual C . NET, You only need to use the MFC Application Guide Code Generator to generate a framework, add some simple code to the source view class, you can generate a very interesting application. When your program is running, the MFC application framework will build a source view class. Composed objects and displays a window that is closely linked to the C view object. Like the C language, the view class code has two partial components, one is the header file (H), one is the execution file (CPP).

MFC Library Application Types

The MFC library supports three application types: Single Document Interface (SDI), Multiple Document Interface (MDI), and Multiple Top-Level Windows Interface (MTI) An SDI application has, from the user's point of view, only one window If.. the application depends on disk-file "document", only one document can be loaded at a time. The original Windows Notepad is an example of and SDI application. An MDI application has multiple child windows, each of which corresponds to an individual document. Earlier versions of Microsoft Office applications (before Office 2000) such as Microsoft Word are examples of MDI applications. An MTI application is a single instance of an application running several of the top-level windows. Modern versions of the Office applications use this model.

MFC application type

MFC supports three application types: single document interface, multi-document interface, multi-top window interface. A SDI application, from the user's point of view, only one window. If the application relies on disk file "document", each time Load a document. Early WINDOWS notepad is an example of SDI. A MDI has multiple sub-windows, each is a separate document. Word in the early Office2000 is the example of this MDI. One MTI's application is a single instance of a single top window by an application. The current version of Office application uses this module .When You Run The MFC Application Wizard to create a New Project, MDI is the default application type. for the early examples in this book, you'll be generating SDI applications because fewer classes and features are involved. Be sure to select the Single Document option (on the Application Type page of the MFC Application Wizard) for these examples. Starting with Chapter 18, You'll Be Generating MDI Applications. The MFC Library Application Framework Architecture Ensures That Most SDI Examples Can Be Upgraded Easily To MDI Applications.

When you use the MFC Application Wizard to establish a new project, MDI is the default type. In the previous example, you see the SDI application, because only a few classroom features. In these examples you can choose a single document option (Application Type Page at the MFC Application Wizard). From Chapter 18, you will see the MDI application. MFC Application Framework Make sure you can see more SDI examples, It also helps you more easily consolidate the MDI app.

EX03A: The "do-nothing" Application

. The MFC Application Wizard generates the code for a functioning MFC library application This working application simply brings up an empty window with a menu attached Later, you add code that draws inside the window You take the following steps to build an application..:

Run the MFC Application Wizard to Generate SDI Application

Source Code. Choose New Project from Visual C 's File Menu.

Select Visual C Projects, And The Select MFC Application from The List of Templates, as shown here.

Type C: / vcppnet / in the Location box Type Ex03a in the Name box, and then click OK Use the links on the left side of the dialog box to move through the various Application Wizard pages to set up project options...

Example 03A: A "nothing" application

The MFC application wizard can generate code. It only produces a simple empty window with a menu for the application. You have to add code for the window later. You can follow the following steps: 1. Run the MFC application Wizard Generate Source Code of SDI Application

Select a new item from the Visual C file menu, select the Visual C project, and select the MFC application in the template window list. As shown:

Fill in: C: / VcppNet /, in the Name box: EX03A and then click OK.

Use the left button to set the project parameters through the Different Application Wizards in the dialog.

ON The Application Type Page, SELECT The Single Document Option And Accept The Defaults for the Rest of the Application, AS Shown Here:

On The Generated Classes Page, Shown Below, Notice That The Class Names and Source-File Names Have Been Generated Based on The Project Name EX03A. You CAN Make

Changes to these names at this point if you want to. Click Finish. The wizard will create your application's subdirectory (Ex03a under / vcppnet) and a series of files in that subdirectory. When the wizard is finished. Look in the application's subdirectory.

In the application type page, select a single document parameter and keep the application's default settings, as shown in the figure:

In the generated class page, it is displayed below. Note that the class name and source file name are based on the project-generated EX03A. If you want to change the name, you can modify it here. Click Finish. Wizard will establish an application in your subdirectory And a series of files (EX03A under / vcppnet). When the wizard is complete. Look at the subdirectory of the application.

Table 3-1 Lists The Files That Are OtRest (for now).

Table 3-1 Important Files in The Application's Subdirectory

File Description

EX03A.VCProj a project File That Allows Visual C .Net To Build Your

Application

EX03A.SLN A Solution File That Contains a Single Entry for EX03A.VCPROJ

EX03A.RC An Ascii Resource Script File

EX03AVIEW.CPP a View Class Implementation File That Contains CEX03AVIEW

Class Member Functions

EX03AVIEW.H A View Class Header File That Contains The CEX03AVIEW CLASS

Declaration

Readme.txt A Text File That Explains The Purpose of The Generated Files

Resource.h a Header File That Contain #define Constant Definitions

Open the ex03aView.cpp and ex03aView.h files and look at the source code. Together, these files define the Cex03aView class, which is central to the application. An object of class Cex03aView corresponds to the application's view window, where all the "action Table 3-1 lists important files (currently)

Table 3-1 Important files in the application subdirectory

File sense

EX03A.VCProj project files make Visual C .NET create your app

The EX03A.sln solution file contains the brief information of the EX03A.

EX03A.RC ASCII resource script file

EX03AVIEW.CPP view class Import file contains CEX03AVIEW member functions

The header file of the EX03AVIEW.H view class contains the declaration of the CEX03AView class

Readme.txt text file Interpretation of the purpose of the result

Resource.h header file #define constant definition

Open EX03AVIEW.CPP and EX03AVIEW.H files and look at the code, these two files define the CEX03AVIEW class, which is the center of the application. A CEX03AView class is equivalent to the application's view window, all "Action "It is there.

2. Compile and link the generated code. In addition to generating code, the MFC Application Wizard creates custom project and workspace files for your application. The project file, ex03a.vcproj, specifies all the file dependencies along with the compile and link option flags ................................................. ..

If the build is successful, an executable program named Ex03a.exe will be created in a new Debug subdirectory underneath / vcppnet / Ex03a. The OBJ files and other intermediate files are also stored in Debug. Compare the files structure on disk with the structure in Solution Explorer, Shown On The Next Page.

Solution Explorer contains a logical view of your project. The header files show up under Header Files, even though they are in the same subdirectory as the CPP files. The resource files are stored in the / res subdirectory.3. Compiled and linked to generate the Code. In addition to the code, the MFC application wizard is a customized item and workspace file for your program. Project file, EX03A.VCProj, specifying all files belonging to compile and connect options. Because new projects become current Project, you can generate an application by generating a menu or clicking the build button on the toolbar, as shown in the figure:

If you generate, a executable named EX03A.EXE will be stored under the newly generated subdirectory in the / vcppnet / ex03a directory .Obj file and other intermediate files are also stored in the debug directory. Use the solution browser Compare the file structure on the disk.

Solution browser arranges a reasonable view for your project file. The header file is placed under Header Files, even if it is in the same subdirectory with those CPP files. Resource files are stored in / res subdirectory .

3 Test the resulting application. Choose Start Without Debugging from the Debug menu. Experiment with the program. It does not do much, does it? (What do you expect with no coding?) Actually, as you might guess, the program has a lot of features-you simply has't activated Them Yet. Close The Program Window when've finished experimenting.

4 Browse the application. Press CTRL ALT J to bring up the Object Browse. If you project settings do not specify browser database creation, Visual C .NET will offer to change the settings and recompile the program for you. (To change the settings yourself, choose Properties from the Project menu. Open the C / C folder, click on the Browse Information property page, and change the Enable Browse Information property to Include All Browse Information (/ FR).)

3 Test the application. If you don't debug, select Start from the Debug Menu. Debug the program. It doesn't do a lot, what do it do? (Do you expect to have no code?) In fact, you may have to guess, This program has many features, you have no flexible use. When you complete debugging, close the program window.

4 Browse the app. Press Ctrl Alt J to call up the object browser. If your project does not establish a detailed browser data setting, Visual C . NET will suggest you change the settings and recompile your program. (You also You can change your settings yourself, select Properties from the Project Menus. Open the C / C folder, click the View Confidence Properties page, select all browser information (/ frs) in Enable the Browse Information Properties box.

After you expand the hierarchy, you Should See Output Similar To this: Compare The Browser Output To Class View:

Class View shows the class hierarchy, much like the Object Browser does. But the Object Browser shows all the functions available on a class, and Class View shows only those that have been overridden. If Class View is sufficient for you, do not bother Building The Browser Database.

After you start the level, you will see a similar interface, compare the class view browser, the class view shows the level of the class, and many object browsers do this. But the object browser displays it. Functions, and class view only shows those who will be heavy. If the class view is enough, then don't establish a browsing database.

The CEX03AVIEW VIEW CLASS

The MFC Application Wizard generated the Cex03aView view class, which is specific to the Ex03A application. (The wizard generates classes based on the project name you entered in the New Project dialog box) Cex03aView is at the bottom of a long inheritance chain of MFC library classes, as shown previously in the Object Browser window. The class picks up member functions and data members all along the chain. You can learn about these classes in the Microsoft Foundation class Reference (online or printed version), but be sure to look at .

The most important Cex03aView base classes are CWnd and Cview. CWnd provides the Cex03aView view class's "windowness", and Cview provides the hooks to the rest of the application framework, particularly to the document and to the frame window, as you'll see in CHApter 12 of this book.

CEX03AVIEW view class

The MFC Application Wizard Class Cex03aview view class is a detailed EX03A application. (This class is generated by the project you entered in the New Project dialog) CEX03AVIEW is a series of inherits from the underlying of the MFC class Come over, also displayed in the previous Object Browser window. This class will be selected from a series of data members. You can learn from the Microsoft's basic class (online help or manual) ), But you must see a description of each base class because these instructions are usually not repeated in the inheritance member function.

The most important base class of CEX03AVIEW is CWND and CView.cwnd Provide "light window" of CEX03AView view class, CView offers to the application framework, unique document and frame window, the same you will see you in Chapter 12 of this book. .Drawing inside the view window: The Windows GDI

Now you're ready to write code to draw inside the view window. You'll be making a few changes directly to the Ex03a source code. Specifically, you'll be fleshing out the OnDraw member function in ex03aView.cpp and working with the Device Context and The Graphics Device Interface (GDI).

Drawing in view window: Windows GDI

Now you are ready to give the view window write draw code. You will directly modify the source code of the EX03A. Special, you will work from the EX03AView.cpp and work with the device context and the graphics device interface.

The OnDraw Member Function

OnDraw is a virtual member function of the Cview class that the application framework calls every time the view window needs to be repainted. A window needs to be repainted if the user resizes the window or reveals a previously hidden part of the window, or if the application changes the window's data, if must inform Windows of the change by calling the view's inherited Invalidate (or InvalidateRect) member function. This call to Invalidate triggers a later call to OnDraw.

Even though you can draw inside a window at any time, it's better to let window changes accumulate and then process them all together in the OnDraw function. That way, your program can respond both to program-generated events and to Windows-generated events such as size.

Ondraw member function

OnDRARW is a virtual function of the CView class. Whenever the view window is to be redrawged, the application frame will call it. One window needs to be redrawn: If the user changes the window size or makes the previously hidden window, the application Change the window data, if Windows must be notified to change the inveralization of the view will call the invalidate. This will first trigger InvaliDate and then call the onDraw

Even if you are alone in the drawing window, it can also have a good time to process the accumulated window changes in the OnDraw function. Therefore, your program can respond to the event generated by the program with Windows, for example, change the size.

The Windows Device Context

Recall from Chapter 1 that Windows does not allow direct access to the display hardware but communicates through an abstraction called a device context that is associated with the window. In the MFC library, the device context is a C object of class CDC that is passed (by pointer) as a parameter to overdraw. After you can call the device cdc Member Functions That do the work of drawing. Windows does not allow you to directly access the display hardware, but communication The context is associated with the window via an abstract call. In the MFC, this device context is the C object class CDC is used to pass the argument. After you have this device context pointer, you can call many CDC functions to draw a drawing work .

Adding Draw Code to the EX03A Program

Now let's write the code to draw some text and a circle inside the view window. Be sure that the project Ex03a is open in Visual C . NET. You can use Class View to locate the code for the function (double-click OnDraw), OR you can open the source code file explorer and local the function yourself.

1. Edit The OnDraw Function in EX03AVIEW.CP. Find The Application Wizard-Generated OnDraw Function in EX03AVIEW.CPP:

Void CEX03AVIEW :: Ondraw (CDC * / * PDC * /)

{

CEX03ADOC * PDOC = getDocument ();

Ask_VALID (PDOC);

IF (! PDOC)

Return;

// Todo: Add a drawing code to this unit data here

}

UNCOMMENT THE POINTER TO The Device Context and Add The Following Boldface Code (Which You Type in) To Replace The Previous Code:

Void CEX03AVIEW :: Ondraw (CDC * / * PDC * /)

{

PDC-> Textout (0, 0, "Hello, World!"); // Prints in Default Font

// & size. Top Left Cornet

PDC-> SelectStockObject (gray_brush); // selects a brush for the

// Circle Interior

PDC-> Ellipse (CRECT (0, 20, 100, 120)); // DRAWS A GRAY CIRCLE

// 100 Units in Diameter

}

Add the drawing code to the EX03A program:

Now let me write some code to the view window. Please confirm that you open the EX03A in Visual C . You can locate the code to the function (double click ondraw), or you can solve resources Manager selects and opens the source code file EX03AVIEW.CPP, locate it to the function. 1. Edit EX03AVIEW.CPP '

Void CEX03AVIEW :: Ondraw (CDC * / * PDC * /)

{

CEX03ADOC * PDOC = getDocument ();

Ask_VALID (PDOC);

IF (! PDOC)

Return;

// Todo: Add a drawing code to this unit data here

}

Remove the annotation of the pointer of the device context, replace the previous code with the increasing black body code:

Void CEX03AVIEW :: OnDraw (CDC * PDC)

{

PDC-> Textout (0, 0, "Hello, World!"); // Print default font

// and size. Top, left short

PDC-> SelectStockObject (gray_brush); // Select a round painting brush

// Draw one inside

PDC-> Ellipse (CRECT (0, 20, 100, 120)); // Gray round

// 100 units in diameter

}

You can safely remove the call to GetDocument because we're not dealing with documents yet. The functions TextOut, SelectStockObject, and Ellipse are all member functions of the application framework's device context class CDC. The Ellipse function draws a circle if the bounding rectangle's length Is equal to its width

The MFC library provides a handy utility class, CRect, for Windows rectangles. A temporary CRect object serves as the bounding rectangle argument for the ellipse drawing function. You'll see more of the CRect class in quite a few of the examples in this book .

You can rest assured that we don't need a document. Function TextOut, SelectStockObject, Ellipse is a member function of the application framework device context class CDC .ellipse function is to draw round If you want to draw a rectangular length equal to the width.

The MFC provides an easy, useful class, CRECT, which can be rectangled for Windows. A temporary CRECT object also draws a rectangle in the EILLPSE drawing function. You will see more CRECT class in this book. example.

2 Recompile and test ex03a. Choild from the build menu, and if The application again. Now you have a program at visibly does

2 recompile and test EX03A. Select Generate the generation of menu, if your application does not have a compilation error in the secondary test. Now your program obviously makes some things.

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

New Post(0)