Adding Multiple Views to a Single Document(From MSDN)
来源:互联网 发布:mac 显示隐藏 编辑:程序博客网 时间:2024/05/17 18:26
Ina single-document interface (SDI) application created with theMicrosoft Foundation Class (MFC) Library, each document type isassociated with a single view type. In some cases, it is desirable tohave the ability to switch the current view of a document with a newview.
For additional procedures on implementing multiple views for a single document, see CDocument::AddView and the COLLECT MFC sample.
You can implement this functionality by adding a new CView-derived class and additional code for switching the views dynamically to an existing MFC application.
The steps are as follows:
Modify the Existing Application Class
Create and Modify the New View Class
Create and Attach the New View
Implement the Switching Function
Add Support for Switching the View
The remainder of this topic assumes the following:
The name of the CWinApp-derived object is CMyWinApp, and CMyWinApp is declared and defined in MYWINAPP.H and MYWINAPP.CPP.
CNewView is the name of the new CView-derived object, and CNewView is declared and defined in NEWVIEW.H and NEWVIEW.CPP.
For the application to switch betweenviews, you need to modify the application class by adding membervariables to store the views and a method to switch them. Add the following code to the declaration of CMyWinApp in MYWINAPP.H: The new member variables, m_pOldView and m_pNewView, point to the current view and the newly created one. The new method (SwitchView) switches the views when requested by the user. The body of the method is discussed later in this topic in Implement the Switching Function. The last modification to the application class requires including a new header file that defines a Windows message (WM_INITIALUPDATE) that is used in the switching function. Insert the following line in the include section of MYWINAPP.CPP: Save your changes and continue to the next step. Creating the new view class is made easy by using the New Class command available from Class View. The only requirement for this class is that it derives from CView. Add this new class to the application. For specific information on adding a new class to the project, see Adding a Class. Once you have added the class to the project, you need to change the accessibility of some view class members. Modify NEWVIEW.H by changing the access specifier from protected to publicfor the constructor and destructor. This allows the class to be createdand destroyed dynamically and to modify the view appearance before itis visible. Save your changes and continue to the next step. To create and attach the new view, you need to modify the InitInstance function of your application class. The modification adds new code that creates a new view object and then initializes both m_pOldView and m_pNewView with the two existing view objects. Because the new view is created within the InitInstancefunction, both the new and existing views persist for the lifetime ofthe application. However, the application could just as easily createthe new view dynamically. Insert this code after the call to ProcessShellCommand: Save your changes and continue to the next step. In the previous step, you added codethat created and initialized a new view object. The last major piece isto implement the switching method, SwitchView. At the end of the implementation file for your application class (MYWINAPP.CPP), add the following method definition: Save your changes and continue to the next step. The final step involves adding code that calls the SwitchViewmethod when the application needs to switch between views. This can bedone in several ways: by either adding a new menu item for the user tochoose or switching the views internally when certain conditions aremet. For more information on adding new menu items and command handler functions, see Handlers for Commands and Control Notifications.CView* m_pOldView;
CView* m_pNewView;
CView* SwitchView( );#include <AFXPRIV.H>
...
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);
...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;
}
- Adding Multiple Views to a Single Document(From MSDN)
- Adding Multiple Views to a Single Document
- Adding Multiple Views to a Single Document
- Adding Content to a Document
- How to build multiple targets from a single Xcode Project
- Adding a Single Task to a Queue
- Adding Form Fields to a MS Word Document
- APEX: Assign Multiple Schemas To A Single Workspace
- fixed: error C2784 from compiling adding an entry to a std::map
- Using GDI+ in a Single Document MFC Project to Rotate, Zoom, and Constraint an Image
- Geometric Context from a Single
- Replacing Multiple Patterns in a Single Pass
- Multiple DataNodes on a single machine
- Adding a Floating Panel to a Window
- [R] adding a title to a histogram
- Unable to find a single main class from the following candidates
- Caused by: java.lang.IllegalStateException: Unable to find a single main class from问题解决
- Unable to find a single main class from the following candidates
- Tag系统性能测试
- 期待已久的中间件:Microsoft .NET RIA Services (转贴)
- javascript解析json
- INFORMIX数据库dbaccess工具不识别终端问题解决方案
- 函数调用方式小结
- Adding Multiple Views to a Single Document(From MSDN)
- 多页签中的控件锚定问题解决办法。
- jQuery-toggle()与toggle(fn,fn)的用法
- GetWindowLong Function
- 2个用到数学归纳法的有意思的证明
- ThinkPHP对Ajax完全支持
- Windows Via C/C++ Part Ⅰ Chapter4: 进程(1)_概述
- 中国与印度软件开发之比较
- 为 PHP 添加 CURL 模块