• MFC中App,Doc,MainFrame,View各指针的互相获取


    首先说明这四个类的

    执行顺序是:App->Doc->MainFrame->View

    消息响应顺序是:View->Doc->MainFrame->App

     1 // App中获取其它三项指针
     2 void CSDIApp::OnApp()
     3 {
     4     // App
     5     // Doc
     6     CDocument *pDoc = ((CFrameWndEx *)m_pMainWnd)->GetActiveDocument();//成员变量CFrameWndEx m_pMainWnd
     7     // MainFrame
     8     CFrameWndEx *pMain = (CFrameWndEx *)AfxGetMainWnd();
     9     // View
    10     CView *pView = ((CFrameWndEx *)m_pMainWnd)->GetActiveView();
    11 }
    12 
    13 // Doc中获取其它三项指针
    14 CSDIDoc::CSDIDoc()//构造函数
    15 {
    16     // App
    17     CWinAppEx *pApp = (CWinAppEx *)AfxGetApp();
    18     // Doc
    19     // MainFrame
    20     // Doc的创建先于MainFrame
    21     // View
    22     // Doc的创建先于View
    23 }
    24 void CSDIDoc::OnDoc()
    25 {
    26     // App
    27     // 同构造函数
    28     // Doc
    29     // MainFrame
    30     CFrameWndEx *pMain = (CFrameWndEx *)AfxGetMainWnd();
    31     // View
    32     CView *pView= (CView *)pMain->GetActiveView();     
    33     POSITION pos = GetFirstViewPosition();       
    34     pView = GetNextView(pos); 
    35 }
    36     
    37 // MainFrame中获取其它三项指针
    38 CMainFrame::CMainFrame()//构造函数
    39 {
    40     theApp.m_nAppLook = theApp.GetInt(_T("ApplicationLook"), ID_VIEW_APPLOOK_VS_2005);
    41     // App
    42     CWinAppEx *pApp = (CWinAppEx *)AfxGetApp();
    43     // Doc
    44     // 构造函数里无法得到当前激活的Doc
    45     // MainFrame
    46     // View
    47     // 构造函数里无法得到View指针,因为Main先于View创建。
    48 }
    49 void CMainFrame::OnMain()
    50 {
    51     // App
    52     // 同构造函数
    53     // Doc
    54     CDocument *pDoc = (CDocument *)GetActiveDocument();
    55     // MainFrame
    56     // View
    57     CView *pView = (CView *)GetActiveView();
    58 }
    59 
    60 // View中获取其它三项指针
    61 CSDIView::CSDIView()//构造函数
    62 {
    63     // App
    64     CWinAppEx *pApp = (CWinAppEx *)AfxGetApp();
    65     // Doc
    66     /* 无法在View的构造函数里得到Doc指针
    67        GetDocument();实际上是返回m_pDocument
    68        m_pDocument在OnCreate();里创建        */
    69     //CDocument *pDoc = GetDocument();
    70     // MainFrame
    71     // 构造函数里无法得到MainFrame指针
    72     // CFrameWndEx *pMain = (CFrameWndEx *)pApp->m_pMainWnd;
    73     // View
    74 }
    75 void CSDIView::OnView()
    76 {
    77     // App
    78     // 同构造函数
    79     // Doc
    80     CDocument *pDoc = GetDocument();
    81     // MainFrame
    82     CFrameWndEx *pMain = (CFrameWndEx *)AfxGetMainWnd();
    83     // View
    84 }
    85 
    86 // Dlg中获取指针
    87 CDlg::CDlg(CWnd* pParent /*=NULL*/)//构造函数
    88     : CDialog(CDlg::IDD, pParent)
    89 {
    90     // App
    91     CWinAppEx *pApp = (CWinAppEx *)AfxGetApp();
    92     // Doc
    93     CDocument *pDoc = ((CFrameWndEx *)AfxGetMainWnd())->GetActiveDocument();
    94     // MainFrame
    95     CFrameWndEx *pMain = (CFrameWndEx *)AfxGetMainWnd();
    96     // View
    97     CView *pView = ((CFrameWndEx *)AfxGetMainWnd())->GetActiveView();
    98 }
  • 相关阅读:
    利用FUSE编写自定义的文件系统
    ubuntu16.04 overlay 不支持redirect_dir开关
    ip rule实现源IP路由,实现一个主机多IP(或多网段)同时通(外部看是完全两个独立IP)
    段地址机制以及段地址转换触发segmentation falt
    sshfs+overlayfs实现一个共享只读资源被多个主机挂载成可写目录
    解析prototxt文件的python库 prototxt-parser(使用parsy自定义文件格式解析)
    arris1750 pandorabox安装bandwidthd之后带宽监控(nlbwmon)报资源不足
    工作中的C++问题汇总
    CMake相关代码片段
    编写合格的C代码(1):通过编译选项将特定警告视为错误
  • 原文地址:https://www.cnblogs.com/luzhiyuan/p/3936283.html
Copyright © 2020-2023  润新知