- The CView Derived Class
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.
//
/////////////////////////////////////////////////////////////////////////////
#define AFX_READERVIEW_H__7EC9070F_F96E_11D0_B9C5_00AA00688598__INCLUDED_
#if _MSC_VER >= 1000#pragma once
#endif // _MSC_VER >= 1000
{
protected: // create from serialization only
CReaderView();
DECLARE_DYNCREATE(CReaderView)
public:
CReaderDoc* GetDocument();
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
public:
virtual ~CReaderView();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
// 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()
};
inline CReaderDoc* CReaderView::GetDocument()
{ return (CReaderDoc*)m_pDocument; }
#endif
//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before the previous line.
// ReaderView.cpp : implementation of the CReaderView class
//
#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()
{
}
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT 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;
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);
}
{
// TODO: add extra initialization before printing
}
{
// TODO: add cleanup after printing
}
// CReaderView diagnostics
#ifdef _DEBUGvoid CReaderView::AssertValid() const
{
CView::AssertValid();
}
{
CView::Dump(dc);
}
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CReaderDoc)));
return (CReaderDoc*)m_pDocument;
}
#endif //_DEBUG
// CReaderView message handlers
Sample ApplicationsA 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