www.gusucode.com > 《C++高级语言程序设计》PPT及全书例子源代码-源码程序 > 《C++高级语言程序设计》PPT及全书例子源代码-源码程序/code/C++例题程序/第8章/s8_6/s8_6View.cpp

    //Download by http://www.NewXing.com
// s8_6View.cpp : implementation of the CS8_6View class
//

#include "stdafx.h"
#include "s8_6.h"

#include "s8_6Doc.h"
#include "s8_6View.h"

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

/////////////////////////////////////////////////////////////////////////////
// CS8_6View

IMPLEMENT_DYNCREATE(CS8_6View, CView)

BEGIN_MESSAGE_MAP(CS8_6View, CView)
	//{{AFX_MSG_MAP(CS8_6View)
	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()

/////////////////////////////////////////////////////////////////////////////
// CS8_6View construction/destruction

CS8_6View::CS8_6View()
{
	// TODO: add construction code here
	m_oCaretPosition.x = m_oCaretPosition.y = 0;	//初始化插入符位置
}

CS8_6View::~CS8_6View()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CS8_6View drawing

void CS8_6View::OnDraw(CDC* pDC)
{
	CS8_6Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	TEXTMETRIC sTextMetric;			
	pDC->GetTextMetrics(&sTextMetric);
	CreateSolidCaret(sTextMetric.tmAveCharWidth/8, sTextMetric.tmHeight);
	
	pDC->TextOut(0, 0, pDoc->m_strText);
	CSize oSize = pDC->GetTextExtent(pDoc->m_strText);
	HideCaret();
	m_oCaretPosition.x = oSize.cx;
	SetCaretPos(m_oCaretPosition);
	ShowCaret();
}

/////////////////////////////////////////////////////////////////////////////
// CS8_6View printing

BOOL CS8_6View::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

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

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

/////////////////////////////////////////////////////////////////////////////
// CS8_6View diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CS8_6View message handlers

void CS8_6View::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	CS8_6Doc* pDoc = GetDocument();		// 取得指向当前文档的指针
	ASSERT_VALID(pDoc);					// 确保pDoc指向当前文档并是正确的 
	pDoc->m_strText += nChar;			// 将输入的可见字符存入m_strText中
	Invalidate();						// 使当前文档显示无效,强行执行OnDraw()函数
	
	CView::OnChar(nChar, nRepCnt, nFlags);
}