MFC中指针的获取

1)  在 View 中获得 Doc 指针

   CYouSDIDoc   *pDoc=GetDocument();一个视只能有一个文档。

2)    在App 中获得MainFrame 指针   
  CWinApp    中的   m_pMainWnd 变量就是 MainFrame 的指针也可以:   CMainFrame  
*pMain   =(CMainFrame   *)AfxGetMainWnd();

3)      在 View 中获得 MainFrame 指针

   CMainFrame *pMain=(CmaimFrame *)AfxGetApp()->m_pMainWnd;

4)      获得 View (已建立)指针

CMainFrame *pMain=(CmaimFrame *)AfxGetApp()->m_pMainWnd; 

5)获得当前文档指针

   CDocument   *   pCurrentDoc   =(CFrameWnd *)m_pMainWnd->GetActiveDocument(); 

6)      获得状态栏与工具栏指针

   CStatusBar   *   pStatusBar=(CStatusBar   *)AfxGetMainWnd()->GetDescendantWindow(AFX_IDW_STATUS_BAR);   
   CToolBar   *   pToolBar=(CtoolBar   *)AfxGetMainWnd()->GetDescendantWindow(AFX_IDW_TOOLBAR);

7)    如果框架中加入工具栏和状态栏变量还可以这样     
  (CMainFrame   *)GetParent()->m_wndToolBar;   
  (CMainFrame   *)GetParent()->m_wndStatusBar;

8)    在Mainframe 获得菜单指针   CMenu   *pMenu=m_pMainWnd->GetMenu();    
9)    在任何类中获得应用程序类用 MFC 全局函数 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();
  3810。View->Frame:GetParentFrame();
11。View->Doc:GetDocuemt()//前面已经说了。
12。Doc->View:前面说了;
13。Doc->Frame:不知道有没有很直接的方法。

posted @ 2013-04-05 10:15  qq921201008  阅读(206)  评论(0编辑  收藏  举报