---
title: "SDI实现多视图并切换视图"
author: "winzenghua"
date: "2009-01-06T08:41:00Z"
tags: []
slug: "sdi"
draft: false
lastmod: "2009-01-06T08:41:00Z"
id: 948170
url: "https://java4gis.iteye.com/blog/948170"
---

首先在H文件有如下声明：

class CMultiViewApp : public CWinApp&lt;?XML:NAMESPACE PREFIX = O /&gt;

{

public:

CView* m\_pFirstView;

CView* m\_pOtherView;

int m\_currentView;

CView* m\_pView2;

CView* m\_pView1;

CMultiViewApp();

// Overrides

// ClassWizard generated virtual function overrides

//{{AFX\_VIRTUAL(CMultiViewApp)

public:

virtual BOOL InitInstance();

//}}AFX\_VIRTUAL

// Implementation

//{{AFX\_MSG(CMultiViewApp)

afx\_msg void OnAppAbout();

afx\_msg void OnViewOtherview();

afx\_msg void OnViewFirstview();

//}}AFX\_MSG

afx\_msg void OnViewChange(UINT nCmdID);

DECLARE\_MESSAGE\_MAP()

};

其次，在CPP文件有如下消息MAP：

/////////////////////////////////////////////////////////////////////////////

// CMultiViewApp

BEGIN\_MESSAGE\_MAP(CMultiViewApp, CWinApp)

//{{AFX\_MSG\_MAP(CMultiViewApp)

ON\_COMMAND(ID\_APP\_ABOUT, OnAppAbout)

ON\_COMMAND(ID\_VIEW\_OTHERVIEW, OnViewOtherview)

ON\_COMMAND(ID\_VIEW\_FIRSTVIEW, OnViewFirstview)

//}}AFX\_MSG\_MAP

// Standard file based document commands

ON\_COMMAND(ID\_FILE\_NEW, CWinApp::OnFileNew)

ON\_COMMAND(ID\_FILE\_OPEN, CWinApp::OnFileOpen)

// Standard print setup command

ON\_COMMAND(ID\_FILE\_PRINT\_SETUP, CWinApp::OnFilePrintSetup)

ON\_COMMAND\_RANGE( ID\_VIEW\_VIEW1, ID\_VIEW\_VIEW2, OnViewChange)

END\_MESSAGE\_MAP()

说明:SDI程序在CMyApp::InitInstance()已经通过DocTemplate创建一个关联的视图/文档实例,切显示出来.具体实现如下:

BOOL CMultiViewApp::InitInstance()

{

AfxEnableControlContainer();

// Standard initialization

// If you are not using these features and wish to reduce the size

// of your final executable, you should remove from the following

// the specific initialization routines you do not need.

#ifdef \_AFXDLL

Enable3dControls(); // Call this when using MFC in a shared DLL

#else

Enable3dControlsStatic(); // Call this when linking to MFC statically

#endif

// Change the registry key under which our settings are stored.

// TODO: You should modify this string to be something appropriate

// such as the name of your company or organization.

SetRegistryKey(\_T("Local AppWizard-Generated Applications"));

LoadStdProfileSettings(); // Load standard INI file options (including MRU)

// Register the application's document templates. Document templates

// serve as the connection between documents, frame windows and views.

CSingleDocTemplate* pDocTemplate;

pDocTemplate = new CSingleDocTemplate(

IDR\_MAINFRAME,

RUNTIME\_CLASS(CMultiViewDoc),

RUNTIME\_CLASS(CMainFrame), // main SDI frame window

RUNTIME\_CLASS(CMultiViewView));

AddDocTemplate(pDocTemplate);

// Parse command line for standard shell commands, DDE, file open

CCommandLineInfo cmdInfo;

ParseCommandLine(cmdInfo);

// Dispatch commands specified on the command line

if (!ProcessShellCommand(cmdInfo))

return FALSE;

CView* pActiveView = ((CFrameWnd\*) m\_pMainWnd)-&gt;GetActiveView();

m\_pFirstView = pActiveView;

m\_pOtherView = (CView\*) new COtherView;

CDocument* pDoc = ((CFrameWnd\*)m\_pMainWnd)-&gt;GetActiveDocument();

//通过CCreateContext实现第二视图和文档的关联

CCreateContext context;

context.m\_pCurrentDoc = pDoc;

UINT m\_ID = AFX\_IDW\_PANE\_FIRST + 1;

CRect rect;

//为了演示第一种多视图是实现方法,把Vew的实例创建放在了这里

m\_pOtherView-&gt;Create(NULL, NULL, WS\_CHILD, rect, m\_pMainWnd, m\_ID, &context);

// The one and only window has been initialized, so show and update it.

m\_pMainWnd-&gt;ShowWindow(SW\_SHOWMAXIMIZED);

m\_pMainWnd-&gt;UpdateWindow();

m\_currentView=1;

return TRUE;

}

## 1. SDI单文档多视图实现方法 1

void CMultiViewApp::OnViewOtherview()

{

// TODO: Add your command handler code here

UINT temp = ::GetWindowLong(m\_pOtherView-&gt;m\_hWnd, GWL\_ID);

::SetWindowLong(m\_pOtherView-&gt;m\_hWnd, GWL\_ID, ::GetWindowLong(m\_pFirstView-&gt;m\_hWnd, GWL\_ID));

::SetWindowLong(m\_pFirstView-&gt;m\_hWnd, GWL\_ID, temp);

m\_pFirstView-&gt;ShowWindow(SW\_HIDE);

m\_pOtherView-&gt;ShowWindow(SW\_SHOW);

((CFrameWnd\*)m\_pMainWnd)-&gt;SetActiveView(m\_pOtherView);

((CFrameWnd\*) m\_pMainWnd)-&gt;RecalcLayout();

m\_pOtherView-&gt;Invalidate();

}

void CMultiViewApp::OnViewFirstview()

{

// TODO: Add your command handler code here

UINT temp = ::GetWindowLong(m\_pOtherView-&gt;m\_hWnd, GWL\_ID); //GetWindowWord()

::SetWindowLong(m\_pOtherView-&gt;m\_hWnd, GWL\_ID, ::GetWindowLong(m\_pFirstView-&gt;m\_hWnd, GWL\_ID));//SetWindowWord()

::SetWindowLong(m\_pFirstView-&gt;m\_hWnd, GWL\_ID, temp);//SetWindowWord()

m\_pOtherView-&gt;ShowWindow(SW\_HIDE);

m\_pFirstView-&gt;ShowWindow(SW\_SHOW);

((CFrameWnd\*)m\_pMainWnd)-&gt;SetActiveView(m\_pFirstView);

((CFrameWnd\*)m\_pMainWnd)-&gt;RecalcLayout();

m\_pFirstView-&gt;Invalidate();

}

## 2. SDI单文档多视图实现方法 2

void CMultiViewApp::OnViewChange(UINT nCmdID)

{

//另外一种方法实现SDI的多视图切换

CView* pViewAdd;

CView* pViewRemove;

CMainFrame* pMainFrame=(CMainFrame\*)AfxGetMainWnd();

CDocument* pDoc = pMainFrame-&gt;GetActiveDocument();

if((nCmdID == ID\_VIEW\_VIEW1) &&