Understanding MFC documents, views, and frameworks [go]

Source: Internet
Author: User

 


Understanding document/view framework
Source. Raytheon

Understanding the interaction between documents and views is the basic skill for compiling an MFC program. However, the application framework of MFC encapsulates the relationship between documents and views, so beginners are often unable to get started. Therefore, writing programs is often confined to the Framework generated by the wizard. This article will try to explain how the frameworks in the white document view work, hoping to help some friends.
Concepts:
(As we all know, Raytheon will repeat it once)
Document Object: used to save data.
View object: displays and edits data.
Application Framework: the framework is used to manage different document Display Interfaces. For example, if you have a data grid display interface and a graphic display interface, the data may all come from your documents, but the views are different. How to use the framework. Why not use a view? In order to take out the interface management independently.
Document Template: MFC regards documents/views/frameworks as one. As long as you create a document/view framework structure program, these three classes will be created for you. This task is completed during application initialization as follows:

Bool cmyhtmlapp: initinstance ()
{
//......
Csingledoctemplate * pdoctemplate;
Pdoctemplate = new csingledoctemplate (
Idr_mainframe,
Runtime_class (cmyhtmldoc ),
Runtime_class (cmainframe), // main SDI frame window
Runtime_class (cmyhtmlview ));
Adddoctemplate (pdoctemplate );
//......
}

Single Document: only one file can be opened at a time, regardless of the number of supported document types. You can create a single document program that supports all image formats, but it can only open one document at a time.
Multi-document: You can open multiple files, regardless of the document type. You can also create a program that can open multiple documents at the same time, but it only supports one document type.

When do I need a document/view framework structure?
First, you do not need to use the framework structure such as document view, even in MFC. You can use this method when you need it. You can complete a program with only views without documents, such as a dialog box-based application.
When do I need it?
When you want to separate your data layer from the interface layer.
Data operations are usually stored in the document class, such as access, open, and close. Here you can operate on your data as much as possible. If you need to update the view after changing the data, then the program will present your changes to the user of your program. It can be seen that the role of a view is to provide an interface for data exchange between users and data. Its role is to display data as needed and provide an input interface as needed. After the user inputs the data, the actual data operations are performed by the document class. What is the framework class doing?
Framework classes exist to facilitate the management of your document classes and view classes. Generally, our operations are completed through the View window. messages are received and processed by the view. Therefore, message ing is generally defined in the view. However, if an application simultaneously has multiple views and the current activity does not process the message, the message will be sent to the Framework Window. In addition, the Framework Window can easily process non-window messages.


Let's talk about the generation process of a typical single-document Program (incomplete, only useful)
1. The cwinapp object is created. This object is global and can only have one object named theapp. At this time, you can do some work, such as registry operations. (If you want to write a software without modifying the registry, you need to write it here)
2. Create a document template in the initinstance () function. The document template uses the cruntimclass static member pointer as the construction parameter.
3. Execute the default command line parameters of the MFC framework. One of the many command line parameters is cmd1, which creates a new file. (If there is no command line parameter, execute the default id_file_new)
4. Document Template instances create documents, views, and frameworks based on the dynamic creation Information of the three categories.
5. initialize documents, views, and frameworks.

We have a certain understanding of how documents, views, and frameworks are generated and their purposes, and how to effectively use them.
Interaction between documents, views, and frameworks.
The process of generating the preceding typical single-document program shows that a complete application is generally composed of four classes: cwinapp application class, cframewnd framework class, cdocument class, And cview class. I will list the four common member functions. However, the parameters and returned values are not listed. You can learn more from msdn. Several important virtual functions are not described yet. Let's take a look.

The global function afxgetapp can be used to obtain the Global Object theapp of the cwinapp application class.
Cwinapp
Data member:
M_pszappname Application name
M_pszexename name of the executable file
M_pszprofilename INIFILE name
M_pszregistrykey key of the registry or INI File
M_hinstance instance handle
M_pmainwnd is the frame window pointer
Member functions:
Initinstance () // Initialization
Parsecommandline () // parses the command line

Cframewnd
Getactivedocument () // get the pointer to the current active document
Getactiveview () // get the current active video pointer
Setactiveview () // set the current view as the active View

Cdocument
Onnewdocument ()
Onopendocument ()
Onsavedocument ()
Onfileclose ()
// The above is used for document operations
Getfirstviewposition () // the first position in the linked list of document objects
Getnextview () // next
// The above is used to traverse all views associated with the document
Getdoctemplate () Get the document template pointer
Addview () // Add a view
Removeview () // delete a view
Updateallview () // update all views

Cview
Get the corresponding document pointer using getdocument ().

The rest will not be listed. Let's take a look at msdn. You can directly view the class members of the cwinapp application class, cframewnd framework class, cdocument class, And cview class.


Finally, let's talk about several common problems.
1. Why is no document template found in the dialog box application?
The default Dialog Box program does not use the document/view framework structure.

2. Does it mean that document classes are not required if I use a database as a data source?
Look at yourself, but I suggest using it. Because the document and view can be used, this is a clear and convenient framework structure, and facilitates the interaction between the three.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.