您的位置:首页 > 其它

MFC中获得各个类的句柄的总结

2011-03-29 23:28 351 查看
1) 在View中获得Doc指针:

CYouSDIDoc *pDoc=GetDocument();一个视只能有一个文档。

2) 在App中获得MainFrame指针:

CWinApp 中的 m_pMainWnd变量就是MainFrame的指针,也可以:CMainFrame *pMain =(CMainFrame *)AfxGetMainWnd();

3) 在View中获得MainFrame指针

CMainFrame *pMain=(CmaimFrame *)AfxGetApp()->m_pMainWnd;

4) 获得View(已建立)指针
CMainFrame *pMain=(CmaimFrame *)AfxGetApp()->m_pMainWnd;
CyouView *pView=(CyouView *)pMain->GetActiveView();

5) 获得当前文档指针
CDocument * pCurrentDoc =(CFrameWnd *)m_pMainWnd->GetActiveDocument();

6) 获得状态栏与工具栏指针
CStatusBar * pStatusBar=(CStatusBar *)AfxGetMainWnd()->GetDescendantWindow(AFX_IDW_STATUS_BAR);
CToolBar * pToolBar=(CtoolBar *)AfxGetMainWnd()->GetDescendantWindow(AFX_IDW_TOOLBAR);

8) 在Mainframe获得菜单指针
CMenu *pMenu=m_pMainWnd->GetMenu();

9) 在任何类中获得应用程序类
AfxGetInstanceHandle 得到句柄,AfxGetApp 得到指针

10)在自己的类和“应用程序类”中获得“文档类”的句柄
SDI: AfxGetMainWnd() -> GetActiveView() -> GetDocument() 得到指针
MDI: AfxGetMainWnd() -> MDIGetActive() -> GetActiveView() -> GetDocument() 得到指针

11)在自己的类和“应用程序类”中获得“视图类”句柄?
SDI: GetActiveView 得到指针
MDI: MDIGetActive() -> GetActiveView() 从 CMainFrame 得到指针,GetActiveView 从 CChildFrame 得到指针

12)在“框架类”中获得“文档类”句柄
SDI: GetActiveView() -> GetDocument() 得到指针
MDI: MDIGetActive() -> GetActiveView() -> GetDocument() 从 CMainFrame 得到指针,GetActiveView() -> GetDocument() 从 CChildFrame 得到指针

13)在“文档类”中获得“视图类”句柄?
GetView(),调用 GetFirstViewPosition 和 GetNextView 函数得到指针

注意:

在提取到各个句柄之后,因为初次提取的都是标准类句柄,所以,在使用时要注意将标准句柄转换成自己的类的句柄。
如:
AfxGetApp();//得到的是WinApp类的句柄,
所以操作前记得转换成自己定义的类的句柄。
如:
((CMyApp*)AfxGetApp())->XXXX();//这的xxxx()就已经是你定义的类成员函数了。

7) 如果框架中加入工具栏和状态栏变量还可以这样
(CMainFrame *)GetParent()->m_wndToolBar;
(CMainFrame *)GetParent()->m_wndStatusBar;

获得CWinApp
获得CMainFrame
获得CChildFrame
获得CDocument
获得CView

在CWinApp中

AfxGetMainWnd()

m_pMainWnd
AfxGetMainWnd()->MDIGetActive()

AfxGetMainWnd()->GetActiveFrame()
SDI:AfxGetMainWnd()->GetActiveView()->GetDocument()

MDI:AfxGetMainWnd()->MDIGetActive()->GetActiveView()->GetDocument()
SDI:AfxGetMainWnd()->GetActiveView()
MDI:AfxGetMainWnd()->MDIGetActive()->GetActiveView()

在CMainFrame中
AfxGetApp()

theApp

MDIGetActive()

GetActiveFrame()
SDI:GetActiveView()->GetDocument()
MDI:MDIGetActive()->GetActiveView()->GetDocument()
SDI:GetActiveView()
MDI:MDIGetActive()->GetActiveView()

在CChildFrame中
AfxGetApp()

theApp
GetParentFrame()

GetActiveView()->GetDocument()
GetActiveView()

在CDocument中
AfxGetApp()

theApp
AfxGetMainWnd()
AfxGetMainWnd()->MDIGetActive()

AfxGetMainWnd()->GetActiveFrame()

POSITION pos = GetFirstViewPosition();GetNextView(pos)

在CView中
AfxGetApp()

theApp
AfxGetMainWnd()
GetParentFrame()
GetDocument()

在其他类中
AfxGetApp()
AfxGetMainWnd()
AfxGetMainWnd()->MDIGetActive()

AfxGetMainWnd()->GetActiveFrame()
SDI:AfxGetMainWnd()->GetActiveView()->GetDocument()

MDI:AfxGetMainWnd()->MDIGetActive()->GetActiveView()->GetDocument()
SDI:AfxGetMainWnd()->GetActiveView()
MDI:AfxGetMainWnd()->MDIGetActive()->GetActiveView
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: