First, the four-class
The order of execution is: App->doc->mainframe->view
The message response order is: View->doc->mainframe->app
1 //app to get the other three pointers2 voidCsdiapp::onapp ()3 {4 //App5 //Doc6CDocument *pdoc = ((CFrameWndEx *) m_pMainWnd)->getactivedocument ();//member variable CFrameWndEx m_pmainwnd7 //MainFrame8CFrameWndEx *pmain = (CFrameWndEx *) AfxGetMainWnd ();9 //ViewTenCView *pview = ((CFrameWndEx *) m_pMainWnd)GetActiveView (); One } A - //doc to get the other three pointers - Csdidoc::csdidoc ()//constructor the { - //App -CWinAppEx *papp = (CWinAppEx *) AfxGetApp (); - //Doc + //MainFrame - //Doc is created prior to mainframe + //View A //Doc is created prior to view at } - voidCsdidoc::ondoc () - { - //App - //Same constructor - //Doc in //MainFrame -CFrameWndEx *pmain = (CFrameWndEx *) AfxGetMainWnd (); to //View +CView *pview= (CView *) pmain->GetActiveView (); -POSITION pos =getfirstviewposition (); thePView =GetNextView (POS); * } $ Panax Notoginseng three additional pointers in//mainframe - cmainframe::cmainframe ()//constructor the { +Theapp.m_napplook = Theapp.getint (_t ("Applicationlook"), id_view_applook_vs_2005); A //App theCWinAppEx *papp = (CWinAppEx *) AfxGetApp (); + //Doc - //the currently active doc is not available in the constructor $ //MainFrame $ //View - //The view pointer cannot be obtained in the constructor because main is created before view. - } the voidCmainframe::onmain () - {Wuyi //App the //Same constructor - //Doc WuCDocument *pdoc = (CDocument *) getactivedocument (); - //MainFrame About //View $CView *pview = (CView *) GetActiveView (); - } - - //view to get the other three pointers A Csdiview::csdiview ()//constructor + { the //App -CWinAppEx *papp = (CWinAppEx *) AfxGetApp (); $ //Doc the /*cannot get doc pointer in view's constructor the getdocument (); actually returns m_pdocument the M_pdocument created in OnCreate ();*/ the //CDocument *pdoc = GetDocument (); - //MainFrame in //mainframe pointer not available in constructor the //CFrameWndEx *pmain = (CFrameWndEx *) papp->m_pmainwnd; the //View About } the voidCsdiview::onview () the { the //App + //Same constructor - //Doc theCDocument *pdoc =getdocument ();Bayi //MainFrame theCFrameWndEx *pmain = (CFrameWndEx *) AfxGetMainWnd (); the //View - } - the //Get pointers in Dlg theCdlg::cdlg (cwnd* pparent/*=null*/)//constructor the : CDialog (Cdlg::idd, pparent) the { - //App theCWinAppEx *papp = (CWinAppEx *) AfxGetApp (); the //Doc theCDocument *pdoc = ((CFrameWndEx *) AfxGetMainWnd ())getactivedocument ();94 //MainFrame theCFrameWndEx *pmain = (CFrameWndEx *) AfxGetMainWnd (); the //View theCView *pview = ((CFrameWndEx *) AfxGetMainWnd ())GetActiveView ();98}
Mutual acquisition of App,doc,mainframe,view pointers in MFC