Sword Based on MFC DocumentViewFramework

zhaozj2021-02-16  47

Among my colleagues, many people are using VC / MFC, but many people are always easily turned to the MFC documents, although many of their tables have a lot of information about VC. In my learning, I found the following understandings and some call functions are the most critical. Whenever someone asks me about the problem with this, I will have a lot of things to explain it after sharing the following documents. . Although these can eventually find the answer from the MSDN, some people are afraid of MSDN; I hope you have this information to greatly reduce your fear.

L preparation

1. Document class is usually related to the access of the data and other processing processes, while it is primarily used to express document data, but each view must be included in a frame (attached to mainframe in SDI, attached to childframe in MDI) The toolbar, menu, etc. can be placed on the frame;

2. Some types of documents, frameworks, and a commonly associated constitute a document template class (base class is CDOCTemplate, its subclass CSINGEDEMPLATE, and CMUTIDEMPLATE), the document template class is constructed in the initialization of WinApp;

3. A app can have n document templates, but the SDI and MDI framework wizards are only one template for default (if you need more, you can imitate the addition); a document template can generate multiple document instances multiple times in the program This so-called multi-document interface MDI (multi-document can not only be multiple different types of documents, but also multiple different types of documents); a data can have n types of expressions, so a document can have n-dependent Relational;

4. Only a movable document, depending on, and the framework at a moment. That is, the current document, the view, and the framework.

Understand these plus the following operations, you can handle this problem.

l Interactive function between MDI objects

Class function return value Global AfxGetApp CWinApp * (pointing to the current program) Global AfxGetMainWnd CWnd * (pointing to the program's main window frame) CMDIFrameWnd MDIGetActive CMDIChild Wnd * (pointing to the currently active MDI child window) CWnd GetParentFrame CFrameWnd * (point to the parent window frame) CFrameWnd GetActiveView CView * (pointing to the current active viewport) CFrameWnd GetActiveDocument CDocument * (points to the currently active document) CView GetDocument CDocument * (linked with the view class) the first position in the list view class views associated with the document CDocument GetFirstViewPosition in the same document CDocument GetNextView The next view class location in the relevant view list

E.G. CMAINFRAME * MFRM = (CMAINFRAME *) AFXGETMAINWND ();

E.G .: Get the current view:

CframeWnd * Pmain = (cframewnd *) AFXGETMAINWND ();

CframeWnd * pchild = (cframewnd *) PMain-> getActiveFrame ();

CView * m_pagentview = (cView *) pchild-> getActiveView ();

l Access the current activity view and activity documentation

1. For the SDI program, the main frame window is the document box window, which can take the following method to get the current documentation and view:

u Get the event document:

CMYDocument * pdoc;

PDOC = (CMYDocument *) ((cframewnd *) AFXGetApp () -> m_pmainwnd) -> getActiveDocument ();

or

PDOC = (CMYDocument *) (cframeWnd *) AFXGETMAINWND ());

Both are equivalent.

u get an active view:

CMYVIEW * PVIEW;

PVIEW = (CMYVIEW *) ((cframewnd *) AFXGetApp () -> m_pmainwnd -> getActiveView ();

2. For the MDI program, since the sub-window is the document box window, first get the active subframe window with getActiveFrame (), then obtain the activity document and the view through the sub-window:

CMDICHILDWND * PCHILD = (cmdichildwnd *) ((cframewnd *) AFXGetApp () -> m_pmainwnd -> getActiveFrame ();

u Get the event document:

CMYDocument * pdoc = pchild-> getActiveDocument ();

CMYVIEW * PVIEW = (CMYVIEW *) PCHILD-> getActiveView ();

The above function fragment can be made into static member functions, such as:

Static CMYDocument :: getcurrentdoc ()

{

CMDICHILDWND * PCHILD = (cmdichildwnd *) ((cframewnd *) AFXGetApp () -> m_pmainwnd -> getActiveFrame ();

CMYDocument * pdoc = pchild-> getActiveDocument ();

}

This will acquire an active document (or view) in the following manner:

CMYDocument :: getcurrentdoc ();

Note: No specific objects are associated with the static member function call.

l Looking for a document in the document template

example:

CMULTIDEMPLATE * PDOCTMPL;

PdoctMPL = ((cimpapp *) AFXGetApp ()) -> m_pdoctemplate;

Position Posdoc = pdoctmpl-> getfirstdocPosition ();

While (null! = POSDOC)

{

Pfrmdoc = (cfrmdoc *) pdoctMPL-> getNextdoc (POSDOC);

NDocorder ;

}

May these can become the sword of MDI, SDI programs, welcome to supplement and advise!

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

New Post(0)