CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  VC/MFC >  基础类

如何在SDI应用程序的对话框类中得到文档类或者视图类的指针?

楼主guangde(光光)2002-10-23 11:11:15 在 VC/MFC / 基础类 提问

如何在SDI应用程序的对话框类中得到文档类或者视图类的指针?(每个问题20分)谢谢! 问题点数:40、回复次数:8Top

1 楼rushing(勇敢的心)回复于 2002-10-23 11:17:23 得分 8

需要自定义,在析构函数中赋值或者另外建立函数赋值。  
  例如,GetDocument,GetViewTop

2 楼nanjianhui(nan)回复于 2002-10-23 11:17:54 得分 8

GetActiveView();     //get   view   pointer  
  Top

3 楼guangde(光光)回复于 2002-10-23 11:24:26 得分 0

能否详细点?我用了下面的代码:((CMainFrame*)(AfxGetApp()->m_pMainWnd))->GetActiveView,但得到的不是和文档类相连的视图类的指针。Top

4 楼sunshinefl(NeverMind)回复于 2002-10-23 11:42:20 得分 8

看看这个   相信你会豁然开朗的   呵呵  
  How   to   Get   Current   CDocument   or   CView   from   Anywhere  
  From   Microsoft   Knowledge   Base   (Article   ID:   Q108587)    
  One   of   the   cases   where   you   might   need   a   pointer   to   the   currently   active   view   or   document   is   in   a   modal   or   modeless   dialog   box.   Generally,   a   dialog   box   should   be   created   by   the   view   class,   because   the   view   is   what   deals   with   the   application's   user   interface.    
  Because   the   view   class   is   creating   the   dialog   box,   it   can   pass   a   pointer   to   itself,   or   the   active   document   [obtained   with   the   GetActiveDocument()   function]   to   the   dialog   box.   This   could   be   done   through   the   dialog   box's   constructor   or   some   other   member   function.   For   modal   dialog   boxes,   the   view   could   also   put   data   from   the   dialog   box   into   the   document   when   DoModal()   returns.    
  These   methods   are   generally   preferable   to   relying   on   generic   functions   to   return   pointers   to   the   currently   active   view   or   document.    
  To   allow   you   to   get   a   pointer   to   the   currently   active   document   from   anywhere   in   the   program,   add   a   static   member   function   to   your   CDocument   derived   class   as   follows:    
  Edit   the   document's   header   file   as   follows   to   add   a   static   member   function,   GetDoc():    
        //   Document   header   file  
   
        class   CMyDoc   :   public   CDocument  
        {  
              ...  
              public:  
              static   CMyDoc   *   GetDoc();  
              ...  
        };  
   
   
   
  For   a   single   document   interface   (SDI)   application,   add   the   following   code   to   your   SDI   document's   implementation   file   for   CMyDoc::GetDoc():    
        //   SDI   document   implementation   file  
   
        CMyDoc   *   CMyDoc::GetDoc()  
        {  
              CFrameWnd   *   pFrame   =   (CFrameWnd   *)(AfxGetApp()->m_pMainWnd);  
              return   (CMyDoc   *)   pFrame->GetActiveDocument();  
        }  
   
   
  For   an   SDI   application,   add   the   following   code   to   your   SDI   view's   implementation   file   for   CMyView::GetView():    
        //   View   implementation   file  
   
        CMyView   *   CMyView::GetView()  
        {  
              CFrameWnd   *   pFrame   =   (CFrameWnd   *)(AfxGetApp()->m_pMainWnd);  
   
              CView   *   pView   =   pFrame->GetActiveView();  
   
              if   (   !pView   )  
                    return   NULL;  
   
              //   Fail   if   view   is   of   wrong   kind  
              //   (this   could   occur   with   splitter   windows,   or   additional  
              //   views   on   a   single   document  
   
              if   (   !   pView->IsKindOf(   RUNTIME_CLASS(CMyView)   )   )  
                    return   NULL;  
   
              return   (CMyView   *)   pView;  
        }  
  Top

5 楼guangde(光光)回复于 2002-10-23 16:59:34 得分 0

试了,好像不行!没人能说详细点吗?再加100分!!!!Top

6 楼sunshinefl(NeverMind)回复于 2002-10-23 18:19:54 得分 8

应该可以   我用过,先在Document   header   file中添加静态成员函数  
  static   CMyDoc   *   GetDoc();  
   
  在实现文件中添加  
  CMyDoc   *   CMyDoc::GetDoc()  
        {  
              CFrameWnd   *   pFrame   =   (CFrameWnd   *)(AfxGetApp()->m_pMainWnd);  
              return   (CMyDoc   *)   pFrame->GetActiveDocument();  
        }  
   
  然后需要Document指针时,调用CMyDoc   *pDoc=   CMyDoc::GetDoc();就可以了!  
  视图指针同理!也是先定义函数,在写实现Top

7 楼andy_lau(天行键,君子当自强不息!)回复于 2002-10-23 18:35:15 得分 8

GetActiveView();Top

8 楼guangde(光光)回复于 2002-10-24 09:17:51 得分 0

已经解决了,不过没有用各位的办法,不过还是要谢谢家!给分!Top

相关问题

  • 怎么样在SDI中弹出的对话框里取得一个子视图的指针?
  • 如何取得分割对话框中的视图指针
  • 如何在对话框中获取视图指针?
  • 如何在对话框中获得当前视图或文档的指针?
  • *********视图与对话框***********
  • 在视图中动态生成一个非模态对话框后怎么来得到指向该对话框的指针呢?大家来讨论好吗?
  • ******在框架窗口中如何获得,视图类的指针,,,在弹出对话框中怎么获得视图类指针****
  • 如何在自建的派生类对话框中,得到视图-文档结构中的文档指针?
  • 在单文档的对话框的子视图中如何获取主文档的指针
  • 怎么在SDI中的弹出对话框中取得文档的指针?急!!

关键词

  • 视图
  • 指针
  • 文档
  • 函数
  • view
  • active
  • document
  • cmydoc
  • 类
  • getdoc

得分解答快速导航

  • 帖主:guangde
  • rushing
  • nanjianhui
  • sunshinefl
  • sunshinefl
  • andy_lau

相关链接

  • Visual C++类图书
  • Visual C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo