Analysis of MFC (2) Document Operations Process in Document View Structure

zhaozj2021-02-08  242

Document view structure document operation flow

FMD (http://www.fmdstudio.net)

A basic architecture

A program can be seen as based on the processing of the document, and the program handable document type may have a variety, and each document may be opened simultaneously. Each document can be divided into data parts and performance portions.

Based on this understanding, the MFC document view framework summarizes each functional module as an object, with these object classes that are associated with each other.

1 Document Type Management

CWINApp Representative, which handles multiple types of documents, so that there must be an internal member of the storage management document type, this member is

CDOCMANAGER * M_PDOCMANAGER

CDOCManager is responsible for managing document types

CDOCManager internal

Cptrlist m_templatelist; (pointer chain table)

Record document type

2 document type implementation

CDOCTemplate (Document Template) represents the specific document type

A program that can be processed, for the corresponding document template

The pointer of these template objects is recorded in m_templatelist, and manages M_PDOCMANAGER

3 Document template implementation

For a certain document, its internal data structure, and external performance form, and the operation commands for it are relatively fixed, the document template has three components, document data, using CDOUCUMENT management; documentation, CView representation; Documents are managed by CFrameWnd in a container, and menu, tool bars, and other commands in the window system. If necessary, the same document may require multiple expression, that is, a cdocument may associate several CView, but when the template declares, only one document corresponds to one view.

This information is recorded in the three members of them.

Cruntimeclass * m_pdocclass; // Class for Creating New Documents

Cruntimeclass * m_pframeclass; // Class for Creating New Frames

Cruntimeclass * m_pviewclass; // Class for Creating New Views

One document template, corresponding to a file type, and this type of file can have a lot, these corresponding files will be recorded

Cptrlist m_doclist;

In addition, the document template also includes resources corresponding to the type of document, such as menu, title, icon, etc.

Establishment of two document types

At the beginning of the program, (in InitInstance) will create a document type

Establish multiple document types (ccmultidoctemplate), then add (AddDDDDDEMPLATE) to CWINAPP's document type list m_templatelist to establish a file type table, file new, open, open, first look for this file type lin list, find out The type of document is obtained, and the document template is obtained by the document template. The documentation should have a data structure, the graphical representation, and operation of the document, and the like.

New construction of the three documents

1 Interface command ID_FILE_NEW is issued

CWINAPP :: OnFileNew () Processing

Pass by CDOCManager * m_pdocmanager (document type management)

2CDocumanager View the file type list, such as the plurality of types of types of documents, give a type selection opportunity.

If there is only one document type in the document type list, this type is selected directly.

3 After finding the document template, the openocumentfile (..) member of the document template will call the document template, the incoming parameter is NULL, indicating that new construction

i. OpenDocumentFile first creates the data section of the document:

CDocument * pdocument = createNewDocument (); This will establish its actual data object according to the cdocument class recorded by m_pdocclass.

After the data object is established, add it to the cptrlist m_doclist, and record all the actual files that open to this document template. At the same time, cdocument also has a list cptrlist m_viewlist; a (cView) view used to record the document object, the view allows multiple (the same data, there may be different expression forms).

II. Establish a container in the form of performance, the frame window

CframeWnd * Pframe = CreateNewFrame (PDocument, NULL);

Frame objects are established based on the framework M_PFrameclass recorded by the document template.

Iii. When establishing a frame object, the type M_PViewClass of CVIEW (data image, data image) will be transmitted to the frame object as a parameter.

CcreateContext context;

CONTEXT.M_PNEWVIEWCLASS = m_pviewclass;

Then the frame object creates its actual window

IF (! pframe-> loadingframe (m_nidresource,

WS_OVERLAPPEDWINDOW | FWS_ADDTOTILE, // DEFAULT FRAME STYLES

Null, & context)

Create generated window will be called in LoadFrame.

When the Frame window establishes a client area, create the CREATEVIEW (PCONTEXT, AFX_IDW_PANE_FIRST) to establish the CView object set by the template.

And create forms pView-> Create (NULL, NULL, AFX_WS_DEFAULT_VIEW, CRECT (0, 0, 0, 0), this, NID, PCONTEXT))

When CVIEW is established, there is an onCreate () to add yourself to the view list of the corresponding document:

PCONTEXT-> M_PCurrentDoc-> AddView (this);

4 Document Object (Generally, the data section of the document (file) becomes a document object, distinguishable with the previous document meaning), the document object's performance section (view object) and the window container in the Windows system (frame object It is also established here, which is the initialization of each object.

i. pdocument-> OnnewDocument ()

There is a new or initial number of the corresponding data in OnNewDocument ().

After completing, add m_nuntitledcount 1, this number is used to count the new document, which is used to give new files from the default name (for example: unnamed 3.txt);

(If OpenDocumentFile (.) Has a parameter (when opening the file), the pDocument-> onopendocument () is initially initial data object,)

II. InitialUpdateFrame Initialization Framework

A CVIEW object will be activated in InitialUpdateFrame (if not, it is other sub-window)

And send SendMessageTodescendants (WM_InitialUpdate, 0, 0, True, True) to all sub-windows;

WM_InitialUpdate is an MFC custom message for implementation of correlation,

In CView :: OnInitialUpdate, you can write to the initial operation you need to add.

Activate the frame window or the corresponding view.

Later, update the frame window title

5 Since then, the new document process ends.

Four documentation

1 Response Open Command ID_FILE_OPEN

CWINApp :: onfileopen ()

Call m_pdocmanager-> onfileOpen ();

2DOPROMPTFileName Provides Open File dialog, get file name 3 call AFXGetApp () -> OpenDocumentFile (newname);

(This is different from the new file)

This is a virtual function of the application class, which can add specific processing.

Under normal circumstances, CWINAPP :: OpenDocumentFile (NewName) will then be called;

4 back to CDOCManager

m_pdocmanager-> OpenDocumentFile (LPSZFileName);

Find a list of document templates to find the document template that the document should be used.

Call the MatchDoctype of each template to determine if it is appropriate, or if it has been opened.

PTEMPLATE-> MatchDOCTYPE (Szpath, PopeCument)

If the file has been opened by a template, activate the corresponding framework, attempt.

If it is not open, there is a template, use this document template to open the file.

PbestTemplate-> OpenDocumentFile (Szpath);

If it is not the file type of the program, the error returns.

5 Open the file with the most suitable document template found.

CMULTIDOCTEMPLATE :: OpenDocumentFile

6 ....

The following steps are the same as the new file 3, but the incoming parameters are different.

When you build a document object, you will call CDocuMet :: OpenDocumentFile

Not cdocumet :: onnewdocument ();

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

New Post(0)