文档视图窗口的切换,本人尝试过,有效;

第一,在每个视图列的构造函属性数改为public,同时头文件加上#include <shlwapi.h>;

第二:

在文档的CQiehuanApp里面弄;

1。

.h 文件

private:
UINT m_nCurView;
CView *m_pViews[3];//三个视图,其中一个是本工程的视图类,2个是新建的cformview类;

afx_msg void On32772();//要点击切换视图的操作;

afx_msg void On32773();////要点击切换视图的操作;
CView* SwitchView( UINT nIndex );
void CreateMoreViews();
BOOL SaveActiveViewsData();

2. .cpp 文件

步骤是 创建,保存,切换;

在初始化函数的最后,也就是return TRUE 的前面创建文档;

第一步;

BOOL CQiehuanApp::InitInstance()
{

CreateMoreViews();

return TRUE;

}

void CQiehuanApp::CreateMoreViews()
{
m_nCurView = 0; // Save index of the currently active view class

// Keep array of views as member of WinApp

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

m_pViews[0] = pActiveView;
m_pViews[1] = (CView*)(new Cmap );//定义一个screenDefineView对象;
m_pViews[2] = ( CView * )new PRODATGA ;

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

// Initialize a CCreateContext to point to the active document.
// With this context, the new view is added to the document
// when the view is created in CView::OnCreate().
CCreateContext newContext;
newContext.m_pNewViewClass = NULL;
newContext.m_pNewDocTemplate = NULL;
newContext.m_pLastView = NULL;
newContext.m_pCurrentFrame = NULL;
newContext.m_pCurrentDoc = pCurrentDoc;

// The ID of the initial active view is AFX_IDW_PANE_FIRST.
// Incrementing this value by one for additional views works
// in the standard document/view case but the technique cannot
// be extended for the CSplitterWnd case.
CRect rect(0, 0, 0, 0); // gets resized later

// Need to cast pointers to have correct Create functions called
// CForm2 is CFormView::Create
for ( int nView=1; nView <3; nView++ )
{
// Create the new view. In this example, the view persists for
// the life of the application. The application automatically
// deletes the view when the application is closed.
m_pViews[nView]->Create(NULL, NULL,
(AFX_WS_DEFAULT_VIEW & ~WS_VISIBLE),
// views are created with the style of AFX_WS_DEFAULT_VIEW
// In MFC 4.0, this is (WS_BORDER | WS_VISIBLE | WS_CHILD)
rect, m_pMainWnd,
AFX_IDW_PANE_FIRST + nView, &newContext);
m_pViews[nView]->SendMessage( WM_INITIALUPDATE );
}

// When a document template creates a view, the WM_INITIALUPDATE
// message is sent automatically. However, this code must
// explicitly send the message, as follows.


/*** End modification of default InitInstance ***/
}

第二部:

切换,切换函数里面调用保存函数;

afx_msg void On32772();//要点击切换视图的操作;

{  

  SwitchView( 2 );//要切换到第二张视图,第0张视图一般是工程的文档类;

}

afx_msg void On32773();////要点击切换视图的操作;

{

  SwitchView( 1 );//要切换到第一张视图,

}

**************

切换功能

CView* CQiehuanApp::SwitchView( UINT nIndex )
{
ASSERT( nIndex >=0 && nIndex < 3 );

CView* pNewView = m_pViews[nIndex];

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

if ( !pActiveView ) // No currently active view
return NULL;

if ( pNewView == pActiveView ) // Already there
return pActiveView;

// Update Doc's data if needed
// Don't change view if data validation fails
if ( ! SaveActiveViewsData() )//这里调用了保存的函数;
{
return pActiveView;
}

m_nCurView = nIndex; // Store the new current view's index

// exchange view window ID's so RecalcLayout() works
UINT temp = ::GetWindowLong(pActiveView->m_hWnd, GWL_ID);
::SetWindowLong(pActiveView->m_hWnd, GWL_ID,
::GetWindowLong(pNewView->m_hWnd, GWL_ID));
::SetWindowLong(pNewView->m_hWnd, GWL_ID, temp);

// Display and update the new current view - hide the old one
pActiveView->ShowWindow(SW_HIDE);
pNewView->ShowWindow(SW_SHOW);
((CFrameWnd*) m_pMainWnd)->SetActiveView(pNewView);
((CFrameWnd*) m_pMainWnd)->RecalcLayout();//将Toolbar、DialogBar等可浮动的东西安排位置、处理和View、Frame之间的位置关系的
pNewView->Invalidate();
return pActiveView;
}

保存功能

BOOL CQiehuanApp::SaveActiveViewsData()
{
CView* pActiveView =
((CFrameWnd*) m_pMainWnd)->GetActiveView();

if ( !pActiveView )
return TRUE; // No active view TO save data from, so count as success

return TRUE; // Not a view with data to save!
}

原文地址:https://www.cnblogs.com/chenzuoyou/p/3293079.html