上周查了好久SDI 单文档多视图方面的资料,怎么获取指定视图指针让我相当苦恼,最后终于找到了方法;
在MainFrm.h里面添加一个方法:
CView* GetView(CRuntimeClass *pClass);
在MainFrm.cpp 实现:
CView* CMainFrame::GetView(CRuntimeClass *pClass)
{
POSITION POS;
POS = AfxGetApp()->GetFirstDocTemplatePosition();
CDocTemplate* pDocTemplate = AfxGetApp()->GetNextDocTemplate(POS);
POS = pDocTemplate->GetFirstDocPosition();
CDocument* pDoc=pDocTemplate->GetNextDoc(POS);
POS = pDoc->GetFirstViewPosition();
CView* pView;
while (POS != NULL){
pView=(CView*)pDoc->GetNextView(POS);
//判断当前获取的视图是否为目标视图
if(pView->IsKindOf(pClass))break;
}
if( pView->IsKindOf(pClass) == FALSE )return NULL;
return pView;
}
可以调用你的目标视图,调用,如下:
(CXX_View*) pView = ((CMainFrame*)AfxGetMainWnd())->GetView(RUNTIME_CLASS(CXX_View));