zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    LeetCode 75. Sort Colors(按颜色进行排序)
    LeetCode 451. Sort Characters By Frequency(按照字符出现次数对字符串排序)
    LeetCode 347. Top K Frequent Elements(出现频率最多的 k 个元素)
    LeetCode 215. Kth Largest Element in an Array(数组求第k大)
    CF #629 Div.3 E(LCA)F
    系统函数
    CASE表达式
    循环得出数据库中所有的 DB_ID,DB_NAME
    数据库的编码问题
    检验临时表是否存在
  • 原文地址:https://www.cnblogs.com/luzhiyuan/p/3936283.html
Copyright © 2011-2022 走看看