前言
《指针式时钟》问题分析,功能分析
(1)正确显示系统时钟;
(2)能准确定位时钟刻度和时分秒针的位置;
(3)能随窗口大小的变化而变化。
源码下载链接,本文Sample源码下载地址:C++实现指针式时钟Sample_c++指针式时钟-C++代码类资源-优快云下载
一、效果图
Sample制作的时钟显示效果如下图所示:
二、部分代码
关键注意查看OnDraw函数内的时钟描画过程
// ClockView.cpp : implementation of the CClockView class
//
#include "stdafx.h"
#include "Clock.h"
#include "ClockDoc.h"
#include "ClockView.h"
#include "math.h"
#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)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}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)
ON_WM_CREATE()
ON_WM_TIMER()
END_MESSAGE_MAP()
/
// CClockView construction/destruction
CClockView::CClockView()
{
// TODO: add construction code here
}
CClockView::~CClockView()
{
}
int CClockView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: 在此添加您专用的创建代码
SetTimer(1,1000,NULL);//设置定时器1
return 0;
}
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
CPen pen(PS_SOLID,5,RGB(255,255,0));
CPen *pOldPen=(CPen*)pDC -> SelectObject(&pen);
static const double pi=3.1415926535;
//得到客户区的矩形
CRect rect;
GetClientRect(&rect);
//得到表盘中央位置,即圆心
int x0 = rect.Width() / 2;
int y0 = rect.Height() / 2;
//得到半径
int r = (x0 > y0 ? y0 : x0);
r = r - 5;
pDC->Ellipse(CRect(x0-r,y0-r,x0+r,y0+r));//画最外侧的圆
CPen Pen2(PS_SOLID,3,RGB(255,0,0));
pDC -> SelectObject(Pen2);
//将要在循环中重复的计算提取出来,减少时间复杂度
//在看代码时请将下列算式代入比较容易理解
double rate = (double)(2.0 * pi / 60.0);
double halfPi=pi/2;
int zoomR=r-20;
//绘制表盘
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(RGB(255,0,0));
for(int mark=1;mark<=60;mark++)
{
int positionX = (int)(zoomR * sin(rate * mark)+ x0);
int positionY = (int)(-zoomR * sin(rate * mark + halfPi)+ y0);
if(mark % 5 ==0)
{
char number[2];
itoa(mark/5,number,10);//按照十进制转换
//绘制数值
pDC -> TextOut(positionX-5,positionY-5,number);
}
}
CTime time = CTime::GetCurrentTime();
int hour = time.GetHour();
int minute = time.GetMinute();
int second = time.GetSecond();
//绘制秒数指针
CPen secondPen(PS_SOLID,2,RGB(117,254,254));
pDC -> SelectObject(secondPen);
pDC -> MoveTo(CPoint(x0,y0));
int secondX = (int)((zoomR-25) * sin(rate * second)+ x0);
int secondY = (int)(-(zoomR-25) * sin(rate * second + halfPi)+ y0);
pDC -> LineTo(CPoint(secondX,secondY));
//绘制分钟指针
CPen minutePen(PS_SOLID,4,RGB(0,0,255));
pDC -> SelectObject(minutePen);
pDC -> MoveTo(CPoint(x0,y0));
int minuteX = (int)((zoomR-70) * sin(rate * minute)+ x0);
int minuteY = (int)(-(zoomR-70) * sin(rate * minute + halfPi)+ y0);
pDC -> LineTo(CPoint(minuteX,minuteY));
//绘制小时指针/
double hourRate=(double)(2.0*pi/12.0);
if(hour > 12)
{
hour -= 12;
}
CPen hourPen(PS_SOLID,8,RGB(0,255,0));
pDC -> SelectObject(hourPen);
pDC -> MoveTo(CPoint(x0,y0));
int hourX = (int)((zoomR-120) * sin(hourRate * hour )+ x0);
int hourY = (int)(-(zoomR-120) * sin(hourRate * hour + halfPi)+ y0);
pDC -> LineTo(CPoint(hourX,hourY));
}
/
// CClockView printing
BOOL CClockView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CClockView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CClockView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/
// 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
void CClockView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if(nIDEvent==1)
Invalidate();
CView::OnTimer(nIDEvent);
}