获得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)
- 通过AfxGetApp()得到当前的App对象;
- 通过AfxGetMainWnd()得到主窗口;
- 通过CMDIFrameWnd::GetActiveFrame得到当前活动窗口;
- 通过GetNextWindow()遍例所有的子窗口;(如果要得到你想要的子窗口,可以通过特定的成员变量来标志);
- 通过CWinApp::GetFirstDocTemplatePostion()以及CWinApp::GetNextDocTemplate()的组合应用来遍历所有的DocTemplate对象,并且用CDocTemplate::GetDocString()来判断当前得到的文档莫板对象是哪个。
- 通过CDocTemplate::GetFirstDocPosition()以及CDocTemplate的GetNextDoc()组合来遍历所有的该模板的文档对象,并用CDocument::GetDocTemplate()来得到文档模板,用CDocment::GetTitle() 或者GetPathName()来判断当前的文档是哪个。
- 通过CDocuemt的GetFirstViewPositon()以及GetNextView()来遍历视图对象,一般通过访问View的成员变量来区别各个视图;通过CView::GetDocument()来得到文档对象;
- Frame->View: 通过GetActiveView方法;
- Frame->Doc:通过GetActiveDocument();
- View->Frame:GetParentFrame();
- View->Doc:GetDocuemt()//前面已经说了。
- Doc->View:前面说了;
- Doc->Frame:不知道有没有很直接的方法。
MFC应用程序中指针的使用:
- 在View中获得Doc指针
- 在App中获得MainFrame指针
- 在View中获得MainFrame指针
- 获得View(已建立)指针
- 获得当前文档指针
- 获得状态栏与工具栏指针
- 获得状态栏与工具栏变量
- 在Mainframe获得菜单指针
- 在任何类中获得应用程序类
- 从文档类取得视图类的指针(1)
- 在App中获得文档模板指针
- 从文档模板获得文档类指针
- 在文档类中获得文档模板指针
- 从文档类取得视图类的指针(2)
- 从一个视图类取得另一视图类的指针
VC中编程对于刚刚开始学习的同学,最大的障碍和问题就是消息机制和指针获取与操作。其实这些内容基本上是每本VC学习工具书上必讲的内容,而且通过MSDN很多问题都能解决。一般我们使用的框架是VC提供的Wizard生成的MFC App Wizard(exe)框架,无论是多文档还是单文档,都存在指针获取和操作问题。下面这节内容主要是一般的框架,然后再讲多线程中的指针使用。使用到的类需要包含响应的头文件。首先一般获得本类(视,文档,对话框都支持)实例指针this,用this的目的,主要可以通过类中的函数向其他类或者函数中发指针,以便于在非本类中操作和使用本类中的功能。
- 在View中获得Doc指针 CYouSDIDoc *pDoc=GetDocument();一个视只能有一个文档。
- 在App中获得MainFrame指针
- CWinApp 中的 m_pMainWnd变量就是MainFrame的指针,也可以: CMainFrame *pMain =(CMainFrame *)AfxGetMainWnd();
- 在View中获得MainFrame指针 CMainFrame *pMain=(CmaimFrame *)AfxGetApp()->m_pMainWnd;
- 获得View(已建立)指针 CMainFrame *pMain=(CmaimFrame *)AfxGetApp()->m_pMainWnd;
CyouView *pView=(CyouView *)pMain->GetActiveView(); - 获得当前文档指针 CDocument * pCurrentDoc =(CFrameWnd *)m_pMainWnd->GetActiveDocument();
- 获得状态栏与工具栏指针 CStatusBar * pStatusBar=(CStatusBar *)AfxGetMainWnd()->GetDescendantWindow(AFX_IDW_STATUS_BAR);