VC ++. Net Chapter 2

xiaoxiao2021-03-06  50

The CMYFRAME CLASS

An object of class CmyFrame represents the application's main frame window. When the constructor class the Create member function of the base class CframeWnd, Windows creates the actual window structure and the application framework links it to the C object. You must call the SbowWindow and UpdateWindow Functions, Also Member Functions of The Base Class, in Order To Display The Window.

CMYFRAME class

Declare a main window class named CMYFrame in your application. When the member function of the base class is, Windows really sets the window structure and connects to the C object connected to the application frame. You must call the showWindow and UpdateWindow functions They are also a member function of the base class, sequential display window.

The cmyframe :: Onlbuttondown Function

This function is a sneak preview of the MFC library's message-handling capability. I've elected to "map" the left mouse button down event to a CmyFrame member function. You'll learn the details of the MFC library's message mapping in Chapter 5 .............................

CMYFRAME :: ONLBUTTONDOWN function

This function is in advance to view the ability of the MFC message processing. I will use the "map" to map this word instead of the mouse that caused by the CMYFrame member function. You will learn more detailed MFC message mapping in Chapter 5. Content. In fact, when the user presses the left mouse button is called to receive this information. This function calls the TRACE Macro in the MFC Displays the message in the debug window.

The cmyframe :: onpaint function

The application framework calls this important mapped member class CmyFrame every time it's necessary to repaint the window: at the start of the program, when the user resizes the window, and when all or part of the window is newly exposed The CpaintDC statement relates to. The Classic Graphics Device Interface (GDI) and is explained in Later Chapter CHAPTERS. The Textout Function Displays "Hello, World!" (WE'LL Look At GDI WHEN We discuss .NET).

CMYFRAME :: OnPaint function

The application framework must be redrawged each time you call the CMYFrame class. You must redraw the window: At the beginning of the program, when the user redes the window, and when all or part of the window is redisplayed. In this

The chapter will introduce the relationship between CPAINTDC in the image device interface .TextOut function displays "Hello, World!" (I discussed. Net considering GDI )

Application Shutdown

The user shuts down the application by closing the main frame window. This action initiates a sequence of events, which ends with the destruction of the CmyFrame object, the exit from Run, the exit from WinMain, and the destruction of the CmyApp object.

Application close

When the user passes through the main frame window, things are done, first destroy the CMYFRAME object, exit the run, exit WinMain, and finally destroy the CMYAPP object.

Look at the code example again. This Time Try To Get The Big Picture.

Most of the application's functionality is in the MFC Library Base

Classes CWINAPP AND CFRAMEWND. I'VE FOLLOWED A FEW

SIMPLE STRUCTURE RULES AND HAVE Written Key Functions in My Derived

Classes. C lets you "borrow" a Lot of code without Copying It. Think

Of it as a partnership between you and the application framework. The application framework.

Application Framework Provides The Structure, And You Provide the Code

That Makes the Application Unique.

At a consideration of the code in the example, try to get a big outline. Many applications

All in the MFC base category CWINAPP and CFRameWnd. In myApp, we track some simple structures.

The key function in the class .C allows you to "borrow" many of the code other than it, copy it. Thanks to the application

Rack partnerships with you. The structure of the application box and the code you provide are unique in the application.

Now You're Beginning to See Why The Application Framework Is More

Than Just A Class Library. NOT Only Does The Application Framework Define

The Application Structure, But It Also Encompasses More Than C Base

Classes. You've Already Seen The Hidden Winmain Function At Work. Other

Elements Support Message Processing, Diagnostics, DLLS, And So Forth.

Now just start to see more class libraries in the application framework. Application framework is not only defined

The program structure, it also contains a lot of C basic classes. You have seen hidden WinMain functions work

After you will see other elements, support messaging, diagnostics, dynamic connection libraries, and more.

MFC Library Message Mapping

Take a look at the onlbuttondown member function in the previousexample. You might thinktemples you might think That IT Would Be An IDEAL CANDIDATE for a Virtual

Function. A Window Base Class Would DEFINE VIRTUAL FUNCTIONS for MOUSE

Event Messages and Other Standard Messages, And Derived Window Classes

Could Override The Functions As Necessary. Some Windows Class Libraries

Do Work this.

MFC message mapping

Ombuttondown member functions in the note example. You may think that it is a perfect representative of virtual functions

The window base class will define the virtual function of the mouse event message and other standard messages, and you must overload from the window.

The function of the mouth. Length Windows class library works this.

However, The MFC Library Application Framework Doesn't Use Virtual

Functions for Windows Messages. Instead, IT Uses Macros to "Map"

Specified Messages to Derived Class Member Functions. why the rejection

Of Virtual Functions? Suppose The MFC Library Used Virtual Functions for

Messages. The CWND Class Would Declare Virtual Functions for more Than

100 Messages. C Requires a Virtual Function Dispatch Table, Called A

VTable, for Each Derived Class Used in a Program. Each VTable Needs One

4-Byte Entry for Each Virtual Function, Regardless of Whather

Functions Are Actually Overridden in The Derive Class. Thus, for Each

Distinct Type of Window OR Control, The Application Would Need A Table

Consisting of over 400 bytes to support Virtual Message Handlers.

However, the MFC application framework does not use virtual functions for Windows messages. Change to, it uses Hongli

