1、获取应用程序指针
  CMyApp* pApp=(CMyApp*)AfxGetApp();

2、获取主框架指针
  CWinApp 中的公有成员变量 m_pMainWnd 就是主框架的指针
  CMainFrame* pMainFrame = (CMainFrame*)(AfxGetApp()->m_pMainWnd);
  或者
  CMainFrame* pMainFrame = (CMainFrame*)AfxGetMainWnd();

3、获取菜单指针
  CMenu* pMenu = AfxGetMainWnd()->GetMenu();

4、获取工具栏、状态栏指针
  主框架中可以直接使用m_wndToolBar、m_wndStatusBar
  其他:
  CToolBar* pToolBar = (CToolBar*)AfxGetMainWnd()->GetDescendantWindow(AFX_IDW_TOOLBAR);
  CStatusBar* pStatusBar = (CStatusBar*)AfxGetMainWnd()->GetDescendantWindow(AFX_IDW_STATUS_BAR);

5、获取控件指针
  先用 GetDlgItem() 再转换,如:
  CButton* pButton = (CButton*)GetDlgItem(IDC_MYBUTTON);

6、获取文档、视图指针

SDI:

1 CMainFrame* pMainFrame = (CMainFrame*)AfxGetMainWnd();
2 CYourDoc* pDoc = (CYourDoc*)pMainFrame->GetActiveDocument();
3 CYourView* pView = (CYourView*)pMainFrame->GetActiveView();

 

MDI:

1 CMainFrame* pMainFrame = (CMainFrame*)AfxGetMainWnd();
2 CChildFrame* pChildFrame = (CChildFrame*)pMainFrame->GetActiveFrame();
3 CYourDoc* pDoc = (CYourDoc*)pChildFrame->GetActiveDocument();
4 CYourView* pView = (CYourView*)pChildFrame->GetActiveView();

 

7、文档、视图

从视图获取文档指针:
CYourDoc* pDoc = GetDocument();

从文档获取视图指针:
利用成员函数 GetFirstViewPosition() 和 GetNextView() 遍历

 

 1 virtual POSITION GetFirstViewPosition() const;
 2 virtual CView* GetNextView(POSITION& rPosition) const;

SDI:

1 CYourView* pView;
2 POSITION pos = GetFirstViewPosition();
3 pView = GetNextView(pos);

MDI:

 1 CView* CYourDoc::GetView(CRuntimeClass* pClass)
 2 {
 3    CView* pView;
 4    POSITION pos=GetFirstViewPosition();
 5    while(pos!=NULL)
 6   {
 7         pView=GetNextView(pos);
 8          if(!pView->IsKindOf(pClass))
 9             break;
10    }
11    if(!pView->IsKindOf(pClass))
12    {
13        AfxMessageBox("Connt Locate the View.");
14        return NULL;
15    }
16     return pView;
17 }

使用如下:

1 CYourView* pView=(CYourView*)GetView(RUNTIME_CLASS(CYourView));

 

8、文档模版、文档

从文档获取文档模版指针:
CDocTemplate* GetDocTemplate() const;

从文档模版获取文档指针:

1 viaual POSITION GetFirstDocPosition( ) const = 0; 
2 visual CDocument* GetNextDoc(POSITION & rPos) const = 0;

 

9、获取分割视图中各个视图的指针

主框架中定义:CSplitterWnd m_wndSplitter;

定义两个View类:CView1、CView2

框架类中重载:

1 BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT, CCreateContext* pContext)
2 {
3     VERIFY(m_splitter.CreateStatic(this,2,1)); //分割成两行一列
4     VERIFY(m_splitter.CreateView(0,0,RUNTIME_CLASS(CView1),CSize(100,100),pContext));
5     VERIFY(m_splitter.CreateView(1,0,RUNTIME_CLASS(CView2),CSize(100,100),pContext));
6     return TRUE;
7 }

获取分割视图指针

1 CView1* pView1 = (CView1*)m_wndSplitter.GetPane(0,0);
2 CView2* pView2 = (CView2*)m_wndSplitter.GetPane(1,0);

10、通过鼠标获得子窗口指针

1 CWnd* ChildWindowFromPoint(POINT point) const;
2 CWnd* ChildWindowFromPoint(POINT point,UINT nFlags) const;

用于确定包含指定点的子窗口
如果指定点在客户区之外,函数返回NULL;
如果指定点在客户区内,但是不属于任何一个子窗口,函数返回该CWnd的指针;
如果有多个子窗口包含指定点,则返回第一个子窗口的指针。
还要注意的是,该函数返回的是一个伪窗口指针,不能将它保存起来供以后使用。
对于第二个参数nFlags有几个含义:
CWP_ALL             file://不忽略任何子窗口
CWP_SKIPNIVSIBLE    file://忽略不可见子窗口
CWP_SKIPDISABLED    file://忽略禁止的子窗口
CWP_SKIPRANSPARENT file://忽略透明子窗口

11.获取CWnd*类

1 CWnd* pWnd;
2 pWnd = FromHandle(GetWindow(m_hWnd,GW_CHILD));//根据窗口句柄获取窗口指针
3 pWnd = FindWindow(NULL,_T("子窗口"));//根据窗口名称获取窗口指针
4 pWnd = GetActiveWindow();//该函数可以获得与调用线程的消息队列相关的活动窗口的窗口句柄

 

在获取视类的指针时,需要在需要获取类的cpp文件前面加入#include "***View.h",这样编译时会报错,解决方法是在视类的cpp文件前面加入#include "***Doc.h"

posted on 2013-07-31 09:20  胡小颖颖  阅读(279)  评论(0编辑  收藏  举报