MFC架構類、文檔類、視圖類相互訪問的方法_文檔

來源:互聯網
上載者:User

1、擷取應用程式指標 
CMyApp* pApp=(CMyApp*)AfxGetApp();

2、擷取主架構指標 
CWinApp 中的公有成員變數 m_pMainWnd 就是主架構的指標 
CMainFrame* pMainFrame = (CMainFrame*)(AfxGetApp()->m_pMainWnd); 
或者 
CMainFrame* pMainFrame = (CMainFrame*)AfxGetMainWnd();

3、擷取菜單指標 
CMenu* pMenu = AfxGetMainWnd()->GetMenu();

4、擷取工具列、狀態列指標 
主架構中可以直接使用m_wndToolBar、m_wndStatusBar 
其他:

CToolBar* pToolBar = (CToolBar*)AfxGetMainWnd()->GetDescendantWindow(AFX_IDW_TOOLBAR); 
CStatusBar* pStatusBar = (CStatusBar*)AfxGetMainWnd()->GetDescendantWindow(AFX_IDW_STATUS_BAR);

5、擷取控制項指標 
先用 GetDlgItem() 再轉換,如:

CButton* pButton = (CButton*)GetDlgItem(IDC_MYBUTTON);

6、通過架構擷取文檔、視圖指標

SDI: 
CMainFrame* pMainFrame = (CMainFrame*)AfxGetMainWnd(); 
CYourDoc* pDoc = (CYourDoc*)pMainFrame->GetActiveDocument(); 
CYourView* pView = (CYourView*)pMainFrame->GetActiveView();

MDI: 
CMainFrame* pMainFrame = (CMainFrame*)AfxGetMainWnd(); 
CChildFrame* pChildFrame = (CChildFrame*)pMainFrame->GetActiveFrame(); 
CYourDoc* pDoc = (CYourDoc*)pChildFrame->GetActiveDocument(); 
CYourView* pView = (CYourView*)pChildFrame->GetActiveView();

7、文檔、視圖

從視圖擷取文檔指標: 
CYourDoc* pDoc = GetDocument();

從文檔擷取視圖指標: 
利用成員函數 GetFirstViewPosition() 和 GetNextView() 遍曆 
virtual POSITION GetFirstViewPosition() const; 
virtual CView* GetNextView(POSITION& rPosition) const;

SDI: 
CYourView* pView; 
POSITION pos = GetFirstViewPosition(); 
pView = GetNextView(pos);

MDI:

定義函數 
CView* CYourDoc::GetView(CRuntimeClass* pClass)

{

CView* pView; 
    POSITION pos=GetFirstViewPosition(); 
    while(pos!=NULL) 
    { 
        pView=GetNextView(pos); 
         if(!pView->IsKindOf(pClass)) 
             break; 
    } 
    if(!pView->IsKindOf(pClass)) 
    { 
        AfxMessageBox("Connt Locate the View."); 
       return NULL; 
    } 
    return pView; 
}

使用如下: 
CYourView* pView=(CYourView*)GetView(RUNTIME_CLASS(CYourView));

8、文檔模版、文檔

從文檔擷取文檔模版指標: 
CDocTemplate* GetDocTemplate() const;

從文檔模版擷取文檔指標: 
viaual POSITION GetFirstDocPosition( ) const = 0; 
visual CDocument* GetNextDoc(POSITION & rPos) const = 0;

9、擷取分割視圖中各個視圖的指標

主架構中定義:CSplitterWnd m_wndSplitter;

定義兩個View類:CView1、CView2

架構類中重載: 
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT, CCreateContext* pContext) 

    VERIFY(m_splitter.CreateStatic(this,2,1)); //分割成兩行一列 
    VERIFY(m_splitter.CreateView(0,0,RUNTIME_CLASS(CView1),CSize(100,100),pContext)); 
    VERIFY(m_splitter.CreateView(1,0,RUNTIME_CLASS(CView2),CSize(100,100),pContext)); 
    return TRUE; 
}

擷取分割視圖指標 
CView1* pView1 = (CView1*)m_wndSplitter.GetPane(0,0); 
CView2* pView2 = (CView2*)m_wndSplitter.GetPane(1,0);

10、通過滑鼠獲得子視窗指標

CWnd* ChildWindowFromPoint(POINT point) const; 
CWnd* ChildWindowFromPoint(POINT point,UINT nFlags) const; 
用於確定包含指定點的子視窗 
如果指定點在客戶區之外,函數返回NULL; 
如果指定點在客戶區內,但是不屬於任何一個子視窗,函數返回該CWnd的指標; 
如果有多個子視窗包含指定點,則返回第一個子視窗的指標。 
還要注意的是,該函數返回的是一個偽視窗指標,不能將它儲存起來供以後使用。 
對於第二個參數nFlags有幾個含義: 
CWP_ALL             file://不忽略任何子視窗 
CWP_SKIPNIVSIBLE    file://忽略不可見子視窗 
CWP_SKIPDISABLED    file://忽略禁止的子視窗 
CWP_SKIPRANSPARENT file://忽略透明子視窗

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.