home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
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

Bookmark It

Search this book:
 
Previous Table of Contents Next


Listing 2.2 MDIView.h and MDIView.cpp


// MDIView.h : interface of the CMDIView class
//
/////////////////////////////////////////////////////////////////////////////

#if 
!defined(AFX_MDIVIEW_H__81CAFB2F_9C82_11D1_887F_D42B07C10710__INCLUDED_)
#define AFX_MDIVIEW_H__81CAFB2F_9C82_11D1_887F_D42B07C10710__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class CMDIView : public CView
{
protected: // create from serialization only
    CMDIView();
    DECLARE_DYNCREATE(CMDIView)

// Attributes
public:
    CMDIDoc* GetDocument();

// Operations
public:

// Overrides
    // ClassWizard generated virtual function overrides

    //{{AFX_VIRTUAL(CMDIView)
    public:
    virtual void OnDraw(CDC* pDC);  // overridden to draw this view
    virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
    protected:
    virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
    virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
    virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
    virtual void OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint);
    //}}AFX_VIRTUAL

// Implementation
public:
    virtual ~CMDIView();
#ifdef _DEBUG
    virtual void AssertValid() const;
    virtual void Dump(CDumpContext& dc) const;
#endif

protected:

// Generated message map functions
protected:
    //{{AFX_MSG(CMDIView)
    afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
};

#ifndef _DEBUG  // debug version in MDIView.cpp
inline CMDIDoc* CMDIView::GetDocument()
   { return (CMDIDoc*)m_pDocument; }
#endif

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately 
before the previous line.



#endif // !defined(AFX_MDIVIEW_H__81CAFB2F_9C82_11D1_887F_D42B07C10710__INCLUDED_)



// MDIView.cpp : implementation of the CMDIView class
//

#include "stdafx.h"
#include "MDI.h"

#include "MDIDoc.h"
#include "MDIView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


/////////////////////////////////////////////////////////////////////////////
// CMDIView

IMPLEMENT_DYNCREATE(CMDIView, CView)

BEGIN_MESSAGE_MAP(CMDIView, CView)
    //{{AFX_MSG_MAP(CMDIView)
    ON_WM_CHAR()
    //}}AFX_MSG_MAP
    // Standard printing commands
    ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
    ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
    ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMDIView construction/destruction

CMDIView::CMDIView()
{


    // TODO: add construction code here

}

CMDIView::~CMDIView()
{
}

BOOL CMDIView::PreCreateWindow(CREATESTRUCT& cs)
{
    // TODO: Modify the Window class or styles here by modifying
    //  the CREATESTRUCT cs

    return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMDIView drawing

void CMDIView::OnDraw(CDC* pDC)
{
    CMDIDoc* 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]);
    }
    // TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CMDIView printing

BOOL CMDIView::OnPreparePrinting(CPrintInfo* pInfo)
{
    // default preparation

    return DoPreparePrinting(pInfo);
}

void CMDIView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
    // TODO: add extra initialization before printing
}

void CMDIView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
    // TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CMDIView diagnostics

#ifdef _DEBUG
void CMDIView::AssertValid() const
{
    CView::AssertValid();
}

void CMDIView::Dump(CDumpContext& dc) const
{
    CView::Dump(dc);
}

CMDIDoc* CMDIView::GetDocument() // non-debug version is inline
{
    ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMDIDoc)));
    return (CMDIDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMDIView message handlers

void CMDIView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
    CMDIDoc* pDoc = GetDocument();



    ASSERT_VALID(pDoc);
  
    if(nChar != '\r'){
        pDoc->text[pDoc->number_lines] += nChar;
        Invalidate();
    }
    else{
        pDoc->number_lines++;      
    }
    // TODO: Add your message handler code here and/or call default
  
    pDoc->UpdateAllViews(this, (long) pDoc->number_lines, NULL);
    pDoc->SetModifiedFlag();

    CView::OnChar(nChar, nRepCnt, nFlags);
}

void CMDIView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
{
    CClientDC dc(this);
    TEXTMETRIC tm;

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

    dc.GetTextMetrics(&tm);

    dc.TextOut(0, (int) lHint * tm.tmHeight, pDoc->text[lHint]);
    // TODO: Add your specialized code here and/or call the base class
  
  

}

When working with multiple views, there’s no reason any two views should show the same part of the same document, so let’s allow the user to move around in MDI documents with scrolling views.

Scrolling MDI Views

The MFC library provides the CScrollView class, which is a view class that supports scrolling. We put this class to work in a new example, ScrollMDI. In this example, the user is able to scroll each view independently.

Create a new MDI project now named ScrollMDI. To install the CScrollView class as our view class, stop in Step 6 of the AppWizard and make sure CMDIScrollView is selected in the Class name box, as shown in Figure 2.5. Select CScrollView in the Base class box, as also shown in Figure 2.5. After you’ve selected CScrollView, click Finish to create the new project.

The CScrollMDIView is derived from the CScrollView class, as shown in ScrollMDIView.h.

class CScrollMDIView : public CScrollView               ⇐

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

This means we have the CScrollView functions available to us for our own use.


Figure 2.5  Installing the CScrollView class.

To make sure we have something to scroll, we set up ScrollMDI to accept multiline text input as we did with our previous program, MDI. To do that, we add OnChar() to the view class using ClassWizard.

void CScrollMDIView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)

{
    CScrollMDIDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
  
    if(nChar != '\r'){
        pDoc->text[pDoc->number_lines] += nChar;
        Invalidate();
    }
    else{
        pDoc->number_lines++;      
    }
  
    pDoc->UpdateAllViews(this, pDoc->number_lines, NULL);
    pDoc->SetModifiedFlag();

    CScrollView::OnChar(nChar, nRepCnt, nFlags);

}


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.