Relationship between MFC templates, documents, views, and frameworks

Source: Internet
Author: User

Class cwinthread: Public c0000target
{
Cwnd * m_pmainwnd;
};

Class cdocmanager: Public cobject

{

Cptrlist m_templatelist;

};

Class cwinapp: Public cwinthread
{
Cdocmanager * m_pdocmanager;
};

1. templates, documents, and views, FrameworkRelationship
(1)DocumentRetain the view of this documentList and pointer to the document template that creates the document. The document must have at least one associated view.Window partition class (csplitterwnd) Allows documents to have multiple views, while viewsOnly one document can be associated.
(2)ViewRetain the pointer to its document and include it in its parent frameworkWindow;
(3) DocumentFrameworkWindow(Include View(MDI child window) to keep pointing to its current active ViewPointer;
(4)Document TemplateKeep the list of opened documents and maintain the frameworkWindows, documents, and viewsIng;
(5)ApplicationsRetain the list of its document templates.

We can use a group of functions to make these classes accessible to each other. Table 6-1 lists these functions.
Table 1-1 Documents, document templates, and viewsAnd frameworkClass mutual access

 

From this object

How to access other objects

Global Functions

Call the global function afxgetapp to obtain the cwinapp application class pointer.

Application

Afxgetapp ()-> m_pmainwnd is the frame window pointer; Use cwinapp: getfirstdoctemplatepostion, cwinapp: getnextdoctemplate to traverse all document templates

Document

Call cdocument: getfirstviewposition, cdocument: getnextview to traverse all views associated with the document. Call cdocument: getdoctemplate to obtain the document template pointer.

Document Template

Call cdoctemplate: getfirstdocposition, cdoctemplate: getnextdoc to traverse all corresponding documents

View

Call cview: getdocument to obtain the corresponding document pointer. Call cview: getparentframe to obtain the Framework Window.

Document Framework Window

Call cframewnd: getactiveview to obtain the pointer to the current active view. Call cframewnd: getactivedocument to obtain the document pointer appended to the current view.

MDI frame window

Call cmdiframewnd: mdigetactive to obtain the MDI subwindow (cmdichildwnd) of the current activity)

Let's take an example to describe how to use the functions in the above table and write a piece of code to traverse the document templates, documents, and views.Functions:

 

Cmyapp * pmyapp = (cmyapp *) afxgetapp (); // get the application pointer
Position p = pmyapp-> getfirstdoctemplateposition (); // get 1st document templates
While (p! = Null) // traverse the Document Template
{
Cdoctemplate * pdoctemplate = pmyapp-> getnextdoctemplate (p );
Position p1 = pdoctemplate-> getfirstdocposition (); // obtain the 1st documents corresponding to the document template.
While (p1! = Null) // traverse the document corresponding to the Document Template
{
Cdocument * pdocument = pdoctemplate-> getnextdoc (p1 );
Position p2 = pdocument-> getfirstviewposition (); // obtain the 1st views corresponding to the document.
While (p2! = Null) // traverse the view corresponding to the document
{
Cview * pview = pdocument-> getnextview (P2 );
}
}
}

It can be seen that the following management relationships and implementation approaches are completely similar:
(1) apply to document templates;
(2) The document template is for the document;
(3) view documents.

 

××××××××××××××××××××××××××××××××××××××××××× ×

1. The application object has a document template manager cdocmanager * m_pdocmanager (New when adddoctemplate is called for the first time)

2. The document template manager has a list of document template objects cptrlist m_templatelist (the adddoctemplate function is responsible for adding this list)

3. The document template object has a static cruntimeclass member pointer of the document, view, and framework for dynamic creation, and a m_nidresource is used to represent the UI object to be used.

4. Each document template object has m_ponlydoc or m_doclist (document pointer or document pointer list), onfilenew and onfileopen call opendocumentfile of the document template object, and opendocumentfile call createnewdocument of the document template, createnewdocument then calls adddocument of the document template to fill the document list or document pointer

5. The document object has a document template pointer m_pdoctemplate (back to the template object of the Document Object ). same as above, it is also the adddocument member function of the document template to set this pointer (the document template itself ). plug the file object you just created

6. The document object has a m_viewlist (View list). Both onfilenew and onfileopen call opendocumentfile of the document template object. This function calls createnewdocument to create the document and then call createnewframe to create the framework object.

Createnewframe: Construct a ccreatecontext object
Ccreatecontext: (1) newly created document pointer (2) cruntimeclass pointer of the View
After createnewframe creates a framework object, it calls loadframe.
The last parameter of loadframe is the ccreatecontext pointer.

Loadframe calls create, create and then createex. The last parameter is the ccreatecontext pointer.
The call to create is triggered by the message ing table. cframewnd: oncreate is called.
One of the fields of the oncreate lpcreatestruct is still the ccreatecontext pointer.

Then, in cframe: oncreate, The cruntimeclass (view) of the ccreatecontext calls Createobject.
After a view object is generated, the object calls create (the last parameter is still the ccreatecontext pointer)

Create of view object is called OnCreate of view object caused by message ing table
The value of the OnCreate parameter of the view is the lpCreateParams of the lpcreatestruct or the CCreateContext pointer)
Therefore, use the CCreateContext member m_pCurrentDoc (current document)
To call CDocument: AddView to add the view to the View list of the document.

7. The view has a document pointer m_pDocument (pointing to the document)
Same as above, it is also initialized by the CDocument: AddView function, as shown below:
PView-> m_pDocument = this;

8. The framework has a m_pViewActive (activity view)
Set by SetActiveView of the framework

The above can be viewed in comparison to "MFC Review (3) MFC Document view structure ".

 

Document Template manager CDocManager * m_pDocManager

The document template manager has a list of document template objects CPtrList m_templateList

The document template object has a static CRuntimeClass member pointer of documents, views, and frameworks for dynamic creation.

Each document template object has a document pointer list m_docList

The document object has a document template pointer m_pDocTemplate (back to the template object to which the document object belongs ).

The document object has a view list m_viewList

The view has a document pointer m_pDocument (pointing to the document)

The framework has a m_pViewActive (activity view)

 

From

Http://hi.baidu.com/king_fp/blog/item/210a0a6dc99146ff42169430.html

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.