| Tenouk C & C++ | MFC Home | Splitter Windows & Multiple Views 3 | Splitter Windows & Multiple Views 5 | Download | Site Index |


 

 

 

 

 

 

Module 14c:

Splitter Windows and Multiple Views 4

 

 

 

 

 

 

 

This is a continuation from the previous module... 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:

 

  1. The MYMFC21D Example: A Multiple View Class MDI Application

  2. The Steps

  3. Resource Requirements

 

 

The MYMFC21D Project Example: A Multiple View Class MDI Application

 

The final example, MYMFC21D, uses the previous document and view classes to create a multiple view class MDI application without a splitter window. The logic is different from the logic in the other multiple view class applications. This time the action takes place in the application class in addition to the main frame class. As you study MYMFC21D, you'll gain more insight into the use of CDocTemplate objects.

This example was generated with the AppWizard Context-Sensitive Help option (step 4). In next Module, Module 15, you'll activate the context-sensitive help capability. If you're starting from scratch, use AppWizard to generate an ordinary MDI application with one of the view classes. Then add the second view class to the project and modify the application class files and main frame class files as described in the following sections.

 

 

The Steps

 

As usual, use AppWizard to create a MDI application. Follow the shown steps.

 

MYMFC21D AppWizard step 1 of 6.

 

Figure 30: MYMFC21D AppWizard step 1 of 6.

 

MYMFC21D step 4 of 6 AppWizard. Don’t forget to tick the Context-sensitive Help; this option will be used in the next module’s project.

 

Figure 31: MYMFC21D step 4 of 6 AppWizard. Don’t forget to tick the Context-sensitive Help; this option will be used in the next module’s project.

 

MYMFC21D step 6 of 6 AppWizard, renaming the view class, their related files and using CScrollView as the view base class.

 

Figure 32: MYMFC21D step 6 of 6 AppWizard, renaming the view class, their related files and using CScrollView as the view base class.

 

MYMFC21D step 6 of 6 AppWizard, renaming the document class and their related files.

 

Figure 33: MYMFC21D step 6 of 6 AppWizard, renaming the document class and their related files.

 

MYMFC21D project summary.

 

Figure 34: MYMFC21D project summary.

 

Note that this is an MDI application. Add a CStringArray data member to the CPoemDoc class. Edit the PoemDoc.h header file or use ClassView.

 

public:

    CStringArray m_stringArray;

 

Adding a CStringArray data member to the CPoemDoc class using ClassView.

 

Figure 35: Adding a CStringArray data member to the CPoemDoc class using ClassView.

 

Entering the member variable type and name.

 

Figure 36: Entering the member variable type and name.

 

Visual C++ code segment

 

Listing 29.

 

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:

 

private:

    CRect m_rectPrint;

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

--------------------------------------------------------------------------------------

Adding a CRect data member to the CStringView class.

 

Figure 37: Adding a CRect data member to the CStringView class.

 

Entering the member variable type and name.

 

Figure 38: Entering the member variable type and name.

 

Visual C++ code segment

 

Listing 30.

 

Edit three CPoemDoc member functions in the file PoemDoc.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:

 

BOOL CPoemDoc::OnNewDocument()

{

    if (!CDocument::OnNewDocument())

        return FALSE;

 

    m_stringArray.SetSize(10);

    m_stringArray[0] = "The pennycandystore beyond the El";

    m_stringArray[1] = "is where I first";

    m_stringArray[2] = "                  fell in love";

    m_stringArray[3] = "                      with unreality";

    m_stringArray[4] = "Jellybeans glowed in the semi-gloom";

    m_stringArray[5] = "of that september afternoon";

    m_stringArray[6] = "A cat upon the counter moved among";

    m_stringArray[7] = "                     the licorice sticks";

    m_stringArray[8] = "                  and tootsie rolls";

    m_stringArray[9] = "           and Oh Boy Gum";

 

    return TRUE;

}

 

Visual C++ code segment

 

Listing 31.

 

 

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:

 

void CPoemDoc::DeleteContents()

{

    // called before OnNewDocument() and when document is closed

    m_stringArray.RemoveAll();

}

 

Similar to the previous example, adding the RemoveAll() function.

 

Figure 39: Similar to the previous example, adding the RemoveAll() function.

 

Visual C++ code segment

 

Listing 32.

 

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 CPoemDoc::Serialize(CArchive& ar)

{

    m_stringArray.Serialize(ar);

}

 

Visual C++ code segment

 

Listing 33.

 

Edit the OnInitialUpdate() function in StringView.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 boldface code:

 

void CStringView::OnInitialUpdate()

{

    CScrollView::OnInitialUpdate();

    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);

}

 

Visual C++ code segment

 

Listing 34.

 

Edit the OnDraw() function in StringView.cpp. The OnDraw() function of class CStringView 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 CStringView::OnDraw(CDC* pDC)

{

    int        i, j, nHeight;

    CString    str;

    CFont      font;

    TEXTMETRIC tm;

 

    CPoemDoc* 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++)

    {

        str.Format("%02d", i);

        pDC->TextOut(i * 1440, 0, str);

    }

    j = -(m_rectPrint.Height() / 1440);

    for (i = 0; i <= j; i++)

    {

        str.Format("%02d", 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,

                    OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,

                    DEFAULT_QUALITY, DEFAULT_PITCH | FF_ROMAN, "Times New Roman");

    CFont* pOldFont = (CFont*) pDC->SelectObject(&font);

    pDC->GetTextMetrics(&tm);

    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]);

    }

    pDC->SelectObject(pOldFont);

    TRACE("LOGPIXELSX = %d, LOGPIXELSY = %d\n", pDC->GetDeviceCaps(LOGPIXELSX),

          pDC->GetDeviceCaps(LOGPIXELSY));

    TRACE("HORZSIZE = %d, VERTSIZE = %d\n", pDC->GetDeviceCaps(HORZSIZE),

          pDC->GetDeviceCaps(VERTSIZE));

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Visual C++ code segment

 

Listing 35.

 

Edit the OnPreparePrinting() function in StringView.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 CStringView::OnPreparePrinting(CPrintInfo* pInfo)

{

    pInfo->SetMaxPage(1);

    return DoPreparePrinting(pInfo);

}

 

Visual C++ code segment

 

Listing 36.

 

Edit the constructor in StringView.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:

 

CStringView::CStringView() : m_rectPrint(0, 0, 11520, -15120)

{

}

 

Visual C++ code segment

 

Listing 37.

 

Build and test the application.

 

MYMFC21D program output, the MDI.

 

Figure 40: MYMFC21D program output, the MDI.

 

 

Continue on next module...part 5.

 

 

 

 

Further reading and digging:

  1. MSDN MFC 7.0 class library online documentation.

  2. MSDN MFC 9.0 class library online documentation - latest version.

  3. MSDN Library

  4. Windows data type.

  5. Win32 programming Tutorial.

  6. The best of C/C++, MFC, Windows and other related books.

  7. Unicode and Multibyte character set: Story and program examples.

 

 

 

 

 


 

| Tenouk C & C++ | MFC Home | Splitter Windows & Multiple Views 3 | Splitter Windows & Multiple Views 5 | Download | Site Index |