Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Fast Track Visual C++ 6.0 Programming (Publisher: John Wiley & Sons, Inc.) Author(s): Steve Holzner ISBN: 0471312908 Publication Date: 09/01/98 Search this book:
Previous | Table of Contents | Next
And we also set up OnDraw():
    void CScrollMDIView::OnDraw(CDC* pDC)

    {
        CScrollMDIDoc* pDoc = GetDocument();
        ASSERT_VALID(pDoc);

        TEXTMETRIC tm;

        pDC->GetTextMetrics(&tm);

        for(int loop_index = 0; loop_index <= pDoc->number_lines; loop_index++){
            pDC->TextOut(0, loop_index * tm.tmHeight, pDoc->text[loop_index]);
        }

    }
As well as OnUpdate():
    void CScrollMDIView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)

    {
        CScrollView::OnUpdate(pSender, lHint, pHint);

        CClientDC dc(this);
        TEXTMETRIC tm;

        CScrollMDIDoc* pDoc = GetDocument();
        ASSERT_VALID(pDoc);

        dc.GetTextMetrics(&tm);

        dc.TextOut(0, (int) lHint * tm.tmHeight, pDoc->text[lHint]);

    }
With this complete, we’re ready to implement scrolling. #### Implementing Scrolling To be able to scroll through a document, we need to know its size on the screen. To store the document’s size (in pixels), we add a new member to the document in ScrollMDIDoc.h called m_size.
    class CScrollMDIDoc : public CDocument

    {
    protected: // create from serialization only
        CScrollMDIDoc();
        DECLARE_DYNCREATE(CScrollMDIDoc)
            .
            .
            .
    protected:
        CSize m_size;                     ⇐
            .
            .
            .
Making the Document’s Size Publicly Accessible We’re using 800 × 800 pixels only as an example here. By using the TEXTMETRIC structure and the GetTextExtent() function, you can determine the exact size of your document, and it’s preferable to do so, since the scrollbar thumbs (the knobs you move in the scroll bar) are automatically sized to indicate the document size. This object is of the CSize class and has two members, cx and cy, to store the two dimensions of the document. We initialize this size to 800 × 800 pixels in the document’s constructor as follows:
    CScrollMDIDoc::CScrollMDIDoc()

    {
        number_lines = 0;
        m_size = CSize(800, 800);                 ⇐

    }
You may have noticed that we made m_size a protected member of the CScrollMDIDoc class. We did so because m_size is internal data that no other part of the program should be able to change. However, other parts of the program will need to know the document’s size, so we add an access function, GetDocumentSize(), that returns that size but does not allow any other part of the program to change m_size directly.
    class CScrollMDIDoc : public CDocument

    {
    protected: // create from serialization only
        CScrollMDIDoc();
        DECLARE_DYNCREATE(CScrollMDIDoc)

    // Attributes
    public:
        int number_lines;
        CString text[1000];

    // Operations
    public:
        CSize GetDocumentSize() {return m_size;}           ⇐
Once we’ve given the document a size, we pass that size on to the CScrollView class. #### Setting the Document’s Size We install the document’s size in the view class’s OnInitialUpdate() function, which AppWizard has already written for us:
    void CScrollMDIView::OnInitialUpdate()

    {
        CScrollView::OnInitialUpdate();

        CSize sizeTotal;
        //TODO: calculate the total size of this view
        sizeTotal.cx = sizeTotal.cy = 100;

        SetScrollSizes(MM_TEXT, sizeTotal);

    }
Here, the program sets the size of the document to 100 × 100 pixels as a default and then passes that size to CScrollView with the SetScrollSizes() so the scrollbars can be maintained appropriately. However, we can use the new GetDocumentSize() function to set the true size of the document this way:
    void CScrollMDIView::OnInitialUpdate()

    {
        CScrollView::OnInitialUpdate();

        CSize sizeTotal;

        CScrollMDIDoc* pDoc = GetDocument();                  ⇐
Resetting the Document’s Size If your document changes size in the course of the editing cycle, just call SetScrollSizes() again to update the size stored in the CScrollView class.
        ASSERT_VALID(pDoc);                                 ⇐
        CSize DocumentSize = pDoc->GetDocumentSize();       ⇐

        sizeTotal.cx = DocumentSize.cx;                     ⇐
        sizeTotal.cy = DocumentSize.cy;                     ⇐
        SetScrollSizes(MM_TEXT, sizeTotal);
Table 2.1 Scroll Size Mapping Modes
Mapping Mode | Logical Unit | Positive y-axis Extends
--- | --- | ---
MM_TEXT | 1 pixel | Downward
MM_HIMETRIC | 0.01 mm | Upward
MM_TWIPS | 1/1440 in | Upward
MM_HIENGLISH | 0.001 in | Upward
MM_LOMETRIC | 0.1 mm | Upward
MM_LOENGLISH | 0.01 in | Upward
Figure 2.6 Scrolling document views. The first argument in our call to SetScrollSizes() is MM_TEXT, a mapping mode, which indicates that we want to use pixels as our document measurement. The possible mapping modes appear in Table 2.1. Now we’ve set the document size and let CScrollView know what that scroll size is. Run the program, as shown in Figure 2.6, and enter text in a new document with several views open, as also shown in Figure 2.6. After you’ve entered text, you can scroll the views independently, as also shown in Figure 2.6. We’ve not only let the user open several views into a document, we’ve let him or her move around in the document using those views. ScrollMDI is a success. ScrollMDIDoc.h and ScrollMDIDoc.cpp appear in Listing 2.3; ScrollMDIView.h and ScrollMDIView.cpp appear in Listing 2.4.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.