C++--在单文档的应用程序增加多个视图
在mfc的單文檔的應用程序(Application)設計時,一般是一種文檔(Documnet)對應一種視圖(View)?。然而在有些時候我需要一種文檔用多種視圖來展現,具體步驟如下:
我們有兩個假設
CMyWinApp是該程序的應用程序,并聲明和定義在MYWINAPP.H和MYWINAPP.CPP文件中.
CNewView是從CView上繼承的視圖類,?并聲明和定義在NEWVIEW.H and NEWVIEW.CPP文件中.
1.修改應用程序類(Application Class);
在MYWINAPP.H加入如下聲明
CView* m_pOldView;
CView* m_pNewView;
CView* SwitchView( );
?m_pOldView?和m_pNewView分別指向當前的視圖類(新建程序時自動創(chuàng)建的一個視圖類)和即將創(chuàng)建的新的視圖類,SwitchView函數用于相應用戶視圖切換操作,具體實現參見下面的步驟。
在MYWINAPP.CPP文件中加入
#include <AFXPRIV.H>
2.創(chuàng)建和修改新的視圖類 (View Class)
選擇菜單的“New Class”創(chuàng)建新的視圖類,并將構造函數和析構函數的訪問類型由protected改成public,這樣用于動態(tài)創(chuàng)建視圖。完成后保存文件。
3.創(chuàng)建和綁定新視圖(View)
為了創(chuàng)建和綁定新視圖,我們需要修改應用程序類的InitInstance函數,在ProcessShellCommand后面添加如下代碼并保存:
...
CView* pActiveView = ((CFrameWnd*) m_pMainWnd)->GetActiveView();
m_pOldView = pActiveView;
m_pNewView = (CView*) new CNewView;
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 = AFX_IDW_PANE_FIRST + 1;
CRect rect(0, 0, 0, 0); // Gets resized later.
// 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_pNewView->Create(NULL, "AnyWindowName", WS_CHILD, rect, m_pMainWnd, viewID, &newContext);
// When a document template creates a view, the WM_INITIALUPDATE
// message is sent automatically. However, this code must
// explicitly send the message, as follows.
m_pNewView->SendMessage(WM_INITIALUPDATE, 0, 0);
...
4.實現切換功能函數(Switching Function)
在MYWINAPP.CPP文件中添加如下代碼,并保存。
CView* CMyWinApp::SwitchView( )
{
?? CView* pActiveView =? ((CFrameWnd*) m_pMainWnd)->GetActiveView();
?? CView* pNewView= NULL;
?? if(pActiveView == m_pOldView)
????? pNewView= m_pNewView;
?? else
????? pNewView= m_pOldView;
?? // Exchange view window IDs so RecalcLayout() works.
?? #ifndef _WIN32
?? UINT temp = ::GetWindowWord(pActiveView->m_hWnd, GWW_ID);
?? ::SetWindowWord(pActiveView->m_hWnd, GWW_ID, ::GetWindowWord(pNewView->m_hWnd, GWW_ID));
?? ::SetWindowWord(pNewView->m_hWnd, GWW_ID, temp);
?? #else
?? UINT temp = ::GetWindowLong(pActiveView->m_hWnd, GWL_ID);
?? ::SetWindowLong(pActiveView->m_hWnd, GWL_ID, ::GetWindowLong(pNewView->m_hWnd, GWL_ID));
?? ::SetWindowLong(pNewView->m_hWnd, GWL_ID, temp);
?? #endif
?? pActiveView->ShowWindow(SW_HIDE);
?? pNewView->ShowWindow(SW_SHOW);
?? ((CFrameWnd*) m_pMainWnd)->SetActiveView(pNewView);
?? ((CFrameWnd*) m_pMainWnd)->RecalcLayout();
?? pNewView->Invalidate();
?? return pActiveView;
}
5.調用SwitchView函數的實現
調用SwitchView函數可以通過增加菜單項來是實現。
void CMainFrame::OnForm1()
{
SwitchView();
}
URL:http://msdn2.microsoft.com/en-us/library/s199bks0(VS.80).aspx
轉載于:https://www.cnblogs.com/xiaomaohai/archive/2007/08/25/6157211.html
總結
以上是生活随笔為你收集整理的C++--在单文档的应用程序增加多个视图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 强制卸载域控制器命令
- 下一篇: 管理文库]我喜欢的10个经典管理学定律点