zoukankan      html  css  js  c++  java
  • 在MFC中的CWinApp得到视图的指针,真难呀(适用于SDI和MDI)

    在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);
    }
    }
    }

  • 相关阅读:
    电路原理图分析
    GPIO学习——用户空间操作
    在Android上运行Java和C程序
    Android命令行工具学习总结
    Android蓝牙学习笔记
    33 把数组排成最小的数
    233 Number of Digit One
    32 从1到n整数中1出现的次数
    31 连续子数组的最大和
    《大型网站技术架构》学习笔记
  • 原文地址:https://www.cnblogs.com/confach/p/112352.html
Copyright © 2011-2022 走看看