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

📄 clockview.cpp

📁 用Visual C++写的clock程序,对于还在学C++的朋友有参考价值
💻 CPP
字号:
// ClockView.cpp : implementation of the CClockView class
//

#include "stdafx.h"
#include "Clock.h"

#include "ClockDoc.h"
#include "ClockView.h"

#include <math.h>
#define PI 2*3.1415926

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

/////////////////////////////////////////////////////////////////////////////
// CClockView

IMPLEMENT_DYNCREATE(CClockView, CView)

BEGIN_MESSAGE_MAP(CClockView, CView)
	//{{AFX_MSG_MAP(CClockView)
	ON_WM_CREATE()
	ON_WM_DESTROY()
	ON_WM_PAINT()
	ON_WM_SIZE()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CClockView construction/destruction

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

}

CClockView::~CClockView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CClockView drawing

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

/////////////////////////////////////////////////////////////////////////////
// CClockView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CClockView message handlers

int CClockView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	SetTimer(1,1000,NULL);
	return 0;
}

void CClockView::OnDestroy() 
{
	CView::OnDestroy();
	
	// TODO: Add your message handler code here
	KillTimer(1);
}

void CClockView::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here
	GetLocalTime(&st);
	stPrevious=st;
	hdc=(HDC)dc;
	SetIsotropic(hdc,cxClient,cyClient);
	SelectObject(hdc,GetStockObject(BLACK_PEN));
	DrawClock(hdc);
	DrawHands(hdc,&stPrevious,TRUE);

	// Do not call CView::OnPaint() for painting messages
}

void CClockView::OnSize(UINT nType, int cx, int cy) 
{
	CView::OnSize(nType, cx, cy);
	
	// TODO: Add your message handler code here
	cxClient=cx;
	cyClient=cy;

}
void CClockView::SetIsotropic(HDC hdc,int cxClient,
							  int cyClient)
{
	SetMapMode(hdc,MM_ISOTROPIC);
	SetWindowExtEx(hdc,1000,1000,NULL);
	SetViewportExtEx(hdc,cxClient/2,-cyClient/2,NULL);
	SetViewportOrgEx(hdc,cxClient/2, cyClient/2,NULL);
}

void CClockView::DrawClock(HDC hdc)
{
	int iAngle;
	POINT pt[3];
	for (iAngle=0;iAngle<360; iAngle+=6)
	{
		pt[0].x=0;
		pt[0].y=900;
		RotatePoint(pt,1,iAngle);
		pt[2].x=pt[2].y=iAngle % 5?33:100;
		pt[0].x-=pt[2].x/2;
		pt[0].y-=pt[2].y/2;
		pt[1].x=pt[0].x+pt[2].x;
		pt[1].y=pt[0].y+pt[2].y;	
		SelectObject(hdc,GetStockObject(BLACK_BRUSH));
		Ellipse(hdc,pt[0].x,pt[0].y,pt[1].x,pt[1].y);
	}
}

void CClockView::DrawHands(HDC hdc, SYSTEMTIME *pst,
						   BOOL fChange)
{
	static POINT pt[3][5]={0,-150,100,0,0,600,-100,0,0,-150,
		0,-200,50,0,0,800,-50,0,0,-200,
		0,0,0,0,0,0,0,0,0,800};
	int i, iAngle[3];
	POINT ptTemp[3][5];
	iAngle[0]=(pst->wHour*30)%360+pst->wMinute/2;
	iAngle[1]=pst->wMinute*6;
	iAngle[2]=pst->wSecond*6;
	memcpy(ptTemp,pt,sizeof(pt));
	for(i=fChange ? 0:2; i<3;i++)
	{
		RotatePoint(ptTemp[i],5,iAngle[i]);
		Polyline(hdc,ptTemp[i],5);
	}
}

void CClockView::RotatePoint(POINT pt[], int iNum,
							 int iAngle)
{
	int i;
	POINT ptTemp;
	for(i=0;i<iNum;i++)
	{
		ptTemp.x=(int) (pt[i].x*cos(PI*iAngle/360)+
			pt[i].y*sin(PI*iAngle/360));
		ptTemp.y=(int) (pt[i].y*cos(PI*iAngle/360)-
			pt[i].x*sin(PI*iAngle/360));
		pt[i]=ptTemp;
	}
}

void CClockView::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	Invalidate(TRUE);
	GetLocalTime(&st);
	fChange=st.wHour !=stPrevious.wHour ||
		st.wMinute != stPrevious.wMinute;
	pdc=GetDC();
	hdc=(HDC)pdc;
	SetIsotropic(hdc,cxClient,cyClient);
	SelectObject(hdc,GetStockObject(WHITE_PEN));
	DrawHands(hdc,&stPrevious,fChange);
	SelectObject(hdc,GetStockObject(BLACK_PEN));
	DrawHands(hdc,&st,TRUE);
	ReleaseDC(pdc);
	stPrevious=st;
	if ((st.wMinute==59) && (st.wSecond>=55))	Beep(50,5);
	if ((st.wMinute==0) && (st.wSecond==0))	Beep(200,5);
}

⌨️ 快捷键说明

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