首先说明这四个类的
执行顺序是:App->Doc->MainFrame->View
消息响应顺序是:View->Doc->MainFrame->App
1 // App中获取其它三项指针 2 void CSDIApp::OnApp() 3 { 4 // App 5 // Doc 6 CDocument *pDoc = ((CFrameWndEx *)m_pMainWnd)->GetActiveDocument();//成员变量CFrameWndEx m_pMainWnd 7 // MainFrame 8 CFrameWndEx *pMain = (CFrameWndEx *)AfxGetMainWnd(); 9 // View 10 CView *pView = ((CFrameWndEx *)m_pMainWnd)->GetActiveView(); 11 } 12 13 // Doc中获取其它三项指针 14 CSDIDoc::CSDIDoc()//构造函数 15 { 16 // App 17 CWinAppEx *pApp = (CWinAppEx *)AfxGetApp(); 18 // Doc 19 // MainFrame 20 // Doc的创建先于MainFrame 21 // View 22 // Doc的创建先于View 23 } 24 void CSDIDoc::OnDoc() 25 { 26 // App 27 // 同构造函数 28 // Doc 29 // MainFrame 30 CFrameWndEx *pMain = (CFrameWndEx *)AfxGetMainWnd(); 31 // View 32 CView *pView= (CView *)pMain->GetActiveView(); 33 POSITION pos = GetFirstViewPosition(); 34 pView = GetNextView(pos); 35 } 36 37 // MainFrame中获取其它三项指针 38 CMainFrame::CMainFrame()//构造函数 39 { 40 theApp.m_nAppLook = theApp.GetInt(_T("ApplicationLook"), ID_VIEW_APPLOOK_VS_2005); 41 // App 42 CWinAppEx *pApp = (CWinAppEx *)AfxGetApp(); 43 // Doc 44 // 构造函数里无法得到当前激活的Doc 45 // MainFrame 46 // View 47 // 构造函数里无法得到View指针,因为Main先于View创建。 48 } 49 void CMainFrame::OnMain() 50 { 51 // App 52 // 同构造函数 53 // Doc 54 CDocument *pDoc = (CDocument *)GetActiveDocument(); 55 // MainFrame 56 // View 57 CView *pView = (CView *)GetActiveView(); 58 } 59 60 // View中获取其它三项指针 61 CSDIView::CSDIView()//构造函数 62 { 63 // App 64 CWinAppEx *pApp = (CWinAppEx *)AfxGetApp(); 65 // Doc 66 /* 无法在View的构造函数里得到Doc指针 67 GetDocument();实际上是返回m_pDocument 68 m_pDocument在OnCreate();里创建 */ 69 //CDocument *pDoc = GetDocument(); 70 // MainFrame 71 // 构造函数里无法得到MainFrame指针 72 // CFrameWndEx *pMain = (CFrameWndEx *)pApp->m_pMainWnd; 73 // View 74 } 75 void CSDIView::OnView() 76 { 77 // App 78 // 同构造函数 79 // Doc 80 CDocument *pDoc = GetDocument(); 81 // MainFrame 82 CFrameWndEx *pMain = (CFrameWndEx *)AfxGetMainWnd(); 83 // View 84 } 85 86 // Dlg中获取指针 87 CDlg::CDlg(CWnd* pParent /*=NULL*/)//构造函数 88 : CDialog(CDlg::IDD, pParent) 89 { 90 // App 91 CWinAppEx *pApp = (CWinAppEx *)AfxGetApp(); 92 // Doc 93 CDocument *pDoc = ((CFrameWndEx *)AfxGetMainWnd())->GetActiveDocument(); 94 // MainFrame 95 CFrameWndEx *pMain = (CFrameWndEx *)AfxGetMainWnd(); 96 // View 97 CView *pView = ((CFrameWndEx *)AfxGetMainWnd())->GetActiveView(); 98 }
时间: 2024-10-25 22:08:29