获得父窗口指针:获得MFC窗口其它类指针的方法

获得 CwinApp:
-
CMainFrame,CChildFrame,CDocument,CView 中直接调用 AfxGetApp() 或用 theApp
-
在其它类中只能用 AfxGetApp()

获得 CMainFrame:
-
CMinApp 中用 AfxGetMainWnd() 或者 m_pMainWnd
-
CChildFrame 中可用 GetParentFrame()
-
在其它类中用 AfxGetMainWnd()

获得 CChildFrame:
-
CView 中用 GetParentFrame()
-
CMainFrame 中用 MDIGetActive() GetActiveFrame()
-
在其它类中用 AfxGetMainWnd()->MDIGetActive() AfxGetMainWnd()->GetActiveFrame()

获得 CDocument:
-
CView 中用 GetDocument()
-
CChildFrame 中用 GetActiveView()->GetDocument()
-
CMainFrame 中用
-if SDI:GetActiveView()->GetDocument()
-if MDI:MDIGetActive()->GetActiveView()->GetDocument()
-
在其它类中
-if SDI:AfxGetMainWnd()->GetActiveView()->GetDocument()
-if MDI:AfxGetMainWnd()->MDIGetActive()->GetActiveView()->GetDocument()

获得 CView:
-
CDocument POSITION pos = GetFirstViewPosition();GetNextView(pos)
-
CChildFrame GetActiveView()
-
CMainFrame
-if SDI:GetActiveView()
-if MDI:MDIGetActive()->GetActiveView()
-
在其它类中
-if SDI:AfxGetMainWnd()->GetActiveView()
-if MDI:AfxGetMainWnd()->MDIGetActive()->GetActiveView()

注意事项:
不过要注意在 doc 中要取得 view 的指针 C*View 要注意类 C*View 声明的问题 ,
因为默认情况下 ,mfc *View.h 中已经包含了 *Doc.h, 如果在 *Doc.h 中包含
*View.h,
就会引起嵌套包含问题 , 这样要在 *Doc.h 中加入 class C*View;
而在 *Doc.cpp 中加入 #include "*View.h"

//
其实完全可以在 CYourApp 中添加各种视或文档的指针,在那些视或文档初
始化的时候将指针传给 CYourApp 中的对应变量,这样以后不管在哪用上面
指针只需 (CYourApp*)AfxGetApp() 取其属性变量即可,明了而且清楚更是
方便我一直专门操作的说:)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值