怎么遍历MFC打开的多个文档?

怎么遍历MFC打开的多个文档?

    需要使用打开的多个文档的数据,在哪可以遍历到呢?处理的例程应该放在哪呢?App?Frame?Doc?View...?

    嗯,我再想想

    在class CMultiDocTemplate定义中看到了:
                  CPtrList m_docList;          // open documents of this type

估计就是这家伙了,还有两个函数应该是用来取数据的

virtual POSITION GetFirstDocPosition() const;
virtual CDocument* GetNextDoc(POSITION& rPos) const;

然后得想办法拿到CMultiDocTemplate的handler,嘿嘿,这家伙就在CDocument类中:

CDocTemplate* GetDocTemplate() const;

这样看来在Doc处理该消息是比较自然的了。

嗯,先试试去

 

 

PS:在网上搜索了一下,有一篇说的比较接近,拿来主义,原创不详!内容如下:

在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);
}
注意:有些if 语句要改为while  循环。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值