我在一个函数里放下列语句,为什么不能成功???
CMainFrame* pFrame=(CMainFrame*)AfxGetMainWnd();
CMYView* pView=(CMYView*)pFrame->GetActiveView();
CMYDoc* pDoc=(CMYDoc*)pView->GetDocument();
我想在文档类外引用文档对象中的成员,怎么实现???

解决方案 »

  1.   

    若以上代码在框架类中则
    需要在MainFrm.cpp中#include "XXView.h"
    #include "XXDoc.h"
    在XXview.h中#include "XXDoc.h"1.若在视类中得到文档类成员
    CMYDoc* pDoc=(CMYDoc*)pView->GetDocument();
    pDoc->yourdata;
    2.若不再视类中,在框架类中先获得视类指针
    CMainFrame* pFrame=(CMainFrame*)AfxGetMainWnd();
    CMYView* pView=(CMYView*)pFrame->GetActiveView();
    CMYDoc* pDoc=(CMYDoc*)pView->GetDocument();
      

  2.   

    单文档框架下:
    CMainFrame * pMainWnd = (CMainFrame *)(AfxGetApp()->m_pMainWnd);
    CMyDoc * pDoc = pMainWnd->GetActiveDocument();多文档框架下:
    CMainFrame* pFrame=(CMainFrame*)(AfxGetApp()->m_pMainWnd);
    CMYView* pView=(CMYView*)pFrame->GetActiveView();
    CMYDoc* pDoc=(CMYDoc*)pView->GetDocument();
      

  3.   

    首先我是在一个全局函数中放的上述代码的,可我都试过了!
    好象在Frame-doc-view框架之外的地方是不能用上述代码引用框架内的内容的吧?!
    最后我是通过全局变量解决了该问题的!!!
    在下感谢各位高手提点!!!
      

  4.   

    我非常同意  khwidesea(阔海) 的观点!
    单文档框架下:
    CMainFrame * pMainWnd = (CMainFrame *)(AfxGetApp()->m_pMainWnd);
    CMyDoc * pDoc = pMainWnd->GetActiveDocument();多文档框架下:
    CMainFrame* pFrame=(CMainFrame*)(AfxGetApp()->m_pMainWnd);
    CMYView* pView=(CMYView*)pFrame->GetActiveView();
    CMYDoc* pDoc=(CMYDoc*)pView->GetDocument();