MFC文档视图中窗口切换 (2012-05-11 18:32:48)
在文档试图应用程序,有时需要在工作区切换试图,以下就是如何切换试图了 1.创建要切换的视图类,同时把构造函数,Create函数改变为public 2.在需要切换试图的动作响应中,加入切换代码,一般是在CMainFrame的菜单响应中加: void CMainFrame::On32771() { // TODO: Add your command handler code here CView *pOldActiveView=GetActiveView();//旧视图 CMyView *pNewActiveView=new CMyView();//要切换的视图 CCreateContext context; context.m_pCurrentDoc=pOldActiveView->GetDocument();//关联到文档.为什么,文档视图,MFC中每个视图要对应一个文档 //创建视图并且关联到上下文中,IDD_MYVIEW为视图CMyView的ID号 pNewActiveView->Create(NULL,NULL,WS_CHILD | WS_BORDER,CFrameWnd::rectDefault,this,IDD_MYVIEW,&context); pNewActiveView->UpdateData(); //视图显示/隐藏 SetActiveView(pNewActiveView); pNewActiveView->ShowWindow(SW_SHOW); pOldActiveView->ShowWindow(SW_HIDE); //重新布局主框架 pNewActiveView->SetDlgCtrlID(AFX_IDW_PANE_FIRST); RecalcLayout(); delete pOldActiveView; } 3.想要切换回去,一样: CMyView *pOldActiveView=(CMyView *)GetActiveView();//旧视图 CChangeViewView *pNewActiveView=new CChangeViewView();//要切换的视图 CCreateContext context; context.m_pCurrentDoc=pOldActiveView->GetDocument();//关联到文档.为什么,文档视图,MFC中每个视图要对应一个文档 //创建视图并且关联到上下文中,IDD_MYVIEW为视图CMyView的ID号 pNewActiveView->Create(NULL,NULL,WS_CHILD | WS_BORDER,CFrameWnd::rectDefault,this,IDD_MYVIEW,&context); pNewActiveView->UpdateData(); //视图显示/隐藏 SetActiveView(pNewActiveView); pNewActiveView->ShowWindow(SW_SHOW); pOldActiveView->ShowWindow(SW_HIDE); //重新布局主框架 pNewActiveView->SetDlgCtrlID(AFX_IDW_PANE_FIRST); RecalcLayout(); delete pOldActiveView; 运行即可实现视图切换