获得指针(文档、视图、框架)

本文介绍了在MFC框架下如何从不同类中获取应用程序、视图和文档对象的指针。提供了多种场景下的具体实现方式,包括CWinApp、CMainFrame、C***Doc及C***View等类中的获取方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

获得指针(文档、视图、框架)

==========================================

 

 

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(); //在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 *

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值