Click here to Skip to main content
12,950,268 members (64,643 online)
Click here to Skip to main content
Add your own
alternative version


109 bookmarked
Posted 21 Oct 2001

Creation of Multiple Dynamic Views

, 15 Nov 2001
Rate this:
Please Sign up or sign in to vote.
An article on creation of multiple views dynamically without document/view architecture
<!-- Download Links --> <!-- Article image -->

Sample Image - dynviews.jpg

<!-- Add the rest of your HTML here -->


This article presents a method for multiple dynamic views creation whithout the MFC's document/view architecture. That views could be attached to other frames, controls or dock bars.

The document/view architecture is a very useful mechanism to create robust applications that requires multiple views, but sometimes there is not necessary such funcionality. Even though creating views whithout the help of the framework is neither evident or easy. The problem lies basically in the following aspects:

  1. Derivating a class from CView does not allow creation of the views using the operator new because the constructor and destructor of the derivated class are protected.

  2. When the window or frame that contains the views created "by hand" is destroyed, then automatically the objects CView are destroyed and is invokated the DestroyWindow() for each one. That's why the created views can not be referenced using objects like member variables of a class but using pointers to objects, so it is necessary to use the operator new for its creation but not invoking delete because the framework will destroy that objects automatically.

  3. When creating multiple views dinamically there is necessary to assign valid identifiers that can not be defined using the resource editor of Visual C++. For that there must be defined a generic identifier that will be used as base for the identification of each view that will be created by the time (using something like WM_USER + idView0, WM_USER + idView1, ...).

Step by Step

  1. Derivate the customized view/views using the wizard of Visual C++ from CView and make public the constructor and destructor of the class. This will allow the using of the operator new in the objects creation.

    //// This view encapsulates a personalized 2D view
    class CMyView : public CView
      class CMyPoint : public CObject  
        CMyPoint(CPoint location) { m_location = location; }
        CPoint  m_location;
      virtual ~CMyView();
      int  m_idView;
    // Attributes
      CObList	  m_points;
    // Operations
    // Overrides
      // ClassWizard generated virtual function overrides
      virtual void OnDraw(CDC* pDC);      // overridden to draw this view
    // Implementation
    #ifdef _DEBUG
      virtual void AssertValid() const;
      virtual void Dump(CDumpContext& dc) const;
      // Generated message map functions
      afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
      afx_msg void OnMouseMove(UINT nFlags, CPoint point);
  2. In the frame/window/dialog class where it is desired to create and reference the views, it is necessary to add an object list/array (CObList) or a set of attributes pointer to the views so in the future could be referenced the views using those variables. In the example it is created an object CObList that stores a pointer to each view created and that object CObList is added to the dialog which is the main window.

    // CDynViewsDlg dialog
    class CDynViewsDlg : public CDialog
    // Construction
      CDynViewsDlg(CWnd* pParent = NULL); // standard constructor
    // Dialog Data
      enum { IDD = IDD_DYNVIEWS_DIALOG };
      CTabCtrl  m_tabCtrl;
      // ClassWizard generated virtual function overrides
      virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV support
    // Implementation
      void AddView(VIEW_TYPE viewType);
      void SelectTab(int nTab);
      HICON m_hIcon;
      // This member allows to reference any
      // dynamically created view
      CObList	  m_listViews;
      // Generated message map functions
      virtual BOOL OnInitDialog();
      afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
      afx_msg void OnPaint();
      afx_msg HCURSOR OnQueryDragIcon();
      afx_msg void OnSelchangeTabctrl(NMHDR* pNMHDR, LRESULT* pResult);
      afx_msg void OnAddview();
      afx_msg void OnAddopenglview();
  3. When is desired to create a new view, simply must be created an object of that class CView using the operator new, store the pointer to that view for lately use, search a new identifier not used for the new view and invoke create of the view using that identifier.

    // Handle for button click "Add 2D View"
    void CDynViewsDlg::OnAddview() 
    // Handle for button click "Add 3D/OpenGL View"
    void CDynViewsDlg::OnAddopenglview() 
     * AddView Method:
     * 1. Creates a new tab for the view
     * 2. Creates a new view dynamically of the desired type
     * 3. Asociates the id of the view with the id of the tab
     * 4. Selects the new created view and tab
    void CDynViewsDlg::AddView(VIEW_TYPE viewType)
      // ID of the new view
      int idNewView = m_listViews.GetCount();
      // View size
      CRect clientRect;
      clientRect.DeflateRect(10, 30);
      // Creation 'on the fly' of the new view
      CMyView* m_pNewView;
      CString tabCaption;
      switch(viewType) {
      case VIEW_2D:
        // The operator 'new' has not asociated delete.
        // The view will be deleted automatically by the framework
        // on the WM_DESTROY handle of the parent (the tab control)
        m_pNewView = new CMyView();
        tabCaption.Format("View %d (2D)", idNewView);
        case VIEW_OPENGL:
        // The operator 'new' has not asociated delete.
        // The view will be deleted automatically by the framework
        // on the WM_DESTROY handle of the parent (the tab control)
        m_pNewView = new COpenGLView();
        tabCaption.Format("View %d (OpenGL)", idNewView);
      // The new tab for the view on the tab control
      m_tabCtrl.InsertItem(idNewView, tabCaption);
      m_pNewView->m_idView = idNewView;
      // Creation of the view window
      if(! m_pNewView->Create(NULL, NULL, WS_VISIBLE | WS_CHILD, clientRect,
                              &m_tabCtrl, ID_VIEW_BASE + idNewView))
        TRACE( "Failed view creation\n" );
      // Using a list is easy to handle all the views created
      // Select the new created tab/view
  4. Once this has been made it is possible to use the pointer to the view to achieve basic window functions like show/hide, change size, etc.

     * This method switchs the visibility of each
     * view, making visible only the selected view
     * asociated with the tab nTab
    void CDynViewsDlg::SelectTab(int nTab)
      CMyView* pViewSelected = NULL;
      POSITION pos = m_listViews.GetHeadPosition();
      // Hide all views under the tabControl
      while(pos != NULL) {
        CMyView* pView = (CMyView*) m_listViews.GetNext(pos);
        if(pView->m_idView == nTab) pViewSelected = pView;
      // Show the view asociated with the tab selected
      if(pViewSelected != NULL)
  5. It is really important to remember that the objects CView created dynamically must not be deleted explicitally using the operator delete. In fact they will be automatically deleted by the framework in the WM_DESTROY message of the window that is parent of the views. In the example the parent of all the views is a tab control and when the application exits all the views are deleted and there are not any memory leaks.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

No Biography provided

You may also be interested in...

Comments and Discussions

Questionhow to add a close button to each tab? Pin
inverson1220@gmail22-Jun-07 16:22
memberinverson1220@gmail22-Jun-07 16:22 
QuestionHow to Communicate from frame to View Pin
urxlnc00725-Aug-05 22:09
memberurxlnc00725-Aug-05 22:09 
QuestionCreate the View in PropertyPage? Pin
bonmeepon19-Apr-05 2:00
memberbonmeepon19-Apr-05 2:00 
GeneralWhy not use CWnd instead of CView Pin
magic116-Jun-04 22:25
membermagic116-Jun-04 22:25 
GeneralJpeg as Background Image in SDI Pin
suniljoshi_MCA30-Mar-04 16:32
membersuniljoshi_MCA30-Mar-04 16:32 
GeneralThanks a lot.Very Good! Pin
benben16-Feb-04 20:46
memberbenben16-Feb-04 20:46 
GeneralMultiple view - definition please Pin
Vaclav8-Aug-03 6:36
memberVaclav8-Aug-03 6:36 
GeneralRe: Multiple view - definition please Pin
rj4527-Oct-03 21:50
memberrj4527-Oct-03 21:50 
Generaladding scrollview Pin
manito23-May-03 12:43
membermanito23-May-03 12:43 
GeneralRe: adding scrollview Pin
rj4527-Oct-03 21:51
memberrj4527-Oct-03 21:51 
Generaldoc/view down! Pin
EPulse2-Jan-03 22:59
memberEPulse2-Jan-03 22:59 
QuestionHow to dynamically create a view in MDI or SDI app? Pin
Wayne King8-Nov-01 14:36
memberWayne King8-Nov-01 14:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170525.1 | Last Updated 16 Nov 2001
Article Copyright 2001 by Pedro Jorquera
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid