Multiple Views Using SDI
In order for this method to work correctly you need to make one simple change in the header of your CView derived classes. The default constructor for the view needs to be have the protected storage class changed to public ( See code sample below). This is due to the fact that we will be constructing the view on the fly when it is switched to the active view. We could create all the views at one time and have them available but that is wasteful of memory and resources and is not really needed. If you wish to do so, however the code can easily be changed to achive this.
class CMainView : public CFormView
{
protected: // create from serialization only Change this to
public: // create from serialization only this in all CView derived classes
CMainView();
DECLARE_DYNCREATE(CMainView)
The actual function that will swap the views is located in your CFrame derived class. This is the logical place to put it because in the SDI architecture the frame class controls the view class. It also allows us to centralize message handling for switching views in/out. As far as MFC member functions go it is pretty simple. It simply disconnects the old view and destroys it , creates a new CView derived class and attaches the document to it, sets a few flags, shows the view and we are in business. here it is.
void CMainFrame::SwitchToForm(int nForm)
{
CView* pOldActiveView = GetActiveView(); // save old view
CView* pNewActiveView = (CView*)GetDlgItem(nForm); // get new view
if (pNewActiveView == NULL)
{
switch(nForm) // these IDs are the dialog IDs of the view but can use anything
{
case IDD_MULTISCREEN_FORM:
pNewActiveView = (CView*)new CMainView;
break;
case IDD_MULTISCREEN_FORM2:
pNewActiveView = (CView*)new CView2;
break;
case IDD_MULTISCREEN_FORM3:
pNewActiveView = (CView*)new CView3;
break;
case IDD_MULTISCREEN_FORM4:
pNewActiveView = (CView*)new CView4;
break;
}
CCreateContext context; // attach the document to the new view
context.m_pCurrentDoc = pOldActiveView->GetDocument();
pNewActiveView->Create(NULL, NULL, 0L, CFrameWnd::rectDefault, // and the frame
this, nForm, &context);
pNewActiveView->OnInitialUpdate();
}
SetActiveView(pNewActiveView); // change the active view
pNewActiveView->ShowWindow(SW_SHOW); // show the new window
pOldActiveView->ShowWindow(SW_HIDE); // hide the old
::SetWindowWord(pNewActiveView->m_hWnd, GWL_ID, AFX_IDW_PANE_FIRST); // gotta have it
RecalcLayout(); // adjust frame
delete pOldActiveView; // kill old view
}
Now all we need to do is attach some menu options and handle the users requests
to switch views. This is easily done using Class wizard to create the blank
functions and message handlers. Below is the code demonstrating a call to
switch screens.
void CMainFrame::OnForm1()
{
SwitchToForm(IDD_MULTISCREEN_FORM);
}
Also as a UI issue it would be nice if the menu option gave a clue as to what
screen we were on and to prevent the user from attempting to switch to the
current screen. This is easily accomplished by the code below.
void CMainFrame::OnUpdateForm1(CCmdUI* pCmdUI)
{
pCmdUI->Enable(GetActiveView()->GetRuntimeClass()!=RUNTIME_CLASS(CMainView));
}
I have a small demo project that shows illustrates the principals talked about in this article. It shows a SDI with 4 screens and allows one to switch views via a popup menu or a frame menu.

Comments
Nike Current Max 1 FB press, father a eager color texture, the new shoes
Posted by Geozyoceada on 04/21/2013 08:38amIn the summer in a pane backing bowels the serene sprite seems to be a good fitting, but if the sprite "feet"? Resolution also fork out you a frisk, take a slaking nourishment! This summer, Nike and Sprite [url=http://markwarren.org.uk/goodbuy.cfm]nike free run[/url] and his sneakers to a blend of outstanding example snow spread of callow, white and indecent color blueprint in the undying Nike Arrogance Max 1 shoes let slip a drinks chill scent.[url=http://northernroofing.co.uk/roofins.cfm]nike free uk[/url] Summer is the yet to pick a cleanly shoe, shoes should be a good choice. Qualifying series Nike Freshen Max HomeTurf city recently at the end of the day comes up, this series in the classic Melody Max shoes to London, Paris and Milan the three paid encomium to the iconic megalopolis of Europe, combined with the characteristics of the three cities, Like Max 1 HYP,Allied Max 90 HYP,Air Max 1 and shoes such as Air Max 95, combined [url=http://northernroofing.co.uk/roofins.cfm]nike free uk[/url] with the Hyperfuse, as kindly as a heterogeneity of materials, such as suede, Whether you crave functioning or retro-everything.
ReplyAlother Stupid Fuck
Posted by Liquid Snake on 01/04/2013 06:24pmHello, Wow, Just cant figure out why people post half ass garbage. This code does switch the View however the View has been created with the wrong styles so when activated it never gets any proper size and the frame window just shows whatever pixels are in the desktop background because the view is active but has no size. Liquid Snake
ReplyThank you!
Posted by Peter on 03/31/2012 12:04pmThanks for this amazing piece of code. I had spent last few hours trying to figure out how to switch views in my database app, with no results, until I came here. Anyway, you've just made my day :)
ReplyDEMO WORKS WITH POSTED BUG FIX - MULTIPLE DOCS ??
Posted by Mike Pliam on 11/09/2010 02:00pmThis code appears to work when the bug fix posted by Marko Shade is substituted for the original CMainFrame::SwitchToForm. But all of the views here use a CDocument derived Doc. What I need is a method of handling views such as CView and CRichEditView that require differing Doc derivations.. This demo comes so close to a solution to that problem, but I do not understand how to link the Docs and the Views. Any ideas greatly appreciated. : )
ReplyMultiple Views Using SDI -- problem
Posted by pradeep556 on 08/07/2008 09:12amWARNING: DO NOT USE THIS CODE
Posted by souldog on 03/25/2004 04:39pmThis code does not work. Do not use it. If you want to see the proper way to do this look at the collections sample on MSDN
-
Reply?
Posted by DanYELL on 06/21/2006 06:16pmIs there any specific code you can recommend? I like the idea of a multiple view like this for a SDI interface.
ReplyUrgent: Need to retain the changes on each view
Posted by Legacy on 12/11/2003 12:00amOriginally posted by: Musa
Hi,
Every time I switch views, it creates new View. I don't want to do that way. I want to retain the changes made to earlier view.
Please help.
ReplyThanks in advance.
Musa
Multiple simultaneous views?
Posted by Legacy on 06/10/2003 12:00amOriginally posted by: david aberdeen
How can this model be extended to allow multiple views at the same time? My app needs to have one main view of a 3d model, and then many other views for data entry, or 2d display of different components of the model. I've currently build the application around non-CView dialog boxes, (with only one open at a time to remove data synchronisation issues), but it's getting many views, and getting messy, whereas a multiple view MDI solution would be much neater.
Any help would be appreciated.
ReplyHelp!this programe is used into database.it couldn't run.
Posted by Legacy on 06/08/2003 12:00amOriginally posted by: shark
this programe is used into database.it couldn't run.debug show "...wrong in file: view.cpp.....line:69.....
ReplyDoes not work
Posted by Legacy on 05/06/2003 12:00amOriginally posted by: vlad
Got the project, recompiled: does not switch at all.
ReplyLoading, Please Wait ...