东软2-计数器ver2

// homework2View.cpp : implementation of the CHomework2View class
//

#include "stdafx.h"
#include "homework2.h"

#include "homework2Doc.h"
#include "homework2View.h"

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

/////////////////////////////////////////////////////////////////////////////
// CHomework2View

IMPLEMENT_DYNCREATE(CHomework2View, CView)

BEGIN_MESSAGE_MAP(CHomework2View, CView)
	//{{AFX_MSG_MAP(CHomework2View)
	ON_WM_LBUTTONDOWN()
	ON_WM_RBUTTONDOWN()
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CHomework2View construction/destruction

CHomework2View::CHomework2View()
{
	// TODO: add construction code here

	CStdioFile myFile("data.txt", CFile::modeRead|CFile::typeText);
	myFile.SeekToBegin();
	CString str1;
	myFile.ReadString(str1);
//	CString str2;
//	myFile.ReadString(str2);
//	AfxMessageBox(str1+str2);弹出对话框

	num = _ttoi(str1);
}

CHomework2View::~CHomework2View()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CHomework2View drawing

void CHomework2View::OnDraw(CDC* pDC)
{
	CHomework2Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here

	CClientDC dc(this); // 更改当前字体 
	LOGFONT lf; 
	dc.GetCurrentFont()->GetLogFont(&lf);
	CFont font; 
	CFont *pOldFont; // 保存设备上下文最初使用的字体对象 
	lf.lfCharSet=134; 
	lf.lfHeight=-150; 
	lf.lfHeight=-150; 
	lf.lfWidth=0; 
	strcpy(lf.lfFaceName, "隶书");
	font.CreateFontIndirect( &lf);
	pOldFont=dc.SelectObject( &font);
	dc.SetBkMode(TRANSPARENT); // 更改当前画笔 
	CPen pen(PS_SOLID, 1, RGB(255, 0, 0)); 
	CPen *pOldPen; 
	pOldPen=dc.SelectObject( &pen); // 开始一个路径 
	dc.BeginPath(); 
	dc.TextOut(10, 10, "孟颖");
	dc.EndPath(); // 绘制路径 
	dc.StrokePath(); 
	//可以用dc.StrokeAndFillPath()函数来代替,不过该函数会使用当前刷子填充路径的内部。 
	dc.SelectObject(pOldFont); 
	dc.SelectObject(pOldPen); 

	CRect rect;
	this->GetWindowRect(&rect);
	int x;
	int y;
	x = rect.Width() / 2;
	y = rect.Height() / 2;

	str.Format("%d", num);

	pDC->TextOut(x, y, str);

	CFile mFile(_T("data.txt"), CFile::modeWrite | CFile::modeCreate);
	mFile.Write(str,2);
	mFile.Flush();
	mFile.Close();
}

/////////////////////////////////////////////////////////////////////////////
// CHomework2View printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CHomework2View diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CHomework2View message handlers

void CHomework2View::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	num += 1;
	this->Invalidate();

	CView::OnLButtonDown(nFlags, point);
}

void CHomework2View::OnRButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	num -= 1;
	this->Invalidate();

	CView::OnRButtonDown(nFlags, point);
}

你可能感兴趣的:(东软2-计数器ver2)