Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Different views in SDI

Different views in SDI

Scheduled Pinned Locked Moved C / C++ / MFC
question
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dart13
    wrote on last edited by
    #1

    I need to put three different forms in SDI application's CView window so that user can choose which view he wants to see. If I make 3 different CFormView classes how do I select one to be shown in a CView window?

    D A 3 Replies Last reply
    0
    • D dart13

      I need to put three different forms in SDI application's CView window so that user can choose which view he wants to see. If I make 3 different CFormView classes how do I select one to be shown in a CView window?

      D Offline
      D Offline
      Dreamz
      wrote on last edited by
      #2

      http://www.codeproject.com/useritems/MultiViewsMFC\_MDI.asp Hope this article will help you .

      D 1 Reply Last reply
      0
      • D dart13

        I need to put three different forms in SDI application's CView window so that user can choose which view he wants to see. If I make 3 different CFormView classes how do I select one to be shown in a CView window?

        A Offline
        A Offline
        Andrew Quinn AUS
        wrote on last edited by
        #3

        Hi there, Create your first view as normal:

        CSingleDocTemplate\* pDocTemplate;
        pDocTemplate = new CSingleDocTemplate(
        	IDR\_MAINFRAME,
        	RUNTIME\_CLASS(CMyDoc),
        	RUNTIME\_CLASS(CMainFrame),       // main SDI frame window
        	RUNTIME\_CLASS(CForm1));
        AddDocTemplate(pDocTemplate);
        

        Then, still in InitInstance, store an array (or obarray) to hold the other views...

        CView\* pActiveView = ((CFrameWnd\*) m\_pMainWnd)->GetActiveView();
        
        m\_pViews\[0\] = pActiveView;
        m\_pViews\[1\] = (CView\*) new CForm2;
        m\_pViews\[2\] = (CView\*) new CForm3;
        
        CDocument\* pCurrentDoc = ((CFrameWnd\*) m\_pMainWnd)->GetActiveDocument();
        
        // Initialize a CCreateContext to point to the active document.
        // With this context, the new view is added to the document
        // when the view is created in CView::OnCreate().
        CCreateContext newContext;
        newContext.m\_pNewViewClass = NULL;
        newContext.m\_pNewDocTemplate = NULL;
        newContext.m\_pLastView = NULL;
        newContext.m\_pCurrentFrame = NULL;
        newContext.m\_pCurrentDoc = pCurrentDoc;
        
        // The ID of the initial active view is AFX\_IDW\_PANE\_FIRST.
        // Incrementing this value by one for additional views works
        // in the standard document/view case but the technique cannot
        // be extended for the CSplitterWnd case.
        UINT viewID\[3\];
        viewID\[1\] = AFX\_IDW\_PANE\_FIRST + 1;
        viewID\[2\] = AFX\_IDW\_PANE\_FIRST + 2;    
        CRect rect(0, 0, 0, 0); // gets resized later
        
        // Need to cast pointers to have correct Create functions called
        for ( int nView=1; nView<NUMVIEWS; nView++ )
        {
            // Create the new view. In this example, the view persists for
            // the life of the application. The application automatically
            // deletes the view when the application is closed.
            m\_pViews\[nView\]->Create(NULL, NULL,
        			(AFX\_WS\_DEFAULT\_VIEW & ~WS\_VISIBLE),
        		// views are created with the style of AFX\_WS\_DEFAULT\_VIEW
        		// In MFC 4.0, this is (WS\_BORDER | WS\_VISIBLE | WS\_CHILD)
        	                rect, m\_pMainWnd,
        			viewID\[nView\], &newContext);
        }
        
        // When a document template creates a view, the WM\_INITIALUPDATE
        // message is sent automatically. However, this code must
        // explicitly send the message to the extra views as follows.
        ((CForm2\*)m\_pViews\[1\])->OnInitialUpdate();
        ((CForm3\*)m\_pViews\[2\])->OnInitialUpdate();
        

        Here's a function to help you switch dynamically between the views...

        CView* CMyApp::SwitchView( UINT n

        D 2 Replies Last reply
        0
        • D dart13

          I need to put three different forms in SDI application's CView window so that user can choose which view he wants to see. If I make 3 different CFormView classes how do I select one to be shown in a CView window?

          D Offline
          D Offline
          Dreamz
          wrote on last edited by
          #4

          I gave the link for MDI :doh: Think same thing will work for this also ;)

          1 Reply Last reply
          0
          • A Andrew Quinn AUS

            Hi there, Create your first view as normal:

            CSingleDocTemplate\* pDocTemplate;
            pDocTemplate = new CSingleDocTemplate(
            	IDR\_MAINFRAME,
            	RUNTIME\_CLASS(CMyDoc),
            	RUNTIME\_CLASS(CMainFrame),       // main SDI frame window
            	RUNTIME\_CLASS(CForm1));
            AddDocTemplate(pDocTemplate);
            

            Then, still in InitInstance, store an array (or obarray) to hold the other views...

            CView\* pActiveView = ((CFrameWnd\*) m\_pMainWnd)->GetActiveView();
            
            m\_pViews\[0\] = pActiveView;
            m\_pViews\[1\] = (CView\*) new CForm2;
            m\_pViews\[2\] = (CView\*) new CForm3;
            
            CDocument\* pCurrentDoc = ((CFrameWnd\*) m\_pMainWnd)->GetActiveDocument();
            
            // Initialize a CCreateContext to point to the active document.
            // With this context, the new view is added to the document
            // when the view is created in CView::OnCreate().
            CCreateContext newContext;
            newContext.m\_pNewViewClass = NULL;
            newContext.m\_pNewDocTemplate = NULL;
            newContext.m\_pLastView = NULL;
            newContext.m\_pCurrentFrame = NULL;
            newContext.m\_pCurrentDoc = pCurrentDoc;
            
            // The ID of the initial active view is AFX\_IDW\_PANE\_FIRST.
            // Incrementing this value by one for additional views works
            // in the standard document/view case but the technique cannot
            // be extended for the CSplitterWnd case.
            UINT viewID\[3\];
            viewID\[1\] = AFX\_IDW\_PANE\_FIRST + 1;
            viewID\[2\] = AFX\_IDW\_PANE\_FIRST + 2;    
            CRect rect(0, 0, 0, 0); // gets resized later
            
            // Need to cast pointers to have correct Create functions called
            for ( int nView=1; nView<NUMVIEWS; nView++ )
            {
                // Create the new view. In this example, the view persists for
                // the life of the application. The application automatically
                // deletes the view when the application is closed.
                m\_pViews\[nView\]->Create(NULL, NULL,
            			(AFX\_WS\_DEFAULT\_VIEW & ~WS\_VISIBLE),
            		// views are created with the style of AFX\_WS\_DEFAULT\_VIEW
            		// In MFC 4.0, this is (WS\_BORDER | WS\_VISIBLE | WS\_CHILD)
            	                rect, m\_pMainWnd,
            			viewID\[nView\], &newContext);
            }
            
            // When a document template creates a view, the WM\_INITIALUPDATE
            // message is sent automatically. However, this code must
            // explicitly send the message to the extra views as follows.
            ((CForm2\*)m\_pViews\[1\])->OnInitialUpdate();
            ((CForm3\*)m\_pViews\[2\])->OnInitialUpdate();
            

            Here's a function to help you switch dynamically between the views...

            CView* CMyApp::SwitchView( UINT n

            D Offline
            D Offline
            dart13
            wrote on last edited by
            #5

            Thanx for your help. I'll try to implement this today and let you know how it works.

            1 Reply Last reply
            0
            • D Dreamz

              http://www.codeproject.com/useritems/MultiViewsMFC\_MDI.asp Hope this article will help you .

              D Offline
              D Offline
              dart13
              wrote on last edited by
              #6

              thanx. hopefully, it will help.

              1 Reply Last reply
              0
              • A Andrew Quinn AUS

                Hi there, Create your first view as normal:

                CSingleDocTemplate\* pDocTemplate;
                pDocTemplate = new CSingleDocTemplate(
                	IDR\_MAINFRAME,
                	RUNTIME\_CLASS(CMyDoc),
                	RUNTIME\_CLASS(CMainFrame),       // main SDI frame window
                	RUNTIME\_CLASS(CForm1));
                AddDocTemplate(pDocTemplate);
                

                Then, still in InitInstance, store an array (or obarray) to hold the other views...

                CView\* pActiveView = ((CFrameWnd\*) m\_pMainWnd)->GetActiveView();
                
                m\_pViews\[0\] = pActiveView;
                m\_pViews\[1\] = (CView\*) new CForm2;
                m\_pViews\[2\] = (CView\*) new CForm3;
                
                CDocument\* pCurrentDoc = ((CFrameWnd\*) m\_pMainWnd)->GetActiveDocument();
                
                // Initialize a CCreateContext to point to the active document.
                // With this context, the new view is added to the document
                // when the view is created in CView::OnCreate().
                CCreateContext newContext;
                newContext.m\_pNewViewClass = NULL;
                newContext.m\_pNewDocTemplate = NULL;
                newContext.m\_pLastView = NULL;
                newContext.m\_pCurrentFrame = NULL;
                newContext.m\_pCurrentDoc = pCurrentDoc;
                
                // The ID of the initial active view is AFX\_IDW\_PANE\_FIRST.
                // Incrementing this value by one for additional views works
                // in the standard document/view case but the technique cannot
                // be extended for the CSplitterWnd case.
                UINT viewID\[3\];
                viewID\[1\] = AFX\_IDW\_PANE\_FIRST + 1;
                viewID\[2\] = AFX\_IDW\_PANE\_FIRST + 2;    
                CRect rect(0, 0, 0, 0); // gets resized later
                
                // Need to cast pointers to have correct Create functions called
                for ( int nView=1; nView<NUMVIEWS; nView++ )
                {
                    // Create the new view. In this example, the view persists for
                    // the life of the application. The application automatically
                    // deletes the view when the application is closed.
                    m\_pViews\[nView\]->Create(NULL, NULL,
                			(AFX\_WS\_DEFAULT\_VIEW & ~WS\_VISIBLE),
                		// views are created with the style of AFX\_WS\_DEFAULT\_VIEW
                		// In MFC 4.0, this is (WS\_BORDER | WS\_VISIBLE | WS\_CHILD)
                	                rect, m\_pMainWnd,
                			viewID\[nView\], &newContext);
                }
                
                // When a document template creates a view, the WM\_INITIALUPDATE
                // message is sent automatically. However, this code must
                // explicitly send the message to the extra views as follows.
                ((CForm2\*)m\_pViews\[1\])->OnInitialUpdate();
                ((CForm3\*)m\_pViews\[2\])->OnInitialUpdate();
                

                Here's a function to help you switch dynamically between the views...

                CView* CMyApp::SwitchView( UINT n

                D Offline
                D Offline
                dart13
                wrote on last edited by
                #7

                I just wanted to thank you again for your help. I applied the code you gave me in the application and it works. I had some problems with the SwitchView(), particulary this line ((CFormView*)pNewView)->ResizeParentToFit(FALSE); because it would produce strange windows sizes for the first 7-8 calls of SwitchView(), and then it would work fine. So I put ((CFrameWnd*) m_pMainWnd)->RecalcLayout(); instead. It works fine now. And I'd like to ask you one more question. I have a button "quit" on one of CFormView's forms. What should I put in OnButtonQuit() message handler, so that the application exits after the button is pressed?

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups