SAMPLE: VSWAP32 Demos Multiple-View Switching in SDIID: Q141334
|
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 file is available for download from the Microsoft Software
Library:
~ Vswap32.exeFor more information about downloading files from the Microsoft Software Library, please see the following article in the Microsoft Knowledge Base:
Q119591 How to Obtain Microsoft Support Files from Online ServicesAfter downloading the file, use the following command to extract the sample and build the appropriate directory structure:
VSWAP32.EXE -d
The most important implementations of this are in:
CVswapApp::InitInstanceThe 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.
CVswapApp::SwitchView
CVswapApp::SaveActiveViewsData
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;
}
For other examples of applications that switch views, please refer to the following samples:
COLLECT (SDI)
ENROLL (SDI)
VWRPLC32 (MDI)
SPLIT32 (Splitter).
For more information on DDX and DDV routines, please see Technical Note
26: DDX and DDV Routines in the Microsoft Development Library.
Additional query words: mfc
Keywords : kbcode kbfile kbsample kbDocView kbMFC kbVC kbVC400 kbVC410 kbVC420 kbVC500 kbVC600
Version : 4.0 4.1 4.2 5.0 6.0
Platform : NT WINDOWS
Issue type :
Last Reviewed: July 19, 1999