VC单文档实现多视图的方法

来源:互联网 发布:眼镜店 知乎 编辑:程序博客网 时间:2024/05/21 17:38
多视图是VC开发中经常要用到的技术之一,一般地实现单文档多视图有两种方式1)通过视图分割的技术(使用CSplitterWnd实现),将窗口分割为多个部分,每个部分显示各自显示不同的视图,这种技术实现起来比较简单,并

多视图是VC开发中经常要用到的技术之一,一般地实现单文档多视图有两种方式1)通过视图分割的技术(使用CSplitterWnd实现),将窗口分割为多个部分,每个部分显示各自显示不同的视图,这种技术实现起来比较简单,并且相关的资料也很多。2)通过一个文档关联多个视图,窗口显示整个视图。第二种实现较第一种复杂,这里给出详细的实现方法。

Step 1:使用VC 6.0新建一个Project,命名为:MultiView。除选择单文档属性外,一切使用“默认”方式。于是你可以获得五个类:CMainFrame ,CMultiViewApp,CMultiViewDoc,CMultiViewView,和CAboutDlg;

Step 2:新建一个新的视图View,添加一个新的MFC Class(Insert->New Class),基类为CView(或者CView的派生子类,如CEditView等)。类的名字为CAnotherView,这就是新的视图;并为CAnotherView添加GetDocument的实现:

CMultiViewDoc* CAnotherView::GetDocument()

{

return (CMultiViewDoc*)m_pDocument;

}

Step 3:在CMultiViewApp添加成员变量记录这两个视图:

private:

CView* m_pFirstView;

CView* m_pAnotherView;

给程序菜单IDR_MAINFRAME添加一个菜单项目“视图”,该菜单项有两个子菜单“视图一”和“视图二”,添加相应函数(void CMultiViewApp:: OnShowFirstview()和void CMultiViewApp:: OnShowSecondview());

Step 4:创建新的视图:在BOOL CMultiViewApp::InitInstance()中添加代码:

…….

//创建一个新的视图

CView* m_pActiveView = ((CFrameWnd*)m_pMainWnd)->GetActiveView();

m_pFirstView = m_pActiveView;

m_pAnotherView = new CAnotherView();

//文档和视图关联

CDocument* m_pDoc = ((CFrameWnd*)m_pMainWnd)->GetActiveDocument();

CCreateContext context;

context.m_pCurrentDoc = m_pDoc;

//创建视图

UINT m_IDFORANOTHERVIEW = AFX_IDW_PANE_FIRST + 1;

CRect rect;

m_pAnotherView->Create(NULL,NULL,WS_CHILD,rect,m_pMainWnd,

m_IDFORANOTHERVIEW,&context);

……

Step 5:现在已经创建了视图,并且都和文档关联起来了。现在要作的就是视图间的转换。在void CMultiViewApp:: OnShowFirstview()中添加实现代码:

void CMultiViewApp::OnShowFirstview()

{

// TODO: Add your command handler code here

UINT temp = ::GetWindowLong(m_pAnotherView->m_hWnd, GWL_ID);

::SetWindowLong(m_pAnotherView->m_hWnd, GWL_ID, ::GetWindowLong(m_pFirstView->m_hWnd, GWL_ID));

::SetWindowLong(m_pFirstView->m_hWnd, GWL_ID, temp);

m_pAnotherView->ShowWindow(SW_HIDE);

m_pFirstView->ShowWindow(SW_SHOW);

((CFrameWnd*)m_pMainWnd)->SetActiveView(m_pFirstView);

((CFrameWnd*) m_pMainWnd)->RecalcLayout();

m_pFirstView->Invalidate();

}

在void CMultiViewApp:: OnShowSecondview()中添加实现代码:

void CMultiViewApp::OnShowSecondview()

{

// TODO: Add your command handler code here

UINT temp = ::GetWindowLong(m_pAnotherView->m_hWnd, GWL_ID);

::SetWindowLong(m_pAnotherView->m_hWnd, GWL_ID, ::GetWindowLong(m_pFirstView->m_hWnd, GWL_ID));

::SetWindowLong(m_pFirstView->m_hWnd, GWL_ID, temp);

m_pFirstView->ShowWindow(SW_HIDE);

m_pAnotherView->ShowWindow(SW_SHOW);

((CFrameWnd*)m_pMainWnd)->SetActiveView(m_pAnotherView);

((CFrameWnd*) m_pMainWnd)->RecalcLayout();

m_pAnotherView->Invalidate();

}

Step 6:为了演示,这里将不同的视图给予一个标记,在CMultiViewView和CAnotherView的OnDraw方法中分别添加以下代码:

pDC->TextOut(400,300,"First View");

pDC->TextOut(400,320,pDoc->GetTitle());

pDC->TextOut(400,300,"Another View");

pDC->TextOut(400,320,pDoc->GetTitle());

至此就大功告成了。

0 0