首页 技术 正文
技术 2022年11月11日
0 收藏 359 点赞 3,804 浏览 2663 个字

From: http://hi.baidu.com/wxnxs/item/156a68f5b3b4ed18e3e3bd03 

MFC中获取App,MainFrame,Doc和View类等指针的方法

1  获取应用程序类(App)指针

在任何类中都可用MFC全局函数AfxGetApp()获得

2 获取框架类(MainFrame)指针

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

2) 在View中获得MainFrame指针

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

3 获取各种视图类(View)指针

1)(在App,MainFrame,Doc中)获取当前已建立View

CMainFrame *pMain = (CMainFrame *)AfxGetApp()->m_pMainWnd;
CMyView *pView = (CMyView *)pMain->GetActiveView();

2)从文档类(Doc)取得视图类(View)的指针—-多视

CDocument类提供了两个函数用于视图类的定位:GetFirstViewPosition()和GetNextView()

virtual POSITION GetFirstViewPosition() const;
virtual  CView* GetNextView(POSITION& rPosition) const;

例:CTestView* pTestView;    POSITION pos=GetFirstViewPosition();     pTestView=GetNextView(pos);

为了方便,我们将其作为一个文档类的成员函数,它有一个参数,表示要获得哪个类的指针。实现如下:

CView* CTestDoc::GetView(CRuntimeClass* pClass)

{

CView* pView;

POSITION pos=GetFirstViewPosition();

while(pos!=NULL){

pView=GetNextView(pos);

if(!pView->IsKindOf(pClass))

break;

}

if(!pView->IsKindOf(pClass)){

AfxMessageBox(“Cannot Locate the View!”);

return NULL;

}

return pView;

}

3)从一个视图类取得另一视图类的指针

用文档类作中转,先得到文档类的指针,再用文档类的视图定位函数取得另一个视图类。同样,可以实现成一个函数:
(假设要从CTestAView中取得指向其它视图类的指针)

CView* CTestAView::GetView(CRuntimeClass* pClass)

{

CTestDoc* pDoc=(CTestDoc*)GetDocument();

CView* pView;

POSITION pos=pDoc->GetFirstViewPosition();

while(pos!=NULL){

pView=pDoc->GetNextView(pos);

if(!pView->IsKindOf(pClass))

break;

}

if(!pView->IsKindOf(pClass)){

AfxMessageBox(“Cannot Locate the View!”);

return NULL;

}

return pView;

}

4)获取分割视图中各个视图的指针
CSplitterWnd  m_wndSplitter;
m_wndSplitter.CreateStatic(this, 1,  2);//分割成一行两列
m_wndSplitter.CreateView(0, 0, RUNTIME_CLASS(CLeftPaneView),  CSize(10, 10), pContext);
m_wndSplitter.CreateView(0, 1,  RUNTIME_CLASS(CRightPaneFrame), CSize(0, 0),  pContext);
//获取左边视图的两种方法
CLeftPaneView* pLeftPaneView     =  (CLeftPaneView*)    m_wndSplitter.GetPane(0,0);
//上一句可以用下句代替:
//CLeftPaneView* pLeftPaneView   = (CLeftPaneView *)GetActiveView();
//获取右边视图

pLeftPaneView->m_pRightPaneFrame = (CRightPaneFrame*)  m_wndSplitter.GetPane(0,1);

4 获取各种文档类(Doc)和文档模板类的指针

1)获得当前文档指针       CDocument * pCurrentDoc = (CFrameWnd  *)m_pMainWnd->GetActiveDocument();

2)从文档模板获得文档类指针

一个文档模板可以有多个文档,每个文档模板都保留并维护了一个所有对应文档的指针列表。

用CDocTemplate::GetFirstDocPosition函数获得与文档模板相关的文档集合中第一个文档的位置,并用POSITION值作为CDocTemplate::GetNextDoc的参数来重复遍历与模板相关的文档列表。函数原形为:

virtual POSITION GetFirstDocPosition( ) const = 0;

virtual CDocument *GetNextDoc(POSITION & rPos) const = 0;

如果列表为空,则rPos被置为NULL.

3)在文档类中获得文档模板指针

在文档中可以调用CDocument::GetDocTemplate获得指向该文档模板的指针。

函数原形如下: CDocTemplate * GetDocTemplate ( )  const; 
如果该文档不属于文档模板管理,则返回值为NULL。

http://blog.csdn.net/puncha/article/details/17394885

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,291