Specify a message to the class member function for "MAP". Why do you refuse to use a virtual function? Imagine that the MFC library uses virtual functions for messages. CWnd classes will define virtual functions of up to 100 messages .c request virtual function allocation table, call VTABLE Each class in the program does this. Each VTable requires all 4 bytes of entry space for each virtual function. No matter how much the actual needs are exceeded. Thus, for each of the different types of windows and classes The application will require a support virtual message manager consisting of 400 bytes.

What About Message Handlers for Menu Command Messages and Messages

From Button Clicks? You Couldn't Define these As Virtual Functions in

A Window Base Class Because Each Application Might Have A Different Set

Of Menu Commands and Buttons. The MFC Library Message Map System Avoidslarge Vtables, and IT Accommodates Application-Specific Command Messages

In Parallel with Ordinary Windows Messages. It Also Allows SELECTED

NonWindow Classes, Such As Document Classes and The Application Class,

To Handle Command Messages. The MFC Library Uses Macros to Connect (OR

Map) Windows Messages to C Member Functions. No EXTENSIONS TO The C Language Are Necessary.

What is the message manager that menu command message and button Click the message? You can't be in the window base class

Define these virtual functions because each application may have different menu names and buttons. MFC messages

The mapping system eliminates the huge virtual table, which is provided to the application compared to the ordinary Windows message.

Detailed command message. It also allows you to select a non-window class, such as document classes and application classes to process commands

Message. MFC uses macro (or mapping) to Windows messages to C member functions. C language non-expansion

It's required.

AN MFC Message Handler Requires A Function Prototype, A Function Body,

And an entry (macro invocation) in the message map. The property window

Helps you add message handlers to your classes. You SELECT A Windows

Message id from a list box, and the wizard generates the code with the

Correct Function Parameters and Return Values.

MFC's message manager request function prototype, function body, and entry (macro command) message mapping. Pass

The property window helps you can add a message manager in your class. You can choose from the list box

The Windows Message ID, and the wizard generates code, correct function parameters, and return values.

Documents and views

The Previous Example Used An Application Object And A Frame Window

Object. Most of Your MFC Library Applications Will Be More Complex.

Typically, They'll Contain Application and Frame Classes Plus Two Other

Classes That Repesent The "Document" and the "view".

This Document-View Architecture is The Core of the Application Framework

And is loosely based on the model / view / controller class from there. From the MODEL / VIEW / CONTROLLER

Smalltalk world.

Documents and views

The previous example uses an application object and a frame window object. Most of your MFC

Applications will be very complicated. Typically, they increase on the basis of applying and framework classes

Two new classes, expressed as "documents" and "view". Documents and view architectures are the core of the application framework

Heart, it is the class of the SmallTalk world and is dispersed in the module / view / controller.

In Simple Terms, The Document-View Architecture Separates DataFromthe User's View of the Data. One Obvious Benefit Is Multiple Views of

The Same Data. Consider a Document That Consists of a Month's Worth of

Stock quotes Stored on Disk. suppose a Table View and a chart view of the

Data Are Both Available. The User Updates Values ​​THROUGH THE TABLE VIEW

Window, and the Chart View Window Changes Because Both Windows Display

The Same Information (But in Different Views).

Under some simple conditions, the data separated by the document view architecture is from the user's own view.

According to. Some useful multi-document views are the same. Considering that a document is stored on disk

More than a month of results. Suppose there are two useful views, one is a table view and a chart view. Use

To update the data through the table view window and the chart view window, because the two windows are displayed

Information. (But different views)

IN AN MFC Library Application, Documents and Views Are Represented

INSTANCES OF C Classes. Figure 2-1 Shows Three Objects of CLASS

Cstockdoc Cor-Responding to Three Companies: AT & T, IBM, AND GM. All three

Documents Have A Table View Attached, And One Document Also Has A Chart

View. as you can see, There Are Four View Objects-Three Objects of Class

CstockTableView and One of Class CstockChartview.

In an MFC application, documents and views are declared as a C class. Figure 2-1 shows

The response between three types of objects CstockDoc with three companies: AT & T, IBM, and GM. There are three documents.

Table view, one is chart view. For example, these four view objects - three

CstockTableView class object and a cstockchartview class object.

The document base class code interacts with the File Open and File Save commands; the derived document class does the actual reading and writing of the document object's data (The application framework does most of the work of displaying the File Open and File Save dialog boxes. . and opening, closing, reading, and writing files) The view base class represents a window contained inside a frame window; the derived view class interacts with its associated document class and does the application's display and printer I / O The derived view class. . and its base classes handle Windows messages the MFC library orchestrates all interactions among documents, views, frame windows, and the application object;. mostly through virtual functions the role of the document base class is to use the file open and file save command source document class reality. Is reading and writing document objects

Data. (Most of the application framework is to display file open and file save dialog and open, close, read

And write files). View base class is expressed as the frame window in the window. The role of the source view class is the federated document class and application

Display and print I / O. Source view class and its base class handle Windows message. MFC library mainly edits through virtual functions

Set, view, frame windows, and application objects. This orchestra makes it works.

Do not think that a document object must be associated with a disk file that is read entirely into memory. If a "document" were really a database, for example, you could override selected document class member functions and the File Open command would bring Up a list of databases INSTEAD OF A LIST OF FILES.

Don't think that the document object must be associated with disk files, it can be read from memory. If a "document" is a real database, for example, you can reselect the document class member function and file open command will propose Database list Instead of file list.

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

New Post(0)