在MFC中的CWinApp得到视图的指针,真难呀(适用于SDI和MDI)

王朝system·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

在MFC的SDI和MDI中,你要想从你的CXXXApp(当然是继承于CWinApp了)得到CXXXView(当然继承于CView了)视图指针可不是一件很容易的事情哟,还是很迂回的。下面将介绍如何得到,OK,让我们开始吧。

如果我们要得到视图指针,首先我们要得到指向文档的指针,如何得到文档类的指针,首先我们又要得到文档模板的指针(即CDocTemplate),如何得到文档模板的指针,很简单,就是通过CWinApp类即可得到。

具体过程如下:

1)得到文档模板的位置

2)根据模板的位置得到文档模板指针

3)根据文档模板得到文档的位置

4)根据文档的位置得到文档的指针

5)根据文档得到视图的位置

6)根据视图的位置得到视图的指针,这就是我们所要得到的。

大家看了,是不是觉得很痛苦呀。

请参见下图:

OK,下面给出具体实现代码

// Get the position of the first document template

POSITION pos = GetFirstDocTemplatePosition();

// Do we have a valid template position?

if (pos)

{

// Get a pointer to the document template

CDocTemplate* pDocTemp = GetNextDocTemplate(pos);

// Do we have a valid pointer?

if (pDocTemp)

{

// Get the position of the first document

POSITION dPos = pDocTemp->GetFirstDocPosition();

// Do we have a valid document position?

if (dPos)

{

// Get a pointer to the document

CTaskingDoc* pDocWnd =

(CTaskingDoc*)pDocTemp->GetNextDoc(dPos);

// Do we have a valid pointer?

if (pDocWnd)

{

// Get the position of the view

POSITION vPos = pDocWnd->GetFirstViewPosition();

// Do we have a valid view position?

if (vPos)

{

// Get a pointer to the view

CTaskingView* pView = (CTaskingView*)pDocWnd->GetNextView(vPos);

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航