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

  • 相关阅读:
    Repeater嵌套Repeater的结构
    解决还原数据库 出现单用户
    常见的一些C#开源框架或者开源项目
    vue 实现动态路由
    c#使用Split分割换行符
    SQL Server 时间戳与时间格式互相转换
    值与枚举的转化
    编程之美,让美国人科技高速发展,浅谈C语言带给美国的变化
    SQL CE数据库搭建和操作
    C# 与API
  • 原文地址:https://www.cnblogs.com/confach/p/112352.html
Copyright © 2011-2022 走看看