<转>文档视图指针互获

来源:互联网 发布:自动发卡程序源码 编辑:程序博客网 时间:2024/05/20 11:21
 

 

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;
CyouView *pView=(CyouView *)pMain->GetActiveView();

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()获得。

10) 从文档类取得视图类的指针
我是从http://download.cqcnc.com/soft/program/article/vc/vc405.html学到的,
从文档获得视图类指针目的一般为了控制同一文档的多个视图的定位问题,我的体会
特别是文字处理CEditView当产生多个视图类时,这个功能是非常需要的。
CDocument类提供了两个函数用于视图类的定位:
GetFirstViewPosition()和GetNextView()
virtual POSITION GetFirstViewPosition() const;
virtual CView* GetNextView(POSITION& rPosition) const;


从该对象如何访问其他对象全局函数调用全局函数AfxGetApp可以得到CWinApp应用类指针应用AfxGetApp()->m_pMainWnd为框架窗口指针;用CWinApp::GetFirstDocTemplatePostion、CWinApp::GetNextDocTemplate来遍历所有文档模板文档调用CDocument::GetFirstViewPosition,CDocument::GetNextView来遍历所有和文档关联的视图;调用CDocument:: GetDocTemplate 获取文档模板指针文档模板调用CDocTemplate::GetFirstDocPosition、CDocTemplate::GetNextDoc来遍历所有对应文档视图调用CView::GetDocument 得到对应的文档指针;调用CView::GetParentFrame 获取框架窗口文档框架窗口调用CFrameWnd::GetActiveView 获取当前得到当前活动视图指针;调用CFrameWnd::GetActiveDocument 获取附加到当前视图的文档指针MDI 框架窗口调用CMDIFrameWnd::MDIGetActive 获取当前活动的MDI子窗口(CMDIChildWnd)
  
综合应用上表中的函数,写一段代码,它完成遍历文档模板、文档和视图的功能:

CMyApp *pMyApp = (CMyApp*)AfxGetApp(); //得到应用程序指针
POSITION p = pMyApp->GetFirstDocTemplatePosition();//得到第1个文档模板
while (p != NULL) //遍历文档模板
{
 CDocTemplate *pDocTemplate = pMyApp->GetNextDocTemplate(p);
 POSITION p1 = pDocTemplate->GetFirstDocPosition();//得到文档模板对应的第1个文档
 while (p1 != NULL) //遍历文档模板对应的文档
 {
  CDocument *pDocument = pDocTemplate->GetNextDoc(p1);
  POSITION p2 = pDocument->GetFirstViewPosition(); //得到文档对应的第1个视图
  while (p2 != NULL) //遍历文档对应的视图
  {
   CView *pView = pDocument->GetNextView(p2);
  }
 }
}

原创粉丝点击