获得
CwinApp:
- 在 CMainFrame,CChildFrame,CDocument,CView 中直接调用 AfxGetApp() 或用 theApp
- 在其它类中只能用 AfxGetApp()
获得 CMainFrame:
- 在 CMinApp 中用 AfxGetMainWnd() 或者 m_pMainWnd
- 在 CChildFrame 中可用 GetParentFrame()
- 在其它类中用 AfxGetMainWnd()
获得 CChildFrame:
- 在 CView 中用 GetParentFrame()
- 在 CMainFrame 中用 MDIGetActive() 或 GetActiveFrame()
- 在其它类中用 AfxGetMainWnd()->MDIGetActive() 或 AfxGetMainWnd()->GetActiveFrame()
获得 CDocument:
- 在 CView 中用 GetDocument()
- 在 CChildFrame 中用 GetActiveView()->GetDocument()
- 在 CMainFrame 中用
-if SDI:GetActiveView()->GetDocument()
-if MDI:MDIGetActive()->GetActiveView()->GetDocument()
- 在其它类中
-if SDI:AfxGetMainWnd()->GetActiveView()->GetDocument()
-if MDI:AfxGetMainWnd()->MDIGetActive()->GetActiveView()->GetDocument()
获得 CView:
- 在 CDocument 中 POSITION pos = GetFirstViewPosition();GetNextView(pos)
- 在 CChildFrame 中 GetActiveView()
- 在 CMainFrame 中
-if SDI:GetActiveView()
-if MDI:MDIGetActive()->GetActiveView()
- 在其它类中
-if SDI:AfxGetMainWnd()->GetActiveView()
-if MDI:AfxGetMainWnd()->MDIGetActive()->GetActiveView()
注意事项:
① 不过要注意在 doc 中要取得 view 的指针 C*View 要注意类 C*View 声明的问题 ,
因为默认情况下 ,mfc 在 *View.h 中已经包含了 *Doc.h, 如果在 *Doc.h 中包含
*View.h, 就会引起嵌套包含问题 , 这样要在 *Doc.h 中加入 class C*View;
而在 *Doc.cpp 中加入 #include "*View.h"
//
② 其实完全可以在 CYourApp 中添加各种视或文档的指针,在那些视或文档初
始化的时候将指针传给 CYourApp 中的对应变量,这样以后不管在哪用上面
指针只需 (CYourApp*)AfxGetApp() 取其属性变量即可,明了而且清楚更是
方便我一直专门操作的说:)
- 在 CMainFrame,CChildFrame,CDocument,CView 中直接调用 AfxGetApp() 或用 theApp
- 在其它类中只能用 AfxGetApp()
获得 CMainFrame:
- 在 CMinApp 中用 AfxGetMainWnd() 或者 m_pMainWnd
- 在 CChildFrame 中可用 GetParentFrame()
- 在其它类中用 AfxGetMainWnd()
获得 CChildFrame:
- 在 CView 中用 GetParentFrame()
- 在 CMainFrame 中用 MDIGetActive() 或 GetActiveFrame()
- 在其它类中用 AfxGetMainWnd()->MDIGetActive() 或 AfxGetMainWnd()->GetActiveFrame()
获得 CDocument:
- 在 CView 中用 GetDocument()
- 在 CChildFrame 中用 GetActiveView()->GetDocument()
- 在 CMainFrame 中用
-if SDI:GetActiveView()->GetDocument()
-if MDI:MDIGetActive()->GetActiveView()->GetDocument()
- 在其它类中
-if SDI:AfxGetMainWnd()->GetActiveView()->GetDocument()
-if MDI:AfxGetMainWnd()->MDIGetActive()->GetActiveView()->GetDocument()
获得 CView:
- 在 CDocument 中 POSITION pos = GetFirstViewPosition();GetNextView(pos)
- 在 CChildFrame 中 GetActiveView()
- 在 CMainFrame 中
-if SDI:GetActiveView()
-if MDI:MDIGetActive()->GetActiveView()
- 在其它类中
-if SDI:AfxGetMainWnd()->GetActiveView()
-if MDI:AfxGetMainWnd()->MDIGetActive()->GetActiveView()
注意事项:
① 不过要注意在 doc 中要取得 view 的指针 C*View 要注意类 C*View 声明的问题 ,
因为默认情况下 ,mfc 在 *View.h 中已经包含了 *Doc.h, 如果在 *Doc.h 中包含
*View.h, 就会引起嵌套包含问题 , 这样要在 *Doc.h 中加入 class C*View;
而在 *Doc.cpp 中加入 #include "*View.h"
//
② 其实完全可以在 CYourApp 中添加各种视或文档的指针,在那些视或文档初
始化的时候将指针传给 CYourApp 中的对应变量,这样以后不管在哪用上面
指针只需 (CYourApp*)AfxGetApp() 取其属性变量即可,明了而且清楚更是
方便我一直专门操作的说:)