⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timeview.cpp

📁 这是书上的代码
💻 CPP
字号:
// TimeView.cpp : implementation of the CTimeView class
//

#include "stdafx.h"
#include "Time.h"

#include "TimeDoc.h"
#include "TimeView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CTimeView
const int SHOW_CIRCLE_TIMER = 100;

IMPLEMENT_DYNCREATE(CTimeView, CView)

BEGIN_MESSAGE_MAP(CTimeView, CView)
	//{{AFX_MSG_MAP(CTimeView)
	ON_WM_CREATE()
	ON_WM_TIMER()
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CTimeView construction/destruction

int CTimeView::m_iTimerCount = 0;
int CTimeView::m_iWhich = -1;
CRect CTimeView::m_rect(30,50,80,100);

CTimeView::CTimeView()
{
	// TODO: add construction code here
	/*
	this->m_iTimerCount = 0;
	this->m_rect.right = 150;
	this->m_rect.bottom =200;
	this->m_rect.left = 100;
	this->m_rect.top = 150;
	*/

}

CTimeView::~CTimeView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CTimeView drawing

void CTimeView::OnDraw(CDC* pDC)
{
	CTimeDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	//根据m_iWhich的值来显示程序执行的路线
	//m_iWhich = 1  代表每次时间到的时候,程序执行的是CTimeView::OnTimer()  
	//m_iWhich = 2  代表每次时间到的时候,程序执行的是CTimeView::MyTimerProc()
	if(m_iWhich == 1)
	{
		pDC->TextOut(0,20,"CTimeView::OnTimer() is called!");
	}
	if(m_iWhich == 2)
	{
		pDC->TextOut(0,20,"CTimeView::MyTimerProc() is called!");
	}
	//通过计时器来实现圆的颜色的更换
	int i;
	i = m_iTimerCount % 4;
	LOGBRUSH lb;
	pDC->GetCurrentBrush()->GetLogBrush(&lb);
	switch (i)
	{
	case 0:
		lb.lbColor=RGB(245,167,242);
		break;
	case 1:
		lb.lbColor=RGB(245,239,120);
		break;
	case 2:
		lb.lbColor=RGB(103,227,81);
		break;
	case 3:
		lb.lbColor=RGB(0,0,255);
		break;		
	}	
	CBrush NewBrush1;
	NewBrush1.CreateBrushIndirect(&lb);
	CBrush* pOldBrush1=pDC->SelectObject(&NewBrush1);	
	pDC->Ellipse(this->m_rect);		
	pDC->SelectObject(pOldBrush1);

}

/////////////////////////////////////////////////////////////////////////////
// CTimeView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CTimeView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CTimeView message handlers

int CTimeView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;	
	//这里给出了文章中讲到的两种方法的设置计时器的方法
	//并且给出了每种方法的SetTimer的各种调用方式,包括
	//正确的和错误的

	//您可以把放在correct code 注释中的其中的某一种SetTimer的调用方式打开,
	//来看程序的运行结果,都将是正确的,也就是说,这里给出了SetTimer的
	//各种调用方法
	/*****************************correct code**********************************/

	/****************文章中讲的的第一种方法********************/
	/***********CTimeView::OnTimer() will be called***********/	
	//::SetTimer(this->m_hWnd,SHOW_CIRCLE_TIMER,1000,NULL);	
	//this->SetTimer(SHOW_CIRCLE_TIMER,1000,NULL);
	/***********CTimeView::OnTimer() will be called!**********/

	/****************文章中讲的的第二种方法********************/
	/***********CTimeView::MyTimerProc() will be called!**********/	
	::SetTimer(this->m_hWnd,SHOW_CIRCLE_TIMER,1000,(TIMERPROC)MyTimerProc);	
	//this->SetTimer(SHOW_CIRCLE_TIMER,1000,(TIMERPROC)MyTimerProc);
	/***********CTimeView::MyTimerProc() will be called!**********/

	/****************************correct code************************************/


	//您可以把放在erro code 注释中的其中的某一种SetTimer的调用方式打开,
	//来看程序的运行结果,都将发生编译错误,错误发生的具体原因,在文章
	//中给出了详细地解释。
	/****************************error code************************************/
	//::SetTimer(this->m_hWnd,SHOW_CIRCLE_TIMER,1000,MyTimerProc);
	//::SetTimer(this->m_hWnd,SHOW_CIRCLE_TIMER,1000,OnTimer);
	//this->SetTimer(SHOW_CIRCLE_TIMER,1000,OnTimer);
	//this->SetTimer(SHOW_CIRCLE_TIMER,1000,MyTimerProc);
	/****************************error code***********************************/
	
	return 0;
}

void CTimeView::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	m_iTimerCount++;
	m_iWhich = 1;	
	this->Invalidate();
	CView::OnTimer(nIDEvent);
}

void CALLBACK CTimeView::MyTimerProc(HWND hwnd,UINT uMsg,UINT_PTR idEvent,DWORD dwTime)
{
	m_iTimerCount++;
	m_iWhich = 2;	
	::InvalidateRect(hwnd,NULL,true);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -