获得窗口和其他类指针的方法

本文介绍了在MFC框架下获取各种类指针的方法,包括CWinApp、CMainFrame、CChildFrame、CDocument、CView等关键组件之间的相互访问途径。通过这些方法可以更灵活地进行窗口管理和数据交互。
获得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()

更加直接的方法是通过全局App指针
1)通过AfxGetApp()得到当前的App对象;
2)通过AfxGetMainWnd()得到主窗口;
3)通过CMDIFrameWnd::GetActiveFrame得到当前活动窗口;
4)通过GetNextWindow()遍例所有的子窗口;(如果要得到你想要的子窗口,可以通过特定的成员变量来标志);
5) 通过CWinApp::GetFirstDocTemplatePostion()以及CWinApp::GetNextDocTemplate()的组 合应用来遍历所有的DocTemplate对象,并且用CDocTemplate::GetDocString()来判断当前得到的文档莫板对象是哪个。
6) 通过CDocTemplate::GetFirstDocPosition()以及CDocTemplate的GetNextDoc()组合来遍历所有的 该模板的文档对象,并用CDocument::GetDocTemplate()来得到文档模板,用CDocment::GetTitle() 或者 GetPathName()来判断当前的文档是哪个。
7)通过CDocuemt的GetFirstViewPositon()以及GetNextView()来遍历视图对象,一般通过访问View的成员变量来区别各个视图;通过CView::GetDocument()来得到文档对象;
8)Frame->View: 通过GetActiveView方法;
9)Frame->Doc:通过GetActiveDocument();
10)View->Frame:GetParentFrame();
11)View->Doc:GetDocuemt()//前面已经说了。
12)Doc->View:前面说了;
13)Doc->Frame:不知道有没有很直接的方法。 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值