首页 > 代码库 > MFC中类指针获取(佳佳)

MFC中类指针获取(佳佳)

定义:

CView*  CYourDoc::GetView(CRuntimeClass* pClass)
{
  CView* pView = NULL;
  POSITION pos=GetFirstViewPosition();

  while(pos!=NULL)

  {
    pView=GetNextView(pos);
    if( pView->IsKindOf(pClass))

    {
      return pView;
    }  
  }
  return NULL;
}

使用:
CYourView* pView=(CYourView*)GetView(RUNTIME_CLASS(CYourView));

注:GetView要用doc的指针

例:

自定义类1(FormView1)中使用自定义类2(FormView2)指针,

CMainFrame* pMainFrame = (CMainFrame*)AfxGetMainWnd();
CYourDoc* pDoc = (CYourDoc*)pMainFrame->GetActiveDocument();
FormView2* pView=(FormView2*)pDoc->GetView(RUNTIME_CLASS(FormView2));

MFC中类指针获取(佳佳)