Multiple Views Using SDI

WEBINAR: On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

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

 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.

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

Comments

  • Jeff

    Posted by Wets J. on 09/05/2015 09:39pm

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

    Reply
  • Thank you!

    Posted by Peter on 03/31/2012 12: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 :)

    Reply
  • DEMO WORKS WITH POSTED BUG FIX - MULTIPLE DOCS ??

    Posted by Mike Pliam on 11/09/2010 02: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. : )

    Reply
  • Multiple Views Using SDI -- problem

    Posted by pradeep556 on 08/07/2008 09:12am

    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 .

    Reply
  • WARNING: DO NOT USE THIS CODE

    Posted by souldog on 03/25/2004 04:39pm

    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/21/2006 06:16pm

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

      Reply
    Reply
  • Urgent: Need to retain the changes on each view

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

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

    Reply
  • Multiple simultaneous views?

    Posted by Legacy on 06/10/2003 12: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.

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

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

    Originally posted by: shark

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

    Reply
  • Does not work

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

    Originally posted by: vlad

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

    Reply
  • Quirk: xp pro + vc6 - thanks.

    Posted by Legacy on 03/26/2003 12: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.

    ++R


    Reply
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

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