bool CMainFrame::MakeWave(const WAVEGEN_PARMS& Parms) { POSITION pos = theApp.GetFirstDocTemplatePosition(); CDocTemplate *pTpl = theApp.GetNextDocTemplate(pos); CWaveShopDoc *pDoc = DYNAMIC_DOWNCAST(CWaveShopDoc, pTpl->CreateNewDocument()); if (pDoc == NULL) return(FALSE); bool retc, canceled; { CProgressDlg ProgDlg; if (!ProgDlg.Create()) // create progress dialog AfxThrowResourceException(); ProgDlg.SetWindowText(LDS(IDS_MAIN_GENERATING_AUDIO)); retc = CWaveGenDlg::MakeWave(Parms, pDoc->m_Wave, &ProgDlg); canceled = ProgDlg.Canceled(); } // destroy progress dialog if (!retc) { // if generation failed if (!canceled) // if user canceled AfxMessageBox(IDS_MAIN_CANT_MAKE_WAVE); return(FALSE); } CDocument *pEmptyDoc = pTpl->OpenDocumentFile(NULL); // create new view if (pEmptyDoc == NULL || m_View == NULL) return(FALSE); CString title = pEmptyDoc->GetTitle(); pEmptyDoc->RemoveView(m_View); // remove empty document from view pDoc->SetTitle(title); // copy empty document's title to generated document pDoc->AddView(m_View); // add generated document to view m_View->OnInitialUpdate(); OnActivateView(m_View); // view is still linked to empty document's undo manager; must relink m_View->SetUndoManager(&pDoc->m_UndoMgr); // link view to undo manager pDoc->m_UndoMgr.SetRoot(m_View); // link undo manager to view return(TRUE); }
void CMainFrame::OnViewChange(UINT nCmdID) // There is an ON_COMMAND_RANGE message map entry associated with // OnViewChange: // ON_COMMAND_RANGE(ID_VIEW_CHANGE1, ID_VIEW_CHANGE2, &OnViewChange) { CView* pViewAdd; CView* pViewRemove; CDocument* pDoc = GetActiveDocument(); // cvView1 and cvView2 are enum members defined in my CMainFrame class if((nCmdID == ID_VIEW_CHANGE1) && (m_currentView == cvView1)) return; if((nCmdID == ID_VIEW_CHANGE2) && (m_currentView == cvView2)) return; if (nCmdID == ID_VIEW_CHANGE2) { if (m_pView2 == NULL) { m_pView1 = GetActiveView(); m_pView2 = new CMyView2; //Note that if OnSize has been overridden in CMyView2 //and GetDocument() is used in this override it can //cause assertions and, if the assertions are ignored, //cause access violation. m_pView2->Create(NULL, NULL, AFX_WS_DEFAULT_VIEW, rectDefault, this, AFX_IDW_PANE_FIRST + 1, NULL); } pViewAdd = m_pView2; pViewRemove = m_pView1; m_currentView = cvView2; } else { pViewAdd = m_pView1; pViewRemove = m_pView2; m_currentView = cvView1; } // Set the child i.d. of the active view to AFX_IDW_PANE_FIRST, // so that CFrameWnd::RecalcLayout will allocate to this // "first pane" that portion of the frame window's client area // not allocated to control bars. Set the child i.d. of the // other view to anything other than AFX_IDW_PANE_FIRST; this // examples switches the child id's of the two views. int nSwitchChildID = pViewAdd->GetDlgCtrlID(); pViewAdd->SetDlgCtrlID(AFX_IDW_PANE_FIRST); pViewRemove->SetDlgCtrlID(nSwitchChildID); // Show the newly active view and hide the inactive view. pViewAdd->ShowWindow(SW_SHOW); pViewRemove->ShowWindow(SW_HIDE); // Connect the newly active view to the document, and // disconnect the inactive view. pDoc->AddView(pViewAdd); pDoc->RemoveView(pViewRemove); SetActiveView(pViewAdd); RecalcLayout(); }