Programming Microsoft Visual C++
In this example, the user can dynamically split the view into four panes with four separate view objects, all managed by a single view class. We'll use the document and the view code from EX19A. AppWizard lets you add a dynamic splitter window to a new application. Create an SDI project. Click the Advanced button in the AppWizard Step 4 dialog. Click on the Window Styles tab, and select Use Split Window as shown here.
When you check the Use Split Window check box, AppWizard adds code to your CMainFrame class. Of course, you could add the same code to the CMainFrame class of an existing application to add splitter capability.
Resources for Splitting
When AppWizard generates an application with a splitter frame, it includes a Split option in the project's View menu. The ID_WINDOW_SPLIT command ID is mapped in the CView class within the MFC library.
CMainFrame
The application's main frame window class needs a splitter window data member and a prototype for an overridden OnCreateClient function. Here are the additions that AppWizard makes to the MainFrm.h file:
protected: CSplitterWnd m_wndSplitter; public: virtual BOOL OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext);
The application framework calls the CFrameWnd::OnCreateClient virtual member function when the frame object is created. The base class version creates a single view window as specified by the document template. The AppWizard-generated OnCreateClient override shown here (in MainFrm.cpp) creates a splitter window instead, and the splitter window creates the first view:
BOOL CMainFrame::OnCreateClient( LPCREATESTRUCT /*lpcs*/, CCreateContext* pContext) { return m_wndSplitter.Create( this, 2, 2, // TODO: adjust the number of rows, columns CSize(10, 10), // TODO: adjust the minimum pane size pContext); }
The CSplitterWnd Create member function creates a dynamic splitter window, and the CSplitterWnd object knows the view class because its name is embedded in the CCreateContext structure that's passed as a parameter to Create.
The second and third Create parameters (2, 2) specify that the window can be split into a maximum of two rows and two columns. If you changed the parameters to (2, 1), you would allow only a single horizontal split. The parameters (1, 2) allow only a single vertical split. The CSize parameter specifies the minimum pane size.
Testing the EX20A Application
When the application starts, you can split the window by choosing Split from the View menu or by dragging the splitter boxes at the left and top of the scroll bars. Figure 20-1 shows a typical single view window with a four-way split. Multiple views share the scroll bars.
Figure 20-1. A single view window with a four-way split.
Категории