在主框架裏手工產生子框架、視和文檔的方法

【原先的做法】
MFC有很多的辦法能夠在主框架中產生子框架、視和文檔。當我們在產生一個工程的時候,
常見的辦法是在一個配置對話框中配置好對應的屬性,然後接下去就是打開一個文檔記錄
數據,打開該文檔的一個視,和相應的菜單、工具欄。而且複雜點的工程中有幾個不同的
文檔類,最起碼的也是有一個文檔對應幾種視的情況,所以在編寫MFC的時候,動態打開
子框架、視和文檔的是種常用的技術。
當我們用AppWizard這種垃圾生成一個App的框架的時候,通常會在App的InitInstance()裏
面new一個DocTemplate,然後用AddDocTemplate()的方式初始化一個模板,然後在
ProcessShellCommand()的方式執行子框架的打開。
我採用的辦法是在ParseCommandLine()後用cmdInfo.m_nShellCommand = cmdInfo.FileNothing
禁止打開新的文件,而是在這裏打開個配置對話框。接着就是在SetDlg.DoModal()後用如下的
代碼打開文檔,視和子框架:
// Creat new ChileFrame
theApp.m_pCurrentFrame = new CChildFrame;

// Creat new view and new doc
theApp.m_pCurrentView = new CEcmcView(VIEW_WAVE);
theApp.AddView(theApp.m_pCurrentView);
theApp.m_pCurrentDoc = new CEcmcDoc(VIEW_WAVE);
theApp.AddDoc(theApp.m_pCurrentDoc);

// Attach them
CCreateContext context;
context.m_pNewViewClass = RUNTIME_CLASS(CEcmcView);
context.m_pCurrentDoc = theApp.m_pCurrentDoc;

theApp.m_pCurrentFrame->LoadFrame(
IDR_EcmprojTYPE,
WS_OVERLAPPEDWINDOW,
this,
&context);
        
UINT ID_WAVEVIEW = AFX_IDS_PREVIEW_CLOSE +1;
CRect rect;
theApp.m_pCurrentView->Create(
NULL,
"New Wave Project",
WS_CHILD,
rect,
this,
ID_WAVEVIEW,
&context);

theApp.m_pCurrentFrame->ShowWindow(SW_SHOW);
theApp.m_pCurrentFrame->InitialUpdateFrame(NULL,TRUE);
這種辦法可以在任何的地方採用,只要預備好菜單資源和定義好視類、文檔類就好了
大家有什麼好的方法,也說說吧
按照上面的代碼,菜單並沒有更新成新的菜單,所以要添加下面代碼:
CMenu currentMenu;
currentMenu.LoadMenu(你要添加菜單的ID);
SetMenu(&currentMenu);

【修正】
上次我寫的這個主題的文章有些問題,在調試的時候,發現在MainFrame中改變產生的視的一個狀態
的時候,在視的成員函數處理的時候,並沒有體現出來。具體來講,我在CEcmcView::OnDraw()中根
據View中的一個成員變量來畫不同的圖的,我在MainFrame::OnProjNew()按鈕響應函數結束的時候改
變view的此成員變量,但是在CEcmcView::OnDraw()中並沒有反應出來,所以後來參照了MFC中CWinApp
::OnFileNew()的寫方法,實現了該類功能,不禁感慨,MFC畢竟寫的很好。
// Creat new doc, view, ChildFrame and attach them
CMultiDocTemplate* pDocTemplate;
pDocTemplate = new CMultiDocTemplate(
IDR_EcmprojTYPE,
RUNTIME_CLASS(CEcmcDoc),
RUNTIME_CLASS(CChildFrame), // custom MDI child frame
RUNTIME_CLASS(CEcmcWaveView));
theApp.AddDocTemplate(pDocTemplate);
CEcmcDoc* pDoc =  (CEcmcDoc*) pDocTemplate->OpenDocumentFile(NULL);
theApp.m_pCurrentDoc = pDoc;
POSITION posView = pDoc->GetFirstViewPosition();
CEcmcWaveView* pView = (CEcmcWaveView*)pDoc->GetNextView(posView);
theApp.m_pCurrentView = pView;
theApp.m_pCurrentFrame = (CChildFrame*)pView->GetParentFrame();

// link the doc and view into app's list
theApp.AddDoc(theApp.m_pCurrentDoc);
theApp.AddView(theApp.m_pCurrentView);

TRACE("-->Mainfrm.OnProjNew:the status of current view is %d/n",theApp.m_pCurrentView->m_currentStatus);

theApp.m_pCurrentFrame->ShowWindow(SW_SHOW);
        theApp.m_pCurrentFrame->InitialUpdateFrame(NULL,TRUE);
發佈了19 篇原創文章 · 獲贊 3 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章