Multiple Views Using SDI


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame

There are many advantages to using a CView derived class over a dialog based application. Many times however there is more information than screen space or the program just needs logical seperation of elements. MDI ( Multiple Document Interface) is the standard method of achiving this but many designers do not care for it. It tends to get messy quickly. Most of the time what is needed is a generalized easy way to have a SDI ( Single Document Interface) framework and the ability to switch the active view when needed. This article describes such a method.

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


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
    pNewActiveView = (CView*)new CMainView;

    pNewActiveView = (CView*)new CView2;

    pNewActiveView = (CView*)new CView3;

    pNewActiveView = (CView*)new CView4;

  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);

 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()

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)

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.

Download demo project - 26 KB

About the Author

Richard Stringer

C C++ ASM are my currrent languages of choice. Currently writing MFC apps for insurance companies


  • Jeff

    Posted by Wets J. on 09/06/2015 04:39am

    Thank you very much. Change "::SetWindowWord" into "::SetWindowLong". THEN IT WORKS. Otherwise NOT.

  • Thank you!

    Posted by Peter on 03/31/2012 07:04pm

    Thanks 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 :)


    Posted by Mike Pliam on 11/09/2010 10:00pm

    This 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. : )

  • Multiple Views Using SDI -- problem

    Posted by pradeep556 on 08/07/2008 04:12pm

    download demo project is not working properly, it not able to switch one view to another view, pls check the demo project code and provide the correct the solution .


    Posted by souldog on 03/26/2004 12:39am

    This 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

    • ?

      Posted by DanYELL on 06/22/2006 01:16am

      Is there any specific code you can recommend? I like the idea of a multiple view like this for a SDI interface.

  • Urgent: Need to retain the changes on each view

    Posted by Legacy on 12/11/2003 08:00am

    Originally posted by: Musa


    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.
    Thanks in advance.

  • Multiple simultaneous views?

    Posted by Legacy on 06/10/2003 07:00am

    Originally 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.

  • Help!this programe is used into database.it couldn't run.

    Posted by Legacy on 06/08/2003 07:00am

    Originally posted by: shark

    this programe is used into database.it couldn't run.debug show "...wrong in file: view.cpp.....line:69.....

  • Does not work

    Posted by Legacy on 05/06/2003 07:00am

    Originally posted by: vlad

    Got the project, recompiled: does not switch at all.

  • Quirk: xp pro + vc6 - thanks.

    Posted by Legacy on 03/26/2003 08:00am

    Originally posted by: roddy.muir

    Although it is obvious to some guru's (unlike me!), in order for this call to work, in this case a CFormView, which is being displayed has to have the style changed, using the resource editor if you wish, from "popup", to "Child", otherwise the pNewActiveView->Create call asserts.

    Richard and Uday thanks a lot! Just a minor quirk, which I found will ASSERT.


  • Loading, Please Wait ...

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date