于获得MFC窗口其它类指针的方法



访问应用程序的其它类 

获得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()取其属性变量即可,明了而且清楚更是 
方便我一直专门操作的说:) 

////////////////////////////////////////////////////////////////// 
我先抛块砖,有玉的砸过来! 
在何时何地,你都可以通过以下方法精确的得到任何一个对象(Application,DocTemplate,Document,View,Frame) 
1。通过AfxGetApp()得到当前的App对象; 
2。通过AfxGetMainWnd()得到主窗口; 
3。通过CMDIFrameWnd::GetActiveFrame得到当前活动窗口; 
4。通过GetNextWindow()遍例所有的子窗口;(如果要得到你想要的子窗口,可以通过特定的成员变量来标志); 
5。通过CWinApp::GetFirstDocTemplatePostion()以及CWinApp::GetNextDocTemplate()的组合应用来遍历所有的DocTemplate对象,并且用CDocTemplate::GetDocString()来判断当前得到的文档莫板对象是哪个。 
6。通过CDocTemplate::GetFirstDocPosition()以及CDocTemplate的GetNextDoc()组合来遍历所有的该模板的文档对象,并用CDocument::GetDocTemplate()来得到文档模板,用CDocment::GetTitle()   或者GetPathName()来判断当前的文档是哪个。 
7。通过CDocuemt的GetFirstViewPositon()以及GetNextView()来遍历视图对象,一般通过访问View的成员变量来区别各个视图;通过CView::GetDocument()来得到文档对象; 
8。Frame-> View:   通过GetActiveView方法; 
9。Frame-> Doc:通过GetActiveDocument(); 
10。View-> Frame:GetParentFrame(); 
11。View-> Doc:GetDocuemt()//前面已经说了。 
12。Doc-> View:前面说了; 
13。Doc-> Frame:不知道有没有很直接的方法。 

原文地址:https://www.cnblogs.com/lebronjames/p/1951344.html