Different views in SDI
-
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?
http://www.codeproject.com/useritems/MultiViewsMFC\_MDI.asp Hope this article will help you .
-
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?
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
-
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?
-
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
-
http://www.codeproject.com/useritems/MultiViewsMFC\_MDI.asp Hope this article will help you .
-
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
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?