CWinApp * AfxGetApp()是一个全局函数,在任何地方都可以获得C***App类对象的指针.
//在C***App类中:
获得CMainFrame类对象的指针:
CMainFrame* pMain=(CMainFrame*)CWinThread::m_pMainWnd;
获得C***View类对象的指针:(假设只有一个视图,须通过CMainFrame)
C***View *pView=(C***View *)((CMainFrame*)m_pMainWnd)->CFrameWnd::GetActiveView();
获得C***Doc类对象的指针(须通过CMainFrame)
C***Doc *pDoc=(C***Doc *)((CMainFrame*)m_pMainWnd)->CFrameWnd::GetActiveDocument();
//在CMainFrame类中:
获得CMainFrame类对象的指针:
CMainFrame* pMain=(CMainFrame*)CWnd::GetActiveWindow();
获得C***View类对象的指针:
C***View *pView=(C***View *)CFrameWnd::GetActiveView();
获得C***Doc类对象的指针
C***Doc *pDoc=(C***Doc *)CFrameWnd::GetActiveDocument();
从框架类CMainFrame获取文档类指针:
CMyTestApp *pApp = (CMyTestApp*)AfxGetApp();
//在C***Doc类中:
获得CMainFrame类对象的指针:
CMainFrame* pMain=(CMainFrame*)AfxGetMainWnd();
CMainFrame* pMain=(CMainFrame*)AfxGetApp()->m_pMainWnd;
获得C***View类对象的指针:
(假设只有一个视图,须通过CMainFrame)
C***View *pView=(C***View *)((CMainFrame*)AfxGetApp()->m_pMainWnd)->CFrameWnd::GetActiveView():
(假设有两个以上视图,以找寻C***View为例)
POSITION pos=CDocument::GetFirstViewPosition();
while(pos != NULL)
{
CView *pView=CDocument::GetNextView(pos);
if(pView->GetRuntimeClass()==RUNTIME_CLASS(C**View))
{
......
}
}
//在C***View类中:
获得CMainFrame类对象的指针:
CMainFrame* pMain=(CMainFrame*)AfxGetMainWnd();
CMainFrame* pMain=(CMainFrame*)CWnd::GetParentFrame();
CMainFrame* pMain=(CMainFrame*)AfxGetApp()->m_pMainWnd;
获得C***Doc类对象的指针
C***Doc *pDoc= GetDocument();
注意:
1 在CFrameWnd::ActivateFrame函数之后可以取得CMainFrame *
2 在CView::OnCreate函数执行完毕后,可以查找C***View *
3 在CView::OnCreate函数执行完毕后,可以取得C***Doc *
//在C***App类中:
获得CMainFrame类对象的指针:
CMainFrame* pMain=(CMainFrame*)CWinThread::m_pMainWnd;
获得C***View类对象的指针:(假设只有一个视图,须通过CMainFrame)
C***View *pView=(C***View *)((CMainFrame*)m_pMainWnd)->CFrameWnd::GetActiveView();
获得C***Doc类对象的指针(须通过CMainFrame)
C***Doc *pDoc=(C***Doc *)((CMainFrame*)m_pMainWnd)->CFrameWnd::GetActiveDocument();
//在CMainFrame类中:
获得CMainFrame类对象的指针:
CMainFrame* pMain=(CMainFrame*)CWnd::GetActiveWindow();
获得C***View类对象的指针:
C***View *pView=(C***View *)CFrameWnd::GetActiveView();
获得C***Doc类对象的指针
C***Doc *pDoc=(C***Doc *)CFrameWnd::GetActiveDocument();
从框架类CMainFrame获取文档类指针:
CMyTestApp *pApp = (CMyTestApp*)AfxGetApp();
POSITION posDT =pApp->GetFirstDocTemplatePosit
CDocTemplate *pDocTemplate =pApp->GetNextDocTemplate( posDT );
POSITION posDoc =pDocTemplate->GetFirstDocPosition();
CMyTestDoc *pDocument = (CMyTestDoc*)pDocTemplate->GetNextDoc( posDoc );
//在C***Doc类中:
获得CMainFrame类对象的指针:
CMainFrame* pMain=(CMainFrame*)AfxGetMainWnd();
CMainFrame* pMain=(CMainFrame*)AfxGetApp()->m_pMainWnd;
获得C***View类对象的指针:
(假设只有一个视图,须通过CMainFrame)
C***View *pView=(C***View *)((CMainFrame*)AfxGetApp()->m_pMainWnd)->CFrameWnd::GetActiveView():
(假设有两个以上视图,以找寻C***View为例)
POSITION pos=CDocument::GetFirstViewPosition();
while(pos != NULL)
{
CView *pView=CDocument::GetNextView(pos);
if(pView->GetRuntimeClass()==RUNTIME_CLASS(C**View))
{
......
}
}
//在C***View类中:
获得CMainFrame类对象的指针:
CMainFrame* pMain=(CMainFrame*)AfxGetMainWnd();
CMainFrame* pMain=(CMainFrame*)CWnd::GetParentFrame();
CMainFrame* pMain=(CMainFrame*)AfxGetApp()->m_pMainWnd;
获得C***Doc类对象的指针
C***Doc *pDoc= GetDocument();
注意:
1 在CFrameWnd::ActivateFrame函数之后可以取得CMainFrame *
2 在CView::OnCreate函数执行完毕后,可以查找C***View *
3 在CView::OnCreate函数执行完毕后,可以取得C***Doc *
本文介绍在MFC框架中如何从不同类中获取应用程序、主窗口、视图及文档对象的指针,包括使用全局函数AfxGetApp()、AfxGetMainWnd()等方法。
2494

被折叠的 条评论
为什么被折叠?



