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

posted @ 2016-10-27 17:14  VIPWTL  阅读(163)  评论(0编辑  收藏  举报