📄 clockctrl.cpp
字号:
#include "stdafx.h"
#include <math.h>
#include "ClockCtrl.h"
//定音PI为圆周率
const double PI = 3.14159;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
///////////////////////////////// Implementation //////////////////////////////
BEGIN_MESSAGE_MAP(CClockCtrl, CStatic)
//{{AFX_MSG_MAP(CClockCtrl)
ON_WM_PAINT()
ON_WM_SIZE()
ON_WM_TIMER()
ON_WM_DESTROY()
ON_WM_ERASEBKGND()
ON_WM_LBUTTONDOWN()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/**
*************************************************************
* 函 数 名: CClockCtrl()
* 描 述: 该类的构造函数,用于初始化成员变量
* 数 据 库: 无
* 数据库表: 无
* 输入参数: 无
* 输出参数: 无
* 返 回 值: 无
* 创 建 人: 袁军
* 日 期: 2007/05/22
* 修改记录:
* 修改人 修改日期 修改描述
*************************************************************
*/
CClockCtrl::CClockCtrl()
{
//Initialize the member variables to their default values
//小时与分钟指针颜色
m_ColorHourMinutesHand = GetSysColor(13);
//5分钟的标签颜色
m_ColorPoints = GetSysColor(13);
m_nXRadius = -1;
m_nYRadius = -1;
m_nPointWidth = -1;
m_nHour = 0;
m_nMinute = 0;
m_nSecond = 0;
m_nTimerID = 0;
//默认为钟表的时区就是系统的时区
m_iTimeZoneDiffer = 0;
m_iMinuteDiffer = 0;
//是否启动时间
m_bEnableRealtime = TRUE;
//是否显示小时
m_bShowHours = TRUE;
//是否显示分钟
m_bShowMinutes = TRUE;
//是否显示秒
m_bShowSeconds = TRUE;
//是否显示表格背景
m_bShowPoints = TRUE;
//是否显示非5的整数秒
m_bShowMinorPoints = TRUE;
//表的背景色
m_ColorBackground = GetSysColor(COLOR_BTNFACE);
//是否显示3D效果
m_b3dPoints = TRUE;
}
void CClockCtrl::PreSubclassWindow()
{
//Let the parent class do its thing
CStatic::PreSubclassWindow();
//Setup the timer if we are displaying realtime values
if (m_bEnableRealtime)
SetRealTime(TRUE);
}
/**
*************************************************************
* 函 数 名: OnPaint()
* 描 述: 绘图使用的绘制函数
* 数 据 库: 无
* 数据库表: 无
* 输入参数: 无
* 输出参数: 无
* 返 回 值: 无
* 创 建 人: 袁军
* 日 期: 2007/05/22
* 修改记录:
* 修改人 修改日期 修改描述
*************************************************************
*/
void CClockCtrl::OnPaint()
{
//如果没有初始化,则设置钟表大小
if (-1 == m_nXRadius)
{
RecalcLayout();
}
// device context for painting
CPaintDC dc(this);
//Setup the brush we are going to use
CBrush PointBrush(m_ColorPoints);
CBrush* pOldBrush = dc.SelectObject(&PointBrush);
//Draw the points going around the clock
if (m_bShowPoints)
{
for (int nMinute=0; nMinute<60; nMinute++)
{
//Work out the face length which is different
//depending on where we are on the ellipse edge
CPoint p = ComputePoint(nMinute, 0.9);
if (0 == (nMinute % 5))
{
DrawFacePoint(dc, p, TRUE);
}
else if (m_bShowMinorPoints)
{
DrawFacePoint(dc, p, FALSE);
}
}
}
//Restore the DC
dc.SelectObject(pOldBrush);
//Then draw all the hands
DrawHands(dc, m_nHour, m_nMinute, m_nSecond, TRUE);
}
/**
*************************************************************
* 函 数 名: DrawHands(CDC& dc, int nHour, int nMinute, int nSecond, BOOL bDrawAll)
* 描 述: 绘制图形钟表的针
* 数 据 库: 无
* 数据库表: 无
* 输入参数: nHour:小时、nMinute:分钟、nSecond:秒钟
* 输出参数: 无
* 返 回 值: 无
* 创 建 人: 袁军
* 日 期: 2007/12/13
* 修改记录:
* 修改人 修改日期 修改描述
*************************************************************
*/
void CClockCtrl::DrawHands(CDC& dc, int nHour, int nMinute, int nSecond, BOOL bDrawAll)
{
// m_nHour = m_nHour + m_iTimeZone;
// if (m_bIsUpdate)
// {
// m_iTimeZone = 0;
// }
//Validate our parameters
ASSERT(nHour >= 0 && nHour <= 12);
ASSERT(nMinute >= 0 && nMinute <= 59);
ASSERT(nSecond >= 0 && nSecond <= 59);
//Setup the brush we are going to use
CBrush PointBrush(m_ColorHourMinutesHand);
CBrush* pOldBrush = dc.SelectObject(&PointBrush);
if (bDrawAll)
{
//Draw the hour hand
if (m_bShowHours)
DrawHand(dc, nHour, HOUR, TRUE);
//Draw the minute hand
if (m_bShowMinutes)
DrawHand(dc, nMinute, MINUTE, TRUE);
//Draw the seconds hand
if (m_bShowSeconds)
DrawHand(dc, nSecond, SECOND, TRUE);
}
else
{
//Hour or minute is changing, erase both
if (((m_nHour != nHour) || (m_nMinute != nMinute)))
{
//Erase the seconds hand
if (m_bShowSeconds)
DrawHand(dc, m_nSecond, SECOND, FALSE);
//Erase the minute hand
if (m_bShowMinutes)
DrawHand(dc, m_nMinute, MINUTE, FALSE);
//Erase the hour hand
if (m_bShowHours)
{
DrawHand(dc, m_nHour, HOUR, FALSE);
}
//Draw the hour hand
if (m_bShowHours)
{
DrawHand(dc, nHour, HOUR, TRUE);
Invalidate(TRUE);
}
//Draw the minute hand
if (m_bShowMinutes)
DrawHand(dc, nMinute, MINUTE, TRUE);
//Draw the seconds hand
if (m_bShowSeconds)
DrawHand(dc, nSecond, SECOND, TRUE);
}
else
{
//Erase the seconds hand
if (m_bShowSeconds)
DrawHand(dc, m_nSecond, SECOND, FALSE);
//Draw the seconds hand
if (m_bShowSeconds)
DrawHand(dc, nSecond, SECOND, TRUE);
}
}
//Store away the new values
m_nHour = nHour;
m_nMinute = nMinute;
m_nSecond = nSecond;
//Restore the DC
dc.SelectObject(pOldBrush);
}
/**
*************************************************************
* 函 数 名: SetTime(int nHour, int nMinute, int nSecond)
* 描 述: SetTime定时触发该事件
* 数 据 库: 无
* 数据库表: 无
* 输入参数: int nHour:时, int nMinute:分, int nSecond:秒)
* 输出参数: 无
* 返 回 值: 无
* 创 建 人: 袁军
* 日 期: 2007/12/13
* 修改记录:
* 修改人 修改日期 修改描述
*************************************************************
*/
void CClockCtrl::SetTime(int nHour, int nMinute, int nSecond)
{
//Validate our parameters
ASSERT(m_hWnd);
ASSERT(nHour >= 0 && nHour <= 12);
ASSERT(nMinute >= 0 && nMinute <= 59);
ASSERT(nSecond >= 0 && nSecond <= 59);
//Do the updated drawing of the hands
CClientDC dc(this);
DrawHands(dc, nHour, nMinute, nSecond, FALSE);
}
/**
*************************************************************
* 函 数 名: MinuteToRadian(double minute)
* 描 述: 计算针的弧度
* 数 据 库: 无
* 数据库表: 无
* 输入参数: 无
* 输出参数: 无
* 返 回 值: 弧度
* 创 建 人: 袁军
* 日 期: 2007/12/13
* 修改记录:
* 修改人 修改日期 修改描述
*************************************************************
*/
double CClockCtrl::MinuteToRadian(double minute)
{
return (minute-15)*PI/30;
}
/**
*************************************************************
* 函 数 名: ComputePoint(int nMinute, double ratio)
* 描 述: 计算针尖坐标
* 数 据 库: 无
* 数据库表: 无
* 输入参数: 无
* 输出参数: 无
* 返 回 值: 针尖坐标值
* 创 建 人: 袁军
* 日 期: 2007/12/13
* 修改记录:
* 修改人 修改日期 修改描述
*************************************************************
*/
CPoint CClockCtrl::ComputePoint(int nMinute, double ratio)
{
CPoint point;
double angle = MinuteToRadian(nMinute);
point.x = m_MiddlePoint.x + (int) (m_nXRadius*ratio*cos(angle) + 0.5);
point.y = m_MiddlePoint.y + (int) (m_nYRadius*ratio*sin(angle) + 0.5);
return point;
}
/**
*************************************************************
* 函 数 名: DrawFacePoint(CDC& dc, const CPoint& point, BOOL bMajor)
* 描 述: 绘制钟表外壳
* 数 据 库: 无
* 数据库表: 无
* 输入参数: 无
* 输出参数: 无
* 返 回 值: 无
* 创 建 人: 袁军
* 日 期: 2007/12/13
* 修改记录:
* 修改人 修改日期 修改描述
*************************************************************
*/
void CClockCtrl::DrawFacePoint(CDC& dc, const CPoint& point, BOOL bMajor)
{
//Work out the size of the point rectangle
CRect rPoint(point, point);
if (bMajor)
{
//5分钟整数的标签设置
int nPointRadius = m_nPointWidth / 2 +1;
rPoint.InflateRect(nPointRadius, nPointRadius);
//Do the actual point drawing
dc.Rectangle(&rPoint);
if (m_b3dPoints)
dc.Draw3dRect(&rPoint, GetSysColor(COLOR_BTNHIGHLIGHT), GetSysColor(COLOR_BTNSHADOW));
}
else
{
if (m_bShowMinorPoints)
{
rPoint.InflateRect(1, 1);
//Do the actual point drawing
dc.Draw3dRect(&rPoint, GetSysColor(COLOR_BTNHIGHLIGHT), GetSysColor(COLOR_BTNSHADOW));
}
}
}
/**
*************************************************************
* 函 数 名: DrawHand(CDC& dc, int nMinute, HandType type, BOOL bDraw)
* 描 述: 绘制针
* 数 据 库: 无
* 数据库表: 无
* 输入参数: CDC& dc, int nMinute, HandType type, BOOL bDraw
* 输出参数: 无
* 返 回 值: 无
* 创 建 人: 袁军
* 日 期: 2007/12/13
* 修改记录:
* 修改人 修改日期 修改描述
*************************************************************
*/
void CClockCtrl::DrawHand(CDC& dc, int nMinute, HandType type, BOOL bDraw)
{
//Calculate the point positions of the hand
CPoint handPoints[4];
GetHandPoints(nMinute, type, handPoints);
if (type == SECOND)
{
//Draw the hand
//dc.Polygon(handPoints, 2);
int nOldRop = dc.SetROP2(R2_NOTXORPEN);
dc.MoveTo(handPoints[0]);
dc.LineTo(handPoints[1]);
dc.SetROP2(nOldRop);
}
else
{
//Pick the correct colors to be used
COLORREF colorBrush;
COLORREF colorPen;
if (bDraw)
{
colorBrush = m_ColorHourMinutesHand;
//colorBrush = 12,48,54;
//针的外边框色
colorPen = RGB(0,0,0);
//colorPen = m_ColorBackground;
}
else
{
colorBrush = m_ColorBackground;
//colorBrush = 12,48,54;
colorPen = m_ColorBackground;
//colorPen = RGB(123,45,23);
}
//Setup the DC
CBrush brushHand(colorBrush);
CPen penHand(PS_SOLID, 1, colorPen);
CBrush* pOldBrush = dc.SelectObject(&brushHand);
CPen* pOldPen = dc.SelectObject(&penHand);
//Draw the hand
dc.Polygon(handPoints, 4);
//Restore the DC
dc.SelectObject(pOldPen);
dc.SelectObject(pOldBrush);
}
}
/**
*************************************************************
* 函 数 名: GetHandPoints(int nValue, HandType type, CPoint* pPoints)
* 描 述: 获取针尖坐标值
* 数 据 库: 无
* 数据库表: 无
* 输入参数: 无
* 输出参数: 无
* 返 回 值: 无
* 创 建 人: 袁军
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -