Program examples compiled using Visual C++ 6.0 (MFC 6.0) compiler on Windows XP Pro machine with Service Pack 2. Topics and sub topics for this Tutorial are listed below:
Splitter Windows and Multiple Views
Except for the MYMFC18 example, each program you've seen so far in this book has had only one view attached to a document. If you've used a Microsoft Windows-based word processor, you know that it's convenient to have two windows open simultaneously on various parts of a document. Both windows might contain normal views, or one window might contain a page layout view and another might contain an outline view.
With the application framework, you can use a splitter window or multiple MDI child windows to display multiple views. You'll learn about both presentation options here, and you'll see that it's easy to make multiple view objects of the same view class (the normal view) in both cases. It's slightly more difficult, however, to use two or more view classes in the same application (say, the outline view and the page layout view).
This module emphasizes the selection and presentation of multiple views. The examples depend on a document with data initialized in the OnNewDocument() function. Look back now to Module 10 for a review of document-view communication.
The Splitter Window
A splitter window appears as a special type of frame window that holds several views in panes. The application can split the window on creation, or the user can split the window by choosing a menu command or by dragging a splitter box on the window's scroll bar. After the window has been split, the user can move the splitter bars with the mouse to adjust the relative sizes of the panes. Splitter windows can be used in both SDI and MDI applications. You can see examples of splitter windows in this module.
An object of class CSplitterWnd represents the splitter window. As far as Windows is concerned, a CSplitterWnd object is an actual window that fully occupies the frame window (CFrameWnd or CMDIChildWnd) client area. The view windows occupy the splitter window pane areas. The splitter window does not take part in the command dispatch mechanism. The active view window (in a splitter pane) is connected directly to its frame window.
When you combine multiview presentation methods with application models, you get a number of permutations. Here are some of them:
MDI application with no splitter windows, multiple view classes. A small change to the standard MDI application allows the use of multiple views. As example MYMFC21D shows, all that's necessary is to add a menu item and a handler function for each additional view class available.
MDI application with splitter child windows. This case is covered thoroughly in the online documentation. The SCRIBBLE example illustrates the splitting of an MDI child window.
Dynamic and Static Splitter Windows
A dynamic splitter window allows the user to split the window at any time by choosing a menu item or by dragging a splitter box located on the scroll bar. The panes in a dynamic splitter window generally use the same view class. The top left pane is initialized to a particular view when the splitter window is created. In a dynamic splitter window, scroll bars are shared among the views. In a window with a single horizontal split, for example, the bottom scroll bar controls both views. A dynamic splitter application starts with a single view object. When the user splits the frame, other view objects are constructed. When the user un-splits the frame, view objects are destroyed.
The panes of a static splitter window are defined when the window is first created and they cannot be changed. The user can move the bars but cannot un-split or re-split the window. Static splitter windows can accommodate multiple view classes, with the configuration set at creation time. In a static splitter window, each pane has separate scroll bars. In a static splitter window application, all view objects are constructed when the frame is constructed and they are all destroyed when the frame is destroyed.
The MYMFC21 Example: A Single View Class SDI Dynamic Splitter
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 MYMFC19. 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.
Figure 1: MYMFC21 step 4 of 6 AppWizard, the Advanced options settings.
Figure 2: MYMFC21 step 6 of 6 AppWizard, using CScrollView for the view base class.
Figure 3: MYMFC21 project summary.
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
Build and run MYMFC21 program. When AppWizard generates an application with a splitter frame, it includes a Split option in the project's View menu when you build and run the program. The ID_WINDOW_SPLIT command ID is mapped in the CView class within the MFC library.
Figure 4: MYMFC21 program output with split windows without any coding.
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:
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*/,
return m_wndSplitter.Create( this,
2, 2, // TODO: adjust the number of rows, columns
CSize(10, 10), // TODO: adjust the minimum pane size
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.
The MYMFC20 steps
Before we see the action, let put in the MYMFC20 steps in this example. Add a CStringArray data member to the CMymfc21Doc class. Edit the Mymfc21Doc.h header file or use ClassView.
Figure 5: Adding member variable to CMymfc21Doc class using ClassView.
Figure 6: Entering the member variable’s type and name.
The document data is stored in a string array. The MFC library CStringArray class holds an array of CString objects, accessible by a zero-based subscript. You need not set a maximum dimension in the declaration because the array is dynamic.
Add a CRect data member to the CStringView class. Edit the StringView.h header file or use ClassView:
Figure 7: Using ClassView to add a CRect data member to the CStringView class.
Figure 8: Entering the member variable type and name.
Edit three CMymfc21Doc member functions in the file Mymfc21Doc.cpp. AppWizard generated skeleton OnNewDocument() and Serialize() functions, but we'll have to use ClassWizard to override the DeleteContents() function. We'll initialize the poem document in the overridden OnNewDocument() function. DeleteContents() is called in CDocument::OnNewDocument, so by calling the base class function first we're sure the poem won't be deleted. The text, by the way, is an excerpt from the twentieth poem in Lawrence Ferlinghetti's book A Coney Island of the Mind. Type 10 lines of your choice. You can substitute another poem or maybe your favorite Win32 function description. Add the following code:
m_stringArray = "The pennycandystore beyond the El";
m_stringArray = "is where I first";
m_stringArray = " fell in love";
m_stringArray = " with unreality";
m_stringArray = "Jellybeans glowed in the semi-gloom";
m_stringArray = "of that september afternoon";
m_stringArray = "A cat upon the counter moved among";
m_stringArray = " the licorice sticks";
m_stringArray = " and tootsie rolls";
m_stringArray = " and Oh Boy Gum";
The CStringArray class supports dynamic arrays, but here we're using the m_stringArray object as though it were a static array of 10 elements. The application framework calls the document's virtual DeleteContents() function when it closes the document; this action deletes the strings in the array. A CStringArray contains actual objects, and a CObArray contains pointers to objects. This distinction is important when it's time to delete the array elements. Here the RemoveAll() function actually deletes the string objects:
// called before OnNewDocument() and when document is closed
Figure 10: Adding the RemoveAll() function to the CMymfc21Doc class.
Serialization isn't important in this example, but the following function illustrates how easy it is to serialize strings. The application framework calls the DeleteContents() function before loading from the archive, so you don't have to worry about emptying the array. Add the following code:
void CMymfc21Doc::Serialize(CArchive& ar)
Edit the OnInitialUpdate() function in mymfc21View.cpp. You must override the function for all classes derived from CScrollView. This function's job is to set the logical window size and the mapping mode. Add the following code:
CSize sizeTotal(m_rectPrint.Width(), -m_rectPrint.Height());
CSize sizePage(sizeTotal.cx / 2, sizeTotal.cy / 2); // page scroll
CSize sizeLine(sizeTotal.cx / 100, sizeTotal.cy / 100); // line scroll
SetScrollSizes(MM_TWIPS, sizeTotal, sizePage, sizeLine);
Edit the OnDraw() function in mymfc21View.cpp. The OnDraw() function of class CMymfc21View draws on both the display and the printer. In addition to displaying the poem text lines in 10-point roman font, it draws a border around the printable area and a crude ruler along the top and left margins. OnDraw() assumes the MM_TWIPS mapping mode, in which 1 inch = 1440 units. Add the code shown below.
void CMymfc21View::OnDraw(CDC* pDC)
int i, j, nHeight;
CMymfc21Doc* pDoc = GetDocument();
// Draw a border - slightly smaller to avoid truncation
pDC->Rectangle(m_rectPrint + CRect(0, 0, -20, 20));
// Draw horizontal and vertical rulers
j = m_rectPrint.Width() / 1440;
for (i = 0; i <= j; i++)
pDC->TextOut(i * 1440, 0, str);
j = -(m_rectPrint.Height() / 1440);
for (i = 0; i <= j; i++)
pDC->TextOut(0, -i * 1440, str);
// Print the poem 0.5 inch down and over;
// use 10-point roman font
font.CreateFont(-200, 0, 0, 0, 400, FALSE, FALSE, 0, ANSI_CHARSET,
DEFAULT_QUALITY, DEFAULT_PITCH | FF_ROMAN, "Times New Roman");
CFont* pOldFont = (CFont*) pDC->SelectObject(&font);
nHeight = tm.tmHeight + tm.tmExternalLeading;
TRACE("font height = %d, internal leading = %d\n", nHeight, tm.tmInternalLeading);
j = pDoc->m_stringArray.GetSize();
for (i = 0; i < j; i++)
pDC->TextOut(720, -i * nHeight - 720, pDoc->m_stringArray[i]);
TRACE("LOGPIXELSX = %d, LOGPIXELSY = %d\n", pDC->GetDeviceCaps(LOGPIXELSX),
TRACE("HORZSIZE = %d, VERTSIZE = %d\n", pDC->GetDeviceCaps(HORZSIZE),
Edit the OnPreparePrinting() function in mymfc21View.cpp. This function sets the maximum number of pages in the print job. This example has only one page. It's absolutely necessary to call the base class DoPreparePrinting() function in your overridden OnPreparePrinting() function. Add the following code:
BOOL CMymfc21View::OnPreparePrinting(CPrintInfo* pInfo)
Edit the constructor in mymfc21View.cpp. The initial value of the print rectangle should be 8-by-15 inches, expressed in twips (1 inch = 1440 twips). Add the following code:
CMymfc21View::CMymfc21View() : m_rectPrint(0, 0, 11520, -15120)
// TODO: add construction code here
Testing the MYMFC21 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 11 shows a typical single view window with a four-way split. Multiple views share the scroll bars.
Figure 11: MYMFC21 output, a single view window with a four-way split.
Continue on next module...part 2.
Further reading and digging:
MSDN What's New (MFC Feature Pack) - feature pack.
DCOM at MSDN.
COM+ at MSDN.
COM at MSDN.