• MFC中获取App,MainFrame,Doc和View类等指针的方法


     

    MFC中获取App,MainFrame,Doc和View类等指针的方法

    1  获取应用程序类(App)指针

    在任何类中都可用MFC全局函数AfxGetApp()获得

    2 获取框架类(MainFrame)指针

    1)在App中获得MainFrame指针
    CWinApp 中的  m_pMainWnd变量就是MainFrame的指针
    也可以: CMainFrame *pMain =(CMainFrame  *)AfxGetMainWnd();

    2) 在View中获得MainFrame指针 

    CMainFrame *pMain=(CMainFrame *)AfxGetApp()->m_pMainWnd;

    3 获取各种视图类(View)指针

    1)(在App,MainFrame,Doc中)获取当前已建立View

    CMainFrame *pMain = (CMainFrame *)AfxGetApp()->m_pMainWnd;
    CMyView *pView = (CMyView *)pMain->GetActiveView();

    2)从文档类(Doc)取得视图类(View)的指针----多视

    CDocument类提供了两个函数用于视图类的定位:GetFirstViewPosition()和GetNextView() 

    virtual POSITION GetFirstViewPosition() const;
    virtual  CView* GetNextView(POSITION& rPosition) const;

    例:CTestView* pTestView;    POSITION pos=GetFirstViewPosition();     pTestView=GetNextView(pos);

    为了方便,我们将其作为一个文档类的成员函数,它有一个参数,表示要获得哪个类的指针。实现如下: 

    CView* CTestDoc::GetView(CRuntimeClass* pClass)

    {

    CView* pView;

    POSITION pos=GetFirstViewPosition();

    while(pos!=NULL){

    pView=GetNextView(pos);

    if(!pView->IsKindOf(pClass))

    break;

    }

    if(!pView->IsKindOf(pClass)){

    AfxMessageBox("Cannot Locate the View!");

    return NULL;

    }

    return pView;

    }

    3)从一个视图类取得另一视图类的指针

    用文档类作中转,先得到文档类的指针,再用文档类的视图定位函数取得另一个视图类。同样,可以实现成一个函数:
    (假设要从CTestAView中取得指向其它视图类的指针)

    CView* CTestAView::GetView(CRuntimeClass* pClass)

    {

    CTestDoc* pDoc=(CTestDoc*)GetDocument();

    CView* pView;

    POSITION pos=pDoc->GetFirstViewPosition();

    while(pos!=NULL){

    pView=pDoc->GetNextView(pos);

    if(!pView->IsKindOf(pClass))

    break;

    }

    if(!pView->IsKindOf(pClass)){

    AfxMessageBox("Cannot Locate the View!");

    return NULL;

    }

    return pView;

    }

    4)获取分割视图中各个视图的指针
    CSplitterWnd  m_wndSplitter;
    m_wndSplitter.CreateStatic(this, 1,  2);//分割成一行两列
    m_wndSplitter.CreateView(0, 0, RUNTIME_CLASS(CLeftPaneView),  CSize(10, 10), pContext);
    m_wndSplitter.CreateView(0, 1,  RUNTIME_CLASS(CRightPaneFrame), CSize(0, 0),  pContext);
    //获取左边视图的两种方法
    CLeftPaneView* pLeftPaneView     =  (CLeftPaneView*)    m_wndSplitter.GetPane(0,0);
    //上一句可以用下句代替:
    //CLeftPaneView* pLeftPaneView   = (CLeftPaneView *)GetActiveView();
    //获取右边视图

    pLeftPaneView->m_pRightPaneFrame = (CRightPaneFrame*)  m_wndSplitter.GetPane(0,1);

    4 获取各种文档类(Doc)和文档模板类的指针

    1)获得当前文档指针       CDocument * pCurrentDoc = (CFrameWnd  *)m_pMainWnd->GetActiveDocument();

    2)从文档模板获得文档类指针

    一个文档模板可以有多个文档,每个文档模板都保留并维护了一个所有对应文档的指针列表。 

    用CDocTemplate::GetFirstDocPosition函数获得与文档模板相关的文档集合中第一个文档的位置,并用POSITION值作为CDocTemplate::GetNextDoc的参数来重复遍历与模板相关的文档列表。函数原形为: 

    virtual POSITION GetFirstDocPosition( ) const = 0; 

    virtual CDocument *GetNextDoc(POSITION & rPos) const = 0;  

    如果列表为空,则rPos被置为NULL.

    3)在文档类中获得文档模板指针

    在文档中可以调用CDocument::GetDocTemplate获得指向该文档模板的指针。

    函数原形如下: CDocTemplate * GetDocTemplate ( )  const; 
    如果该文档不属于文档模板管理,则返回值为NULL。

    http://blog.csdn.net/puncha/article/details/17394885
  • 相关阅读:
    FLEX监视浏览器关闭事件
    [FMS]FMS流媒体服务器onStatus介绍说明
    JS调用水晶报表打印翻页按钮事件
    js之获取窗口大小和位置信息
    Window对象简介
    Js中的window.parent ,window.top,window.self 详解
    flex4+fms3.5+cs4开发实时音视频直播及点播详解
    VS2010与水晶报表V13的打包集成小结
    最完美解决方案:js弹出窗口关闭当前页面,而不弹出提示框
    使用C#将HTML文本转换为普通文本,去掉所有的Html标记(转)
  • 原文地址:https://www.cnblogs.com/findumars/p/7545826.html
Copyright © 2020-2023  润新知