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));

8、文檔模版、文檔

  從文檔獲取文檔模版指針:

  CDocTemplate* GetDocTemplate() const;

  從文檔模版獲取文檔指針:

  viaual POSITION GetFirstDocPosition( ) const = 0;

  visual CDocument* GetNextDoc(POSITION & rPos) const = 0;

  9、獲取分割視圖中各個視圖的指針

  主框架中定義:CSplitterWnd m_wndSplitter;

  定義兩個View類:CView1、CView2

  框架類中重載:

  BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT, CCreateContext* pContext)

  {

  VERIFY(m_splitter.CreateStatic(this,2,1)); //分割成兩行一列

   VERIFY(m_splitter.CreateView(0,0,RUNTIME_CLASS(CView1),CSize(100,100),pContext));

   VERIFY(m_splitter.CreateView(1,0,RUNTIME_CLASS(CView2),CSize(100,100),pContext));

  return TRUE;

  }

  獲取分割視圖指針

  CView1* pView1 = (CView1*)m_wndSplitter.GetPane(0,0);

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

  10、通過鼠標獲得子窗口指針

  CWnd* ChildWindowFromPoint(POINT point) const;

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

  用於確定包含指定點的子窗口

  如果指定點在客戶區之外,函數返回NULL;

  如果指定點在客戶區內,但是不屬於任何一個子窗口,函數返回該CWnd的指針;

  如果有多個子窗口包含指定點,則返回第一個子窗口的指針。

  還要注意的是,該函數返回的是一個偽窗口指針,不能將它保存起來供以后使用。

  對於第二個參數nFlags有幾個含義:

  CWP_ALL             file://不忽略任何子窗口

  CWP_SKIPNIVSIBLE    file://忽略不可見子窗口

  CWP_SKIPDISABLED    file://忽略禁止的子窗口

  CWP_SKIPRANSPARENT file://忽略透明子窗口

  獲取工具條的指針

  在缺省狀態下,有一個默認的工具條AFX_IDW_TOOLBAR,我們可以根據相應的ID去獲取工具條指針,方法如下:

  CToolBar* pToolBar=(CToolBar*)AfxGetMainWnd()->GetDescendantWindow(AFX_IDW_TOOLBAR);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM