登录站点

用户名

密码

自定义类中怎么获得文档 视图指针

已有 963 次阅读  2010-10-19 22:39   标签视图  文档  指针  定义 

自定义类中怎么获得文档 视图指针

分享
标签: vc++ 2009-03-26 16:43


自定义类中怎么获得文档 视图指针

在类的开始部分要声明 文档 视图类(#include "YourDocument.h"  #include "YourView.h"  )

方法1:

CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();   
  CYourDocument*   pDoc   =   pFrame->GetActiveDocument();   
    
  (  CYourView*   pView   =   pFrame->GetActiveView(); )

这时编译会出错,cannot convert from 'class CDocument *' to "class CYourDocument* ";

需要类型强制转换:  CYourDocument*   pDoc   =(CYourDocument* )  pFrame->GetActiveDocument();

再运行不会出错。

方法2 :

获得当前活动试图的文档 
-if SDI:AfxGetMainWnd()->GetActiveView()->GetDocument() 
-if MDI:AfxGetMainWnd()->MDIGetActive()->GetActiveView()->GetDocument()

例子:

CMainFrame* pMain = (CMainFrame*)AfxGetMainWnd();
CCh1_1Doc* pDoc = (CCh1_1Doc*)(pMain->MDIGetActive()->GetActiveView()->GetDocument());

总结:

VC MFC SDI/MDI框架各部分指针获取方式

 

获得CWinApp

获得CMainFrame

获得CChildFrame

获得CDocument

获得CView

在CWinApp中

 

AfxGetMainWnd()

m_pMainWnd

AfxGetMainWnd()->MDIGetActive()

AfxGetMainWnd()->GetActiveFrame()

SDI:AfxGetMainWnd()->GetActiveView()->GetDocument()

MDI:AfxGetMainWnd()->MDIGetActive()->GetActiveView()->GetDocument()

SDI:AfxGetMainWnd()->GetActiveView()  
MDI:AfxGetMainWnd()->MDIGetActive()->GetActiveView() 
在CMainFrame中

AfxGetApp()

theApp

MDIGetActive()

GetActiveFrame()

SDI:GetActiveView()->GetDocument()  
MDI:MDIGetActive()->GetActiveView()->GetDocument()  
SDI:GetActiveView()  
MDI:MDIGetActive()->GetActiveView() 
在CChildFrame中

AfxGetApp()

theApp

GetParentFrame() 

 

GetActiveView()->GetDocument()   GetActiveView()
在CDocument中

AfxGetApp()

theApp

AfxGetMainWnd()  

AfxGetMainWnd()->MDIGetActive()

AfxGetMainWnd()->GetActiveFrame()

POSITION   pos   =   GetFirstViewPosition();GetNextView(pos)  
在CView中

AfxGetApp()

theApp

AfxGetMainWnd()   GetParentFrame()   GetDocument()
在其他类中

AfxGetApp()

AfxGetMainWnd()  

AfxGetMainWnd()->MDIGetActive()

AfxGetMainWnd()->GetActiveFrame() 

SDI:AfxGetMainWnd()->GetActiveView()->GetDocument()

MDI:AfxGetMainWnd()->MDIGetActive()->GetActiveView()->GetDocument()

SDI:AfxGetMainWnd()->GetActiveView()  
MDI:AfxGetMainWnd()->MDIGetActive()->GetActiveView() 
 
理一理MFC的这几个类的关系,可以很容易明白上面的这些乱七八糟的逻辑。
App是应用域,所有的域中的东西都可以通过全局函数访问到它。
MainFrame是主框架,也基本可以用全局函数访问到。
MainFrame下是若干个ChildFrame,ChildFrame中若干个View和Document(可能不成对),ChildFrame管理着View,View和Document进行互操作。
因此整体框架就出来了,一般除了直接应用的关系都可以通过MainFrame-->Active ChildFrame-->Active View-->Document这条线进行访问,这应该叫什么来自?万能方法吧^_^。

(自己的话:其实可以取到视图类指针,调用视图类中GetDocument()函数来得到文档指针)

上一篇: 用skinmagic给VC6开发的软件换肤 下一篇: #pragma pack(1) and #pragma pack(push,1)的含义

分享 举报