www.pudn.com > IPServer.rar > Clock.cpp
#include "StdAfx.h" #include "Clock.h" #includeconst double PI = 3.1415926535897931f; CClock::CClock(void) : m_penHour(PS_SOLID, 2, RGB(128,128,128)) , m_pen(PS_SOLID, 3, RGB(19,157,255)) { memset(&m_point, 0, 4*sizeof(POINT)); } CClock::~CClock(void) { } void CClock::ClockStart(HWND hWnd, UINT nIDTimer, const CRect &rc) { CalculateHandAngle(); SetTimer(hWnd, nIDTimer, 500, NULL); m_hWnd = hWnd; m_nIDTimer = nIDTimer; m_fRadius = rc.Width()/2.0f-4.0f; m_fxCenter = rc.right/2.0f; m_fyCenter = rc.right/2.0f+5.0f; } void CClock::ClockStop() { KillTimer(m_hWnd, m_nIDTimer); } void CClock::CalculateHandAngle() { time_t tval = time(NULL); tm *time; time=localtime(&tval/*, &tval*/); m_fAngleSecond = PI*(time->tm_sec)/30.0f; m_fAngleMinute = PI*(time->tm_min + time->tm_sec/60.0f)/30.0f; m_fAngleHour = PI*(time->tm_hour + time->tm_min/60.0f)/6.0f; } void CClock::ClockDraw(CDC *pDC) { CPen * oldPen = pDC->SelectObject(&m_penHour); for (int i=0; i<60; ++i) { pDC->MoveTo((int)(m_fxCenter + (m_fRadius-6.0f)*sin(PI*i/30.0)), (int)(m_fyCenter- (m_fRadius-6.0f)*cos(PI*i/30.0))); pDC->LineTo((int)(m_fxCenter + ((m_fRadius-4.0f)*sin(PI*i/30.0))), (int)(m_fyCenter- (m_fRadius-4)*cos(PI*i/30.0))); } pDC->SelectObject(&m_pen); for (int j=0; j<12; ++j) { pDC->MoveTo((int)(m_fxCenter + (m_fRadius-7)*sin(PI*j/6.0)), (int)(m_fyCenter- (m_fRadius-7)*cos(PI*j/6.0))); pDC->LineTo((int)(m_fxCenter + (m_fRadius-4)*sin(PI*j/6.0)), (int)(m_fyCenter- (m_fRadius-4)*cos(PI*j/6.0))); } pDC->SelectObject(&m_penHour); DrawClockHand(pDC, 30, 10, 10, m_fAngleHour); DrawClockHand(pDC, 15, 8, 10, m_fAngleMinute); DrawClockHand(pDC, 9, 2, 15, m_fAngleSecond); pDC->SelectObject(&m_pen); pDC->Ellipse((int)(m_fxCenter-1.0f), (int)(m_fyCenter-1.0f), (int)(m_fxCenter+1.0f), (int)(m_fyCenter+1.0f)); pDC->SelectObject(oldPen); } void CClock::DrawClockHand(CDC *pDC, double f1, double f2, double f3, double fAngle) { m_point[0].x = long(m_fxCenter + ((m_fRadius-f1)*sin(fAngle))); m_point[0].y = long(m_fyCenter - ((m_fRadius-f1)*cos(fAngle))); m_point[1].x = long(m_fxCenter - (f2*cos(fAngle))); m_point[1].y = long(m_fyCenter - (f2*sin(fAngle))); m_point[2].x = long(m_fxCenter - (f3*sin(fAngle))); m_point[2].y = long(m_fyCenter + (f3*cos(fAngle))); m_point[3].x = long(m_fxCenter + (f2*cos(fAngle))); m_point[3].y = long(m_fyCenter + (f2*sin(fAngle))); pDC->Polygon(m_point, 4); }