Mutual acquisition of Doc,view,mainfrmae,app pointers in MFC

Source: Internet
Author: User

The app is the app domain, and everything in the domain can be accessed through a global function.

Mainframe is the main frame and can be accessed basically with global functions.

Mainframe is a number of view and document (possibly unpaired) in several childframe,childframe, Childframe manages the interoperation of View,view and document.

Therefore, the overall framework is out, generally in addition to direct application of the relationship can be accessed through the mainframe-->active childframe-->active view-->document this line,

      1:   because for SDI programs, the main frame window is the document box (if this is not known, It is necessary to look at the composition of the single document under MFC.  
          The following is about the .  of a single document           
           
        Example: How to get a pointer to a view class in the CMainFrame framework.
                     can get the frame pointer first and then call the GetActiveView function to point to the current Activity view. &NBSP

             c **view * pVi ew
               pview= (c**view*) (( cframewnd*) AfxGetApp ()->m_pmainwnd)->getactiveview ();

Of course these may all know is so used, but the real m_pMainWnd and AfxGetApp () what meaning perhaps some people do not understand.
Everyone probably knows how to get the mainframe pointer (frame Class) in the app: the m_pMainWnd variable in CWINAPP is the pointer to CMainFrame.

So under other classes you can get m_pMainWnd first, you get the mainframe pointer. So to get a pointer to the view class, you must first get the CFrameWnd pointer m_pMainWnd, and then the GetActiveView under Call Framewnd point to the current Activity view.
The origin of m_pMainWnd:
Each MFC application has an object that CWinApp a derived class. This object corresponds to the main thread of the program. The CWinApp class has a CWnd * M_PMAINWND member variable. This member variable records the main window of the application.
When you create a new MFC application, the m_pMainWnd assignment statement appears in the InitInstance virtual function. The only exception is the single-document interface of the MFC application, you cannot see this code in the InitInstance function, because it has been hidden in the ProcessShellCommand in this function. As a result, you can conclude that if you create your own window class, you should assign the object of this class to m_pMainWnd.

And this member can only be used in the C**app class, so how to use the CWinApp class of the CWnd type of variables to get the main frame window pointer??
The AfxGetApp function is only possible because AfxGetApp () gets the object of the CWinApp class, and the AfxGetApp return value is the CWinApp object pointer, which is the object defined in the C**app.cpp generated by MFC (the pointer to the object Theapp).

Because you get Cwndapp member functions or member variables in the project you create, you must cast to a class in your own project to find member functions or variables.
Note: In a single document, the simplest way to get a visual pointer is to
((C**view *) Cframewnd::getactiveview ())

2: Of course, you can also get pointers to document classes in Framewnd:
cmydocument* PDoc;
Pdoc= (cmydocument*) ((cframewnd*) AfxGetApp ()->m_pmainwnd)->getactivedocument ();


3: From above can know: How to get mainframe pointer in view
CMainFrame *pmain= (CMainFrame *) AfxGetApp ()->m_pmainwnd;

Note: Get the main frame window class pointer from the view class: With the function: Cwnd::getparentframe () or AfxGetMainWnd () Also
can achieve the purpose. GetParentFrame () works by searching in the parent window chain until CFrameWnd or its derived class is found, and returns its pointer.

((CMainFrame *) cwnd::getparentframe ())
Or
((CMainFrame *) AfxGetMainWnd ())

Two:
Of course, for MDI programs, because the child window is the document box, you first get the active child frame window with GetActiveFrame (), and then get the active view and the document from that Subwindow:

Cmdichildwnd*pchild= (cmdichildwnd*) ((cframewnd*) AfxGetApp ()->m_pmainwnd)-

>getactiveframe ();

Get the active view:
cmyview* pview= (cmyview*) Pchild->getactiveview ();

Get the active document:
cmydocument* pdoc=pchild->getactivedocument ();


Note: You can also use this method to get a view pointer in multiple documents
Get the active child frame window
cmdichildwnd* pchild= (cmdichildwnd*) getactiveframe ();
Or:
cmdichildwnd* pchild=mdigetactive ();
Get the active view of the active sub-frame window
cmyview* pview= (cmyview*) Pchild->getactiveview ();


Three:
1. Obtaining a pointer to a document class from a view class
Before you need to reference the document class in the view class, use the following statement:
C*doc *pdoc= (c*doc*) getdocument ();
You can then use the Pdoc pointer to access the document class.
2. Pointer to a view class from a document class The CDocument class provides two functions for locating the view class:

GetFirstViewPosition () and GetNextView ()

Note: The parameters in GetNextView () are referenced in parentheses, so the post-execution value may change. GetFirstViewPosition () for

Returns the first view position (not a pointer to the view class, but a position type value), GetNextView () has two functions: Returns a pointer to the next view class and changes the value of the passed-in position type parameter in a reference-transfer manner. Obviously, in the test program, there is only one view class, so just call these two functions once to get Ctestview pointers as follows (you need to define a position structure variable to assist the operation):

c*view* PView;
POSITION pos=getfirstviewposition ();
Pview=getnextview (POS);

The pointer to the C*view class is then pview. After executing a few sentences, the variable pos=null, because there is no next view class, naturally there is no next view class POSITION. However, the few statements are too simple to have too much commonality and security features; When you want to return a pointer to a specified class in multiple views as in the previous statement, we need to traverse all the view classes until the specified class is found. When you determine whether a class pointer is an instance of a class, you can use the IsKindOf () member function to check the rows.

Such as:
Pview->iskindof (Runtime_class (C*view));
You can check whether the PView refers to the C*view class.
With the above basics, we can already get pointers to any class from the document class. For convenience, we use it as a member function of a document class, and it has a parameter that represents the pointer to which class to get. The implementation is as follows:
CVIEW*C*DOC::GETVIEWW (cruntimeclass* pClass)
{cview* PView;
POSITION pos=getfirstviewposition ();
while (Pos!=null) {
Pview=getnextview (POS);
if (!pview->iskindof (PClass))
break;}
if (!pview->iskindof (PClass)) {
AfxMessageBox ("Connt Locate the View.");
return NULL;}
return PView;}
This is determined by using the member function IsKindOf () of the view class two times, because there are three possible exits while loops:
1.pos is null, that is, the next view class does not exist for operation;
2.pView has met the requirements.
3.1 and 2 are satisfied. This is because the function of GetNextView () is to change the current view pointer to the position of a view and return the current view pointer, so POS is the position of the next view class for PView, and it is entirely possible that both Pos==null and PView meet the needs. When the desired view is the last view is the last view class as cited. Two judgments are therefore required.
Use this function in the following format (for example, to obtain the Ctestview pointer):
ctestview* ptestview= (ctestview*) GetView (Runtime_class (Ctestview));
Runtime_class is a macro that simply understands what it does: Convert the name of the class to CRuntimeClass as a pointer.
Coercion type conversions are also considered for security reasons, because pointer types from the same base class are mutually compatible. This type of coercion may not be necessary, but it can avoid some of the problems that may arise

******************************************************

The acquisition and application of pointers to various classes in MFC classes

Get CWinApp

Get CMainFrame

Get CChildFrame

Get CDocument

Get CView

In the CWinApp

AfxGetMainWnd ()

m_pMainWnd

AfxGetMainWnd ()->mdigetactive ()

AfxGetMainWnd ()->getactiveframe ()

Sdi:afxgetmainwnd ()->getactiveview ()->getdocument ()

Mdi:afxgetmainwnd ()->mdigetactive ()->getactiveview ()->getdocument ()

Sdi:afxgetmainwnd ()->getactiveview ()
Mdi:afxgetmainwnd ()->mdigetactive ()->getactiveview () in CMainFrame

AfxGetApp ()

Theapp

MDIGetActive ()

GetActiveFrame ()

Sdi:getactiveview ()->getdocument ()
Mdi:mdigetactive ()->getactiveview ()->getdocument () Sdi:getactiveview ()
Mdi:mdigetactive ()->getactiveview () in CChildFrame

AfxGetApp ()

Theapp

GetParentFrame ()

GetActiveView ()->getdocument () GetActiveView () in CDocument

AfxGetApp ()

Theapp

AfxGetMainWnd ()

AfxGetMainWnd ()->mdigetactive ()

AfxGetMainWnd ()->getactiveframe ()

POSITION pos = getfirstviewposition (); GetNextView (POS) in CView

AfxGetApp ()

Theapp

AfxGetMainWnd () GetParentFrame () GetDocument () in other classes

AfxGetApp ()

AfxGetMainWnd ()

AfxGetMainWnd ()->mdigetactive ()

AfxGetMainWnd ()->getactiveframe ()

Sdi:afxgetmainwnd ()->getactiveview ()->getdocument ()

Mdi:afxgetmainwnd ()->mdigetactive ()->getactiveview ()->getdocument ()

Sdi:afxgetmainwnd ()->getactiveview ()
Mdi:afxgetmainwnd ()->mdigetactive ()->getactiveview ()
The relationship between these several classes of MFC can easily be understood in the above-mentioned chaotic logic. The app is the app domain, and everything in the domain can be accessed through a global function. Mainframe is the main frame and can be accessed basically with global functions. Mainframe is a number of view and document (possibly unpaired) in several childframe,childframe, Childframe manages the interoperation of View,view and document. Therefore, the overall framework is out, generally in addition to direct application of the relationship can be accessed through the Mainframe-->activechildframe-->active view-->document this line

_______________________________

About the documents and views under MFC as well as the access between frames, these issues are already commonplace, but I think there is no detailed explanation, special

not for poor English people, I looked at some blog, summed up a bit! I hope the same people like me a little help!
One:
      1:   because for SDI programs, the main frame window is the document box (if this is not known, look at the composition of the single Document under MFC). &NBSP
          The following is about the .      of a single document       
          
         Example: How to get a pointer to a view class in the CMainFrame framework.
                     can get the frame pointer first and then call the GetActiveView function to point to the current Activity view.  

C **view * pView;
               pview= (c**view*) (( cframewnd*) AfxGetApp ()->m_pmainwnd)->getactiveview ();

Of course these may all know is so used, but the real m_pMainWnd and AfxGetApp () what meaning perhaps some people do not understand.
Everyone probably knows how to get the mainframe pointer (frame Class) in the app: the m_pMainWnd variable in CWINAPP is the pointer to CMainFrame.

So under other classes you can get m_pMainWnd first, you get the mainframe pointer. So to get a pointer to the view class, you must first get the CFrameWnd pointer m_pMainWnd, and then the GetActiveView under Call Framewnd point to the current Activity view.
The origin of m_pMainWnd:
Each MFC application has an object that CWinApp a derived class. This object corresponds to the main thread of the program. The CWinApp class has a CWnd * M_PMAINWND member variable. This member variable records the main window of the application.
When you create a new MFC application, the m_pMainWnd assignment statement appears in the InitInstance virtual function. The only exception is the single-document interface of the MFC application, you cannot see this code in the InitInstance function, because it has been hidden in the ProcessShellCommand in this function. So you can conclude that if you create your own window class, you will assign the object of this class to m_pMainWnd. And this member can only be used in the C**app class, so how to use the CWinApp class of the CWnd type of variables to get the main frame window pointer??
The AfxGetApp function is only possible because AfxGetApp () gets the object of the CWinApp class, and the AfxGetApp return value is the CWinApp object pointer, which is the object defined in the C**app.cpp generated by MFC (the pointer to the object Theapp).

Because you get Cwndapp member functions or member variables in the project you create, you must cast to a class in your own project to find member functions or variables.
Note: In a single document, the simplest way to get a visual pointer is to
((C**view *) Cframewnd::getactiveview ())

2: Of course, you can also get pointers to document classes in Framewnd:
cmydocument* PDoc;
Pdoc= (cmydocument*) ((cframewnd*) AfxGetApp ()->m_pmainwnd)->getactivedocument ();


3: From above can know: How to get mainframe pointer in view
CMainFrame *pmain= (CMainFrame *) AfxGetApp ()->m_pmainwnd;

Note: Get the main frame window class pointer from the view class: With the function: Cwnd::getparentframe () or AfxGetMainWnd () Also
can achieve the purpose. GetParentFrame () works by searching in the parent window chain until CFrameWnd or its derived class is found, and returns its pointer.

((CMainFrame *) cwnd::getparentframe ())
Or
((CMainFrame *) AfxGetMainWnd ())

Two:
Of course, for MDI programs, because the child window is the document box, you first get the active child frame window with GetActiveFrame (), and then get the active view and the document from that Subwindow:

Cmdichildwnd*pchild= (cmdichildwnd*) ((cframewnd*) AfxGetApp ()->m_pmainwnd)-

>getactiveframe ();

Get the active view:
cmyview* pview= (cmyview*) Pchild->getactiveview ();

Get the active document:
cmydocument* pdoc=pchild->getactivedocument ();


Note: You can also use this method to get a view pointer in multiple documents
Get the active child frame window
cmdichildwnd* pchild= (cmdichildwnd*) getactiveframe ();
Or:
cmdichildwnd* pchild=mdigetactive ();
Get the active view of the active sub-frame window
cmyview* pview= (cmyview*) Pchild->getactiveview ();


Three:
1. Obtaining a pointer to a document class from a view class
Before you need to reference the document class in the view class, use the following statement:
C*doc *pdoc= (c*doc*) getdocument ();
You can then use the Pdoc pointer to access the document class.
2. Pointer to a view class from a document class The CDocument class provides two functions for locating the view class:

GetFirstViewPosition () and GetNextView ()

Note: The parameters in GetNextView () are referenced in parentheses, so the post-execution value may change. GetFirstViewPosition () for

Returns the first view position (not a pointer to the view class, but a position type value), GetNextView () has two functions: Returns a pointer to the next view class and changes the value of the passed-in position type parameter in a reference-transfer manner. Obviously, in the test program, there is only one view class, so just call these two functions once to get Ctestview pointers as follows (you need to define a position structure variable to assist the operation):

c*view* PView;
POSITION pos=getfirstviewposition ();
Pview=getnextview (POS);

The pointer to the C*view class is then pview. After executing a few sentences, the variable pos=null, because there is no next view class, naturally there is no next view class POSITION. However, the few statements are too simple to have too much commonality and security features; When you want to return a pointer to a specified class in multiple views as in the previous statement, we need to traverse all the view classes until the specified class is found. When you determine whether a class pointer is an instance of a class, you can use the IsKindOf () member function to check the rows.

Such as:
Pview->iskindof (Runtime_class (C*view));
You can check whether the PView refers to the C*view class.
With the above basics, we can already get pointers to any class from the document class. For convenience, we use it as a member function of a document class, and it has a parameter that represents the pointer to which class to get. The implementation is as follows:
CVIEW*C*DOC::GETVIEWW (cruntimeclass* pClass)
{cview* PView;
POSITION pos=getfirstviewposition ();
while (Pos!=null) {
Pview=getnextview (POS);
if (!pview->iskindof (PClass))
break;}
if (!pview->iskindof (PClass)) {
AfxMessageBox ("Connt Locate the View.");
return NULL;}
return PView;}
This is determined by using the member function IsKindOf () of the view class two times, because there are three possible exits while loops:
1.pos is null, that is, the next view class does not exist for operation;
2.pView has met the requirements.
3.1 and 2 are satisfied. This is because the function of GetNextView () is to change the current view pointer to the position of a view and return the current view pointer, so POS is the position of the next view class for PView, and it is entirely possible that both Pos==null and PView meet the needs. When the desired view is the last view is the last view class as cited. Two judgments are therefore required.
Use this function in the following format (for example, to obtain the Ctestview pointer):
ctestview* ptestview= (ctestview*) GetView (Runtime_class (Ctestview));
Runtime_class is a macro that simply understands what it does: Convert the name of the class to CRuntimeClass as a pointer.
Coercion type conversions are also considered for security reasons, because pointer types from the same base class are mutually compatible. This type of coercion may not be necessary, but it can avoid some of the problems that may arise

Get CWinApp Get CMainFrame Get CChildFrame Get CDocument Get CVIEWW
In CWinApp

AfxGetMainWnd ()
m_pMainWnd

AfxGetMainWnd ()->mdigetactive ()
AfxGetMainWnd ()->getactiveframe ()

Sdi:afxgetmainwnd ()->getactiveview ()->getdocument ()
Mdi:afxgetmainwnd ()->mdigetactive ()->getactiveview ()->getdocument ()

Sdi:afxgetmainwnd ()->getactiveview ()
Mdi:afxgetmainwnd ()->mdigetactive ()->getactiveview ()

In CMainFrame

AfxGetApp ()

Theapp

MDIGetActive ()

GetActiveFrame ()
Sdi:getactiveview ()->getdocument ()
Mdi:mdigetactive ()->getactiveview ()->getdocument ()
Sdi:getactiveview ()
Mdi:mdigetactive ()->getactiveview ()
In CChildFrame

AfxGetApp ()

Theapp
GetParentFrame () GetActiveView ()->getdocument () GetActiveView ()
In CDocument

AfxGetApp ()

Theapp
AfxGetMainWnd ()

AfxGetMainWnd ()->mdigetactive ()

AfxGetMainWnd ()->getactiveframe ()

POSITION pos = getfirstviewposition (); GetNextView (POS)

CMainFrame *pmain = (CMainFrame *) AfxGetApp ()->m_pmainwnd;

CMyView *pview = (CMyView *) Pmain->getactiveview ();

In CVIEWW

AfxGetApp ()

Theapp
AfxGetMainWnd () GetParentFrame ()

GetDocument ()

Cyousdidoc *pdoc=getdocument ();

(RPM) Doc,view,mainfrmae,app of each pointer in MFC

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.