2011年10月27日 星期四

[MFC] SDI, MDI如何取得Doc,View,MainFrmae,App之指標

開發MFC Document/View架構的程式時,首先遇到的問題就是如何取得Doucment, View, MainFrame與App的指標。
以下是一些常用的指標取得方法:

  1. 取得應用程式類別CWinApp指標
    MFC全域函數 AfxGetApp() 取得
     
  2. 取得MainFrame指標
    • 使用成員變數m_pMainWnd
      (
      m_pMainWndCWinThread的成員變數, CWinApp繼承自CWinThread, 故可使用m_pMainWnd取得主視窗之指標)
      CMainFrame* pMainWnd = (CMainFrame*) AfxGetApp()->m_pMainWnd;
    • 透過MFC全域函數 AfxGetMainWnd() 取得
      CMainFrame* pMainWnd = (CMainFrame*) AfxGetMainWnd();
  3. View中取得Document指標
    用View的成員函數
    GetDocument()取得
    CMyAppDoc*
    pDoc=GetDocument();

  4. 取得現行的View指標先取得MainFrame指標,再由MainFrameGetActiveView()函數取得View的指標
    CMainFrame* pMainWnd = (CMainFrame*) AfxGetApp()->m_pMainWnd; 

    CMyAppView* pView = ( CMyAppView*) pMainWnd->GetActiveView();


  5. 取得現行的Document指標
    先取得
    FrameWnd指標,再由FrameWndGetActiveDocument()函數取得Document的指標
    CFrameWnd* pMainFrm = (CFrameWnd*) AfxGetApp()->m_pMainWnd;
    CDocument* pCurDoc = (CFrameWnd*) pMainFrm ->GetActiveDocument();


沒有留言:

張貼留言