創建一個新的視圖窗口來顯示已打開的文檔中的內容

當調用CMultiDocTemplate::OpenDocumentFile,爲第二個參數傳遞FALSE,此時將不會顯示視圖窗口,但是一個文檔確實已被打開了.

BOOL CMyApp::InitInstance()

{

   

    CMultiDocTemplate* pDocTemplate;

       pDocTemplate = new CMultiDocTemplate(

              IDR_MAINFRAME,

              RUNTIME_CLASS(CSinoDoc),

              RUNTIME_CLASS(CChildFrame), // custom MDI child frame

              RUNTIME_CLASS(CSinoView));

       AddDocTemplate(pDocTemplate);

       m_pMyDocTemplate = pDocTemplate;

   

}

 

void CMainFrame::OnFileOpen()

{

m_pDoc=theApp.m_pMyDocTemplate->OpenDocumentFile(

dlg.GetPathName(), FALSE);

       //把打開的文檔指針保存下來

}

可能在不久之後,你又需要視圖窗口來顯示這個已打開的文檔中的數據.

先跟蹤MFC源碼,看看CMultiDocTemplate:: OpenDocumentFile打開一個文檔的過程.

CDocument* CMultiDocTemplate::OpenDocumentFile(LPCTSTR lpszPathName,

       BOOL bMakeVisible)

{

       CDocument* pDocument = CreateNewDocument();

      

       CFrameWnd* pFrame = CreateNewFrame(pDocument, NULL);

      

       InitialUpdateFrame(pFrame, pDocument, bMakeVisible);

       return pDocument;

}

 

CFrameWnd* CDocTemplate::CreateNewFrame(CDocument* pDoc, CFrameWnd* pOther)

{

CCreateContext context;

       context.m_pCurrentFrame = pOther;

       context.m_pCurrentDoc = pDoc;

       context.m_pNewViewClass = m_pViewClass;     // 用來創建視圖

       context.m_pNewDocTemplate = this;

       CFrameWnd* pFrame = (CFrameWnd*)m_pFrameClass->CreateObject();

pFrame->LoadFrame(m_nIDResource,

                     WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE,   // default frame styles

                     NULL, &context);

很簡單,用下面的代碼就可以創建一個新的視圖來顯示已打開的文檔中的數據.

void CMainFrame::OnShowData()

{

      

       CFrameWnd* pFrame = theApp.m_pMyDocTemplate->CreateNewFrame(m_pDoc, NULL);

      

       pFrame->InitialUpdateFrame(m_pDoc, TRUE); // 傳遞TRUE,打開視圖窗口

}

也許你希望用另一個完全不同的視圖來顯示這個文檔中的數據

void CMainFrame::OnShowDataAnother()

{

CCreateContext context;

CAnotherFrame* pFrame = new CAnotherFrame;

// 如果默認的構造函數是protected,那麼用下面的語句

//CAnotherFrame*pFrame= //(CAnotherFrame*)RUNTIME_CLASS(CAnotherFrame)->CreateObject();

// CAnotherFrame和下面的CAnotherView沒有加入文檔模板鏈表

context.m_pCurrentFrame = pFrame;

pFrame->m_pViewClass = RUNTIME_CLASS(CAnotherView);

pFrame->LoadFrame(IDR_MAINFRAME,WS_OVERLAPPEDWINDOW|       FWS_ADDTOTITLE, NULL, &context);

pFrame->SetTitle(_T(“Title”));

pFrame->InitialUpdateFrame(NULL, TRUE);

pFrame->GetActiveView()->m_pDoc = m_pDoc;

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章