设为首页 加入收藏

TOP

引用MFC指针的获取
2014-11-12 16:15:07 】 浏览:4143
Tags:引用 MFC 指针 获取

  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:


  CMainFrame* pMainFrame = (CMainFrame*)AfxGetMainWnd();


  CYourDoc* pDoc = (CYourDoc*)pMainFrame->GetActiveDocument();


  CYourView* pView = (CYourView*)pMainFrame->GetActiveView();


  MDI:


  CMainFrame* pMainFrame = (CMainFrame*)AfxGetMainWnd();


  CChildFrame* pChildFrame = (CChildFrame*)pMainFrame->GetActiveFrame();


  CYourDoc* pDoc = (CYourDoc*)pChildFrame->GetActiveDocument();


  CYourView* pView = (CYourView*)pChildFrame->GetActiveView();


  7、文档、视图


  从视图获取文档指针:


  CYourDoc* pDoc = GetDocument();


  从文档获取视图指针:


  利用成员函数 GetFirstViewPosition() 和 GetNextView() 遍历


  virtual POSITION GetFirstViewPosition() const;


  virtual CView* GetNextView(POSITION& rPosition) const;


  SDI:


  CYourView* pView;


  POSITION pos = GetFirstViewPosition();


  pView = GetNextView(pos);


  MDI:


  定义函数


  CView* CYourDoc::GetView(CRuntimeClass* pClass)


  {


  CView* pView;


  POSITION pos=GetFirstViewPosition();


  while(pos!=NULL)


  {


  pView=GetNextView(pos);


  if(!pView->IsKindOf(pClass))


  break;


  }


  if(!pView->IsKindOf(pClass))


  {


  AfxMessageBox("Connt Locate the View.");


  return NULL;


  }


  return pView;


  }


  使用如下:


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


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇指针数组和指向指针的指针 下一篇C++的域作用分辨符

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目