Webocreation

Tuesday, November 17, 2009

The CView Derived Class

  1. The CView Derived Class
The class CReaderView, derived from CView, is the view class for the Reader application.

CReaderView contains two member functions, GetDocument and OnDraw. The GetDocument member function is used to retrieve a pointer to the associated document as follows:
inline CReaderDoc* CReaderView::GetDocument()
{ return (CReaderDoc*)m_pDocument; }

The OnDraw member function displays the text line by line on the screen. It uses the GetLineList member function to get a pointer to the CStringList object as follows:

CStringList *pLineList = GetDocument()->GetLineList();

Next, a for loop is executed to go through the strings in the list and display them on the screen. This task is accomplished using the TabbedTextOut function as follows:

for(pos = pLineList->GetHeadPosition(); pos != NULL; )
{
strLine = pLineList->GetAt(pos);
pDC->TabbedTextOut(nXPos,nYPos,strLine,0,NULL,0);
pLineList->GetNext(pos);
nYPos +=nYDelta;
}

To see the source code for the files associated with the CReaderView class, click this icon. For easy reference, the segments of code discussed in this section are highlighted in bold.

// ReaderView.h : interface of the CReaderView class

//

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

#if !defined(AFX_READERVIEW_H__7EC9070F_F96E_11D0_B9C5_00AA00688598__INCLUDED_)

#define AFX_READERVIEW_H__7EC9070F_F96E_11D0_B9C5_00AA00688598__INCLUDED_

#if _MSC_VER >= 1000

#pragma once
#endif // _MSC_VER >= 1000

class CReaderView : public CView

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

// Attributes

public:
CReaderDoc* GetDocument();

// Operations

public:

// Overrides

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CReaderView)
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);
//}}AFX_VIRTUAL

// Implementation

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

protected:

// Generated message map functions
protected:
//{{AFX_MSG(CReaderView)
// NOTE - the ClassWizard will add and remove member functions here.
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

#ifndef _DEBUG // debug version in ReaderView.cpp

inline CReaderDoc* CReaderView::GetDocument()
{ return (CReaderDoc*)m_pDocument; }
#endif

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

//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_READERVIEW_H__7EC9070F_F96E_11D0_B9C5_00AA00688598__INCLUDED_)

// ReaderView.cpp : implementation of the CReaderView class
//

#include "stdafx.h"

#include "Reader.h"

#include "ReaderDoc.h"

#include "ReaderView.h"

#ifdef _DEBUG

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

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

// CReaderView

IMPLEMENT_DYNCREATE(CReaderView, CView)

BEGIN_MESSAGE_MAP(CReaderView, CView)
//{{AFX_MSG_MAP(CReaderView)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}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()

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

// CReaderView construction/destruction

CReaderView::CReaderView()

{
// TODO: add construction code here

}

CReaderView::~CReaderView()
{
}

BOOL CReaderView::PreCreateWindow(CREATESTRUCT& cs)

{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs

return CView::PreCreateWindow(cs);

}

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

// CReaderView drawing

void CReaderView::OnDraw(CDC* pDC)

{
CStringList *pLineList = GetDocument()->GetLineList();
CString strLine;
POSITION pos;
int nXPos=10; int nYPos=10; int nYDelta = 0;

TEXTMETRIC tm;

pDC->GetTextMetrics(&tm);
nYDelta = tm.tmHeight;
for(pos = pLineList->GetHeadPosition(); pos != NULL; )
{
strLine = pLineList->GetAt(pos);
pDC->TabbedTextOut(nXPos,nYPos,strLine,0,NULL,0);
pLineList->GetNext(pos);
nYPos +=nYDelta;
}
TRACE( "nYDelta = %d\n",nYDelta );
}

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

// CReaderView printing

BOOL CReaderView::OnPreparePrinting(CPrintInfo* pInfo)

{
// default preparation
return DoPreparePrinting(pInfo);
}

void CReaderView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)

{
// TODO: add extra initialization before printing
}

void CReaderView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)

{
// TODO: add cleanup after printing
}

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

// CReaderView diagnostics

#ifdef _DEBUG

void CReaderView::AssertValid() const
{
CView::AssertValid();
}

void CReaderView::Dump(CDumpContext& dc) const

{
CView::Dump(dc);
}

CReaderDoc* CReaderView::GetDocument() // non-debug version is inline

{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CReaderDoc)));
return (CReaderDoc*)m_pDocument;
}
#endif //_DEBUG

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

// CReaderView message handlers

Sample Applications
A short description follows of the sample application related to this chapter. This sample application is located in \Samples\Ch04.

Sample application subdirectory Description of application

\Reader An SDI application built using AppWizard. The application reads strings of text from a file and displays them on the screen.

No comments:

Post a Comment