在实际的应用中,经常需要在多个类之间交换数据,调用另一个类中的函数,框架提供一系列的函数用于多个类之间的交互
1.单文档应用程序的类与类之间的访问
(1)在所有类中获取App指针:
CWin *pApp=AfxGetApp();
CXXXApp *pMyApp=(CXXXApp*)pApp;
(2)所有类中获取CMainFrame类指针:
CMainFrame *pFrame=(CMainFrame*)AfxGetMainWnd();
CMainFrame *pFrame=(CMainFrame*)(AfxGetApp()->m_pMainWnd);
(3)在框架类(CMainFrame)中访问视图(View)
CXXXView *pView=(CXXXView*)GetActiveView();
(4)在框架类(CMainFrame)中访问文档(Doc)
CXXXDoc *pDOC=(CXXXDoc*)GetActiveDocument();
(5)在视图类(View)中访问文档(Doc)
CXXXDoc *pDoc=GetDocumen();
(6)在文档类(Doc)中访问关联的所有视图(View)
POSITION pos=GetFirstViewPosition();
while(pos ! = NULL)
{
CXXXView *pView=(CXXXView*)GetNextView(pos);
}
2.多文档应用程序类与类之间的交互
(1)在所有类中获取App类指针
CWinApp *pApp=AfxGetApp();
CMDIXXXApp * pMyApp=(CMDIXXXApp*)pApp;
(2)所有类中获取CMainFrame类指针
CMainFrame *pFrame=(CMainFrame*)AfxGetMainWnd();
CMainFrame *pFrame=(CMainFrame*)(AfxGetApp()->m_pMainWnd);
(3)获取当前活动的MDI子窗口指针
CMainFrame *pMainFrame=(CMainFrame*)AfxGetMainWnd();
CChildFrame *pActiveChild=(CChildFrame*)pMainFrame->MDIGetActive();
(4)获取活动视图、文档的指针
CMainFrame *pMainFrame=(CMainFrame*)AfxGetMainWnd();
CChildFrame *pActiveChild=(CChildFrame*)pMainFrame->MDIGetActive();
CMDIXXXView *pActiveView=(CMDIXXXView*)pActiveChild->GetActiveView();
CMDIXXXDoc *pActiveDoc=(CMDIXXXDoc*)pActiveChild->GetActiveDocument();