Microsoft KB Archive/141334

= VSWAP32 samples are available for multiple-view switching in a single-document interface (SDI) application =

Article ID: 141334

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 4.0 Standard Edition

 Microsoft Visual C++ 4.1 Subscription

 Microsoft Visual C++ 4.2 Enterprise Edition

 Microsoft Visual C++ 5.0 Enterprise Edition</li></ul>

 Microsoft Visual C++ 6.0 Enterprise Edition</li></ul>

 Microsoft Visual C++ 4.2 Professional Edition</li></ul>

 Microsoft Visual C++ 5.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q141334

<div class="summary_section">

SUMMARY
VSWAP32 demonstrates methods of switching between multiple views on a single document in a single-document interface (SDI) application. VSWAP32 displays two form views and a normal view that displays the list of data collected in the two form views.

NOTE: This is the 32-bit version of this sample. There is also a 16-bit version available called VSWAP.EXE.

This sample application demonstrates using DDX/DDV (dialog data exchange/dialog data validation), with correct document updating and data validation when switching between views. It also correctly catches unsaved/unvalidated data when the application is closed.

The following files are available for download from the Microsoft Download Center:

Visual C++ 6.0
Download Vcswap32.exe now

For more information about how to download Microsoft support files, click the following article number to view the article in the Microsoft Knowledge Base:

119591 How to obtain Microsoft support files from online services

Microsoft scanned this file for viruses. Microsoft used the most current virus-detection software that was available on the date that the file was posted. The file is stored on security-enhanced servers that help prevent any unauthorized changes to the file.

Visual C++ .NET
Download Vcswap32vcnet.exe now

Release Date: June 26, 2002

For more information about how to download Microsoft support files, click the following article number to view the article in the Microsoft Knowledge Base:

119591 How to obtain Microsoft support files from online services

Microsoft scanned this file for viruses. Microsoft used the most current virus-detection software that was available on the date that the file was posted. The file is stored on security-enhanced servers that help prevent any unauthorized changes to the file. After downloading the file, use the following command to extract the sample and build the appropriate directory structure:

VSWAP32.EXE -d

<div class="moreinformation_section">

MORE INFORMATION
The most important implementations of this are in:

CVswapApp::InitInstance

CVswapApp::SwitchView

CVswapApp::SaveActiveViewsData

The standard InitInstance code creates an initial document template, document, and view during the call to ProcessShellCommand. The code added to the end of InitInstance creates the view objects for the extra views used in this application and stores their pointers in a CVswapApp member array. It then cycles through a loop which creates the windows for the view objects. Each view window is created with a unique child window ID and a CCreateContext object that associates each view with the same CDocument object, which was created by ProcessShellCommand. Finally, the sample code triggers the OnInitialUpdate for the extra views.

In this sample, both views persist for the lifetime of the application; however, the application could create the new view dynamically.

The SwitchView function created in this sample swaps the current view with a previously hidden view. It accomplishes this by first switching their child window IDs. This step is necessary because MFC relies on the standard view pane having the child window ID of AFX_IDW_PANE_FIRST. The rest of the code hides the current view and tells MFC to repaint and begin using the new view.

The SaveActiveViewsData is used to save the information from the active view to the document object. It is called by SwitchView whenever a view is switched. SaveActiveViewsData subsequently calls SetToDoc, which actually updates the data from the form view to the document by using MFC's DDX/DDV mechanisms in DoDataExchange.

Sample code
The SwitchView function is included here as a reference.

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

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; pNewView->Invalidate; return pActiveView; }

<div class="references_section">