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

📄 gendigitalclock.cpp

📁 VisualC高级编程技术精粹.rar
💻 CPP
字号:
// GenDigitalClock.cpp: implementation of the CGenDigitalClock class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "SuperDigitalClock.h"
#include "GenDigitalClock.h"

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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

// 构造函数
CGenDigitalClock::CGenDigitalClock()
{
	fundal.CreateSolidBrush(c_fundal);
	inside.CreateSolidBrush(c_inside);
	line.CreatePen(PS_SOLID,1,c_line);
}

// 析构函数
CGenDigitalClock::~CGenDigitalClock()
{
	fundal.DeleteObject();
	inside.DeleteObject();
	line.DeleteObject();
}

// 画垂直线
void CGenDigitalClock::DrawVerLine(CRect rect, 
								   CDC* pDC, 
								   bool bFlag)
{
	int nWidth = rect.right - rect.left;
	int nHeight = rect.bottom - rect.top;
	int dev = nWidth/2;

	if (bFlag)
	{
		pDC->SelectObject(&inside);
		pDC->SelectObject(&line);
	}
	else 
	{
		pDC->SelectObject(&fundal);
		pDC->SelectObject(&line);
	}

	POINT lp[6];
	lp[0].x = rect.left + dev;
	lp[0].y = rect.top;
	lp[1].x = rect.left + nWidth;
	lp[1].y = rect.top + dev;
	lp[2].x = rect.left + nWidth;
	lp[2].y = rect.bottom - dev;
	lp[3].x = rect.left + dev;
	lp[3].y = rect.bottom;
	lp[4].x = rect.left;
	lp[4].y = rect.bottom - dev;
	lp[5].x = rect.left;
	lp[5].y = rect.top + dev;
	pDC->Polygon(lp,6);
}

// 画水平直线
void CGenDigitalClock::DrawHorLine(CRect rect, 
								   CDC* pDC, 
								   bool bFlag)
{
	int nWidth = rect.bottom - rect.top;
	int nHeight = rect.right - rect.left;
	int dev = nWidth/2;
	if (bFlag)
	{
		pDC->SelectObject(&inside);
		pDC->SelectObject(&line);
	}
	else 
	{
		pDC->SelectObject(&fundal);
		pDC->SelectObject(&line);
	}

	POINT lp[6];
	lp[0].x = rect.left;
	lp[0].y = rect.top+dev;
	lp[1].x = rect.left+dev;
	lp[1].y = rect.top;
	lp[2].x = rect.right-dev;
	lp[2].y = rect.top;
	lp[3].x = rect.right;
	lp[3].y = rect.top+dev;
	lp[4].x = rect.right-dev;
	lp[4].y = rect.bottom;
	lp[5].x = rect.left+dev;
	lp[5].y = rect.bottom;
	pDC->Polygon(lp,6);
}

// 画矩形框
void CGenDigitalClock::DrawRect(CRect rect, CDC* pDC)
{
	int x = rect.left;
	int x1 = rect.right;
	int y = rect.top;
	int y1 = rect.bottom;
	int nWidth = x1 - x;

	fundal.DeleteObject();
	fundal.CreateSolidBrush(c_fundal);
	inside.DeleteObject();
	inside.CreateSolidBrush(c_inside);
	line.DeleteObject();
	line.CreatePen(PS_SOLID,1,c_line);

	int sM = nWidth*7/100;
	int sm = nWidth*2/100;

	CTime ct(CTime::GetCurrentTime());
	int hour=ct.GetHour();
	int minute=ct.GetMinute();
	int seconds=ct.GetSecond();

	int nTime[7];
	nTime[1] = hour/10;
	nTime[2] = hour%10;
	nTime[3] = minute/10;
	nTime[4] = minute%10;
	nTime[5] = seconds/10;
	nTime[6] = seconds%10;


	int nWidth_Tmp = nWidth*12/100;
	int sus = (y1-y)*2/100;

	CRect dr[7];
	dr[1].left=sm;
	dr[1].right=dr[1].left+nWidth_Tmp;
	dr[1].top=sus;
	dr[1].bottom=y1-sus;

	dr[2].left=dr[1].right+sm;
	dr[2].right=dr[2].left+nWidth_Tmp;
	dr[2].top=sus;
	dr[2].bottom=y1-sus;
	
	dr[3].left=dr[2].right+sM;
	dr[3].right=dr[3].left+nWidth_Tmp;
	dr[3].top=sus;
	dr[3].bottom=y1-sus;

	dr[4].left=dr[3].right+sm;
	dr[4].right=dr[4].left+nWidth_Tmp;
	dr[4].top=sus;
	dr[4].bottom=y1-sus;

	dr[5].left=dr[4].right+sM;
	dr[5].right=dr[5].left+nWidth_Tmp;
	dr[5].top=sus;
	dr[5].bottom=y1-sus;

	dr[6].left=dr[5].right+sm;
	dr[6].right=dr[6].left+nWidth_Tmp;
	dr[6].top=sus;
	dr[6].bottom=y1-sus;

	for (int i = 1; i <= 6; i++)
	{
		DrawRectForTime(nTime[i],dr[i],pDC);
	}
	
	CPoint p1,p2;
	p1.x = dr[2].right + sM/2;
	p1.y = y + ((y1-y)/8)*3;
	p2.x = p1.x;
	p2.y = y + ((y1-y)/8)*6;
	DrawEllipse(p1,p2,sM/4,pDC);

	p1.x = dr[4].right + sM/2;
	p1.y = y + ((y1-y)/8)*3;
	p2.x = p1.x;
	p2.y = y + ((y1-y)/8)*6;
	DrawEllipse(p1,p2,sM/4,pDC);
}

// 画矩形框
void CGenDigitalClock::DrawRectForTime(int n, 
									   CRect rect, 
									   CDC* pDC)
{
	int x = rect.left;
	int x1 = rect.right;
	int y = rect.top;
	int y1 = rect.bottom;
	
	int nWidth = (x1-x)/4;
	
	int nHeight=(y1-y)/2;
	
	int nTmpWidth = (x1-x) - nWidth-4;
	switch (n)
	{
	case 8:
		{
			DrawHorLine(CRect(x+nWidth/2+2,y,x1-nWidth/2-2,
				        y+nWidth),pDC,TRUE);
			DrawVerLine(CRect(x,y+nWidth/2,x+nWidth,y+nHeight),
				        pDC,TRUE);
			DrawVerLine(CRect(x,y1-nHeight,x+nWidth,y1-nWidth/2),
				        pDC,TRUE);
			DrawHorLine(CRect(x+nWidth/2+2,y1-nWidth,
				        x1-nWidth/2-2,y1),pDC,TRUE);
			DrawVerLine(CRect(x1-nWidth,y+nHeight,x1,
				        y1-nWidth/2),pDC,TRUE);
			DrawVerLine(CRect(x1-nWidth,y+nWidth/2,x1,
				        y+nHeight),pDC,TRUE);
			DrawHorLine(CRect(x+nWidth/2+2,y+nHeight-nWidth/2,
				        x1-nWidth/2-2,
				        y+nHeight+nWidth/2),pDC,TRUE);
			break;
		}
	case 0:
		{
			DrawHorLine(CRect(x+nWidth/2+2,y,x1-nWidth/2-2,
				        y+nWidth),pDC,TRUE);
			DrawVerLine(CRect(x,y+nWidth/2,x+nWidth,y+nHeight),
				        pDC,TRUE);
			DrawVerLine(CRect(x,y1-nHeight,x+nWidth,y1-nWidth/2),
				        pDC,TRUE);
			DrawHorLine(CRect(x+nWidth/2+2,y1-nWidth,
				        x1-nWidth/2-2,y1),pDC,TRUE);
			DrawVerLine(CRect(x1-nWidth,y+nHeight,x1,
				        y1-nWidth/2),pDC,TRUE);
			DrawVerLine(CRect(x1-nWidth,y+nWidth/2,x1,
				        y+nHeight),pDC,TRUE);
			DrawHorLine(CRect(x+nWidth/2+2,y+nHeight-nWidth/2,
				        x1-nWidth/2-2,y+nHeight+nWidth/2),
				        pDC,FALSE);
			break;
		}
	case 1:
		{
			DrawHorLine(CRect(x+nWidth/2+2,y,x1-nWidth/2-2,
				y+nWidth),pDC,FALSE);
			DrawVerLine(CRect(x,y+nWidth/2,x+nWidth,
				y+nHeight),pDC,FALSE);
			DrawVerLine(CRect(x,y1-nHeight,x+nWidth,
				y1-nWidth/2),pDC,FALSE);
			DrawHorLine(CRect(x+nWidth/2+2,y1-nWidth,
				x1-nWidth/2-2,y1),pDC,FALSE);
			DrawVerLine(CRect(x1-nWidth,y+nHeight,x1,
				y1-nWidth/2),pDC,TRUE);
			DrawVerLine(CRect(x1-nWidth,y+nWidth/2,x1,
				y+nHeight),pDC,TRUE);
			DrawHorLine(CRect(x+nWidth/2+2,y+nHeight-nWidth/2,
				x1-nWidth/2-2,y+nHeight+nWidth/2),
				pDC,FALSE);
				
			break;
		}
	case 2:
		{
			DrawHorLine(CRect(x+nWidth/2+2,y,x1-nWidth/2-2,
				y+nWidth),pDC,TRUE);
			DrawVerLine(CRect(x,y+nWidth/2,x+nWidth,
				y+nHeight),pDC,FALSE);
			DrawVerLine(CRect(x,y1-nHeight,x+nWidth,
				y1-nWidth/2),pDC,TRUE);
			DrawHorLine(CRect(x+nWidth/2+2,y1-nWidth,
				x1-nWidth/2-2,y1),pDC,TRUE);
			DrawVerLine(CRect(x1-nWidth,y+nHeight,x1,
				y1-nWidth/2),pDC,FALSE);
			DrawVerLine(CRect(x1-nWidth,y+nWidth/2,x1,
				y+nHeight),pDC,TRUE);
			DrawHorLine(CRect(x+nWidth/2+2,y+nHeight-nWidth/2,
				x1-nWidth/2-2,y+nHeight+nWidth/2),pDC,TRUE);
				        
			break;
		}
	case 3:
		{
			DrawHorLine(CRect(x+nWidth/2+2,y,x1-nWidth/2-2,
				y+nWidth),pDC,TRUE);
			DrawVerLine(CRect(x,y+nWidth/2,x+nWidth,
				y+nHeight),pDC,FALSE);
			DrawVerLine(CRect(x,y1-nHeight,x+nWidth,
				y1-nWidth/2),pDC,FALSE);
			DrawHorLine(CRect(x+nWidth/2+2,y1-nWidth,
				x1-nWidth/2-2,y1),pDC,TRUE);
			DrawVerLine(CRect(x1-nWidth,y+nHeight,x1,
				y1-nWidth/2),pDC,TRUE);
			DrawVerLine(CRect(x1-nWidth,y+nWidth/2,x1,
				y+nHeight),pDC,TRUE);
			DrawHorLine(CRect(x+nWidth/2+2,y+nHeight-nWidth/2,
				x1-nWidth/2-2,y+nHeight+nWidth/2),pDC,TRUE);
				        
			break;
		}
	case 4:
		{
			DrawHorLine(CRect(x+nWidth/2+2,y,x1-nWidth/2-2,
				y+nWidth),pDC,FALSE);
			DrawVerLine(CRect(x,y+nWidth/2,x+nWidth,
				y+nHeight),pDC,TRUE);
			DrawVerLine(CRect(x,y1-nHeight,x+nWidth,
				y1-nWidth/2),pDC,FALSE);
			DrawHorLine(CRect(x+nWidth/2+2,y1-nWidth,
				x1-nWidth/2-2,y1),pDC,FALSE);
			DrawVerLine(CRect(x1-nWidth,y+nHeight,x1,
				y1-nWidth/2),pDC,TRUE);
			DrawVerLine(CRect(x1-nWidth,y+nWidth/2,x1,
				y+nHeight),pDC,TRUE);
			DrawHorLine(CRect(x+nWidth/2+2,y+nHeight-nWidth/2,
				x1-nWidth/2-2,y+nHeight+nWidth/2),pDC,TRUE);
				        
			break;
		}
	case 5:
		{
			DrawHorLine(CRect(x+nWidth/2+2,y,x1-nWidth/2-2,
				y+nWidth),pDC,TRUE);
			DrawVerLine(CRect(x,y+nWidth/2,x+nWidth,
				y+nHeight),pDC,TRUE);
			DrawVerLine(CRect(x,y1-nHeight,x+nWidth,
				y1-nWidth/2),pDC,FALSE);
			DrawHorLine(CRect(x+nWidth/2+2,y1-nWidth,
				x1-nWidth/2-2,y1),pDC,TRUE);
			DrawVerLine(CRect(x1-nWidth,y+nHeight,x1,
				y1-nWidth/2),pDC,TRUE);
			DrawVerLine(CRect(x1-nWidth,y+nWidth/2,x1,
				y+nHeight),pDC,FALSE);
			DrawHorLine(CRect(x+nWidth/2+2,y+nHeight-nWidth/2,
				x1-nWidth/2-2,y+nHeight+nWidth/2),pDC,TRUE);
				        
			break;
		}
	case 6:
		{
			DrawHorLine(CRect(x+nWidth/2+2,y,x1-nWidth/2-2,
				y+nWidth),pDC,TRUE);
			DrawVerLine(CRect(x,y+nWidth/2,x+nWidth,
				y+nHeight),pDC,TRUE);
			DrawVerLine(CRect(x,y1-nHeight,x+nWidth,
				y1-nWidth/2),pDC,TRUE);
			DrawHorLine(CRect(x+nWidth/2+2,y1-nWidth,
				x1-nWidth/2-2,y1),pDC,TRUE);
			DrawVerLine(CRect(x1-nWidth,y+nHeight,x1,
				y1-nWidth/2),pDC,TRUE);
			DrawVerLine(CRect(x1-nWidth,y+nWidth/2,x1,
				y+nHeight),pDC,FALSE);
			DrawHorLine(CRect(x+nWidth/2+2,y+nHeight-nWidth/2,
				x1-nWidth/2-2,y+nHeight+nWidth/2),pDC,TRUE);
				        
			break;
		}
	case 7:
		{
			DrawHorLine(CRect(x+nWidth/2+2,y,x1-nWidth/2-2,
				y+nWidth),pDC,TRUE);
			DrawVerLine(CRect(x,y+nWidth/2,x+nWidth,
				y+nHeight),pDC,FALSE);
			DrawVerLine(CRect(x,y1-nHeight,x+nWidth,
				y1-nWidth/2),pDC,FALSE);
			DrawHorLine(CRect(x+nWidth/2+2,y1-nWidth,
				x1-nWidth/2-2,y1),pDC,FALSE);
			DrawVerLine(CRect(x1-nWidth,y+nHeight,x1,
				y1-nWidth/2),pDC,TRUE);
			DrawVerLine(CRect(x1-nWidth,y+nWidth/2,x1,
				y+nHeight),pDC,TRUE);
			DrawHorLine(CRect(x+nWidth/2+2,y+nHeight-nWidth/2,
				x1-nWidth/2-2,y+nHeight+nWidth/2),pDC,FALSE);
				        
			break;
		}
	case 9:
		{
			DrawHorLine(CRect(x+nWidth/2+2,y,x1-nWidth/2-2,
				y+nWidth),pDC,TRUE);
			DrawVerLine(CRect(x,y+nWidth/2,x+nWidth,
				y+nHeight),pDC,TRUE);
			DrawVerLine(CRect(x,y1-nHeight,x+nWidth,
				y1-nWidth/2),pDC,FALSE);
			DrawHorLine(CRect(x+nWidth/2+2,y1-nWidth,
				x1-nWidth/2-2,y1),pDC,TRUE);
			DrawVerLine(CRect(x1-nWidth,y+nHeight,x1,
				y1-nWidth/2),pDC,TRUE);
			DrawVerLine(CRect(x1-nWidth,y+nWidth/2,x1,
				y+nHeight),pDC,TRUE);
			DrawHorLine(CRect(x+nWidth/2+2,y+nHeight-nWidth/2,
				x1-nWidth/2-2,y+nHeight+nWidth/2),pDC,TRUE);
				        
			break;
		}
	}
}

// 画椭圆
void CGenDigitalClock::DrawEllipse(CPoint cp1 , 
								   CPoint cp2, 
								   int radius, 
								   CDC* pDC)
{
	pDC->SelectObject(&inside);

	pDC->Ellipse(CRect(cp1.x-radius,cp1.y-radius,
		               cp1.x+radius,cp1.y+radius));
	pDC->Ellipse(CRect(cp2.x-radius,cp2.y-radius,
		               cp2.x+radius,cp2.y+radius));
}

// 设定时间
void CGenDigitalClock::SetTime(short hour, 
							   short minute, 
							   short second, 
							   CRect rect, 
							   CDC* pDC)
{
	int x = rect.left;
	int x1 = rect.right;
	int y = rect.top;
	int y1 = rect.bottom;
	int nWidth = x1-x;
	fundal.DeleteObject();
	fundal.CreateSolidBrush(c_fundal);
	inside.DeleteObject();
	inside.CreateSolidBrush(c_inside);
	line.DeleteObject();
	line.CreatePen(PS_SOLID,1,c_line);

	int sM = nWidth*7/100;
	int sm = nWidth*2/100;

	int hh = hour;
	int mm = minute;
	int ss = second;

	int nTime[7];
	nTime[1] = hh/10;
	nTime[2] = hh%10;
	nTime[3] = mm/10;
	nTime[4] = mm%10;
	nTime[5] = ss/10;
	nTime[6] = ss%10;

	int nWidth_Tmp = nWidth*12/100;
	int sus = (y1-y)*2/100;
	CRect dr[7];
	dr[1].left = sm;
	dr[1].right = dr[1].left+nWidth_Tmp;
	dr[1].top = sus;
	dr[1].bottom = y1-sus;

	dr[2].left = dr[1].right+sm;
	dr[2].right = dr[2].left+nWidth_Tmp;
	dr[2].top = sus;
	dr[2].bottom = y1-sus;
	
	dr[3].left = dr[2].right+sM;
	dr[3].right = dr[3].left+nWidth_Tmp;
	dr[3].top = sus;
	dr[3].bottom = y1-sus;

	dr[4].left = dr[3].right+sm;
	dr[4].right = dr[4].left+nWidth_Tmp;
	dr[4].top = sus;
	dr[4].bottom = y1-sus;

	dr[5].left = dr[4].right+sM;
	dr[5].right = dr[5].left+nWidth_Tmp;
	dr[5].top = sus;
	dr[5].bottom = y1-sus;

	dr[6].left = dr[5].right+sm;
	dr[6].right = dr[6].left+nWidth_Tmp;
	dr[6].top = sus;
	dr[6].bottom = y1-sus;

	for (int i = 1; i <= 6; i++)
	{
		DrawRectForTime(nTime[i],dr[i],pDC);
	}

	CPoint p1,p2;
	p1.x = dr[2].right + sM/2;
	p1.y = y+((y1-y)/8)*3;
	p2.x = p1.x;
	p2.y = y+((y1-y)/8)*6;
	DrawEllipse(p1,p2,sM/4,pDC);

	p1.x = dr[4].right + sM/2;
	p1.y = y + ((y1-y)/8)*3;
	p2.x = p1.x;
	p2.y = y + ((y1-y)/8)*6;
	DrawEllipse(p1,p2,sM/4,pDC);
}

⌨️ 快捷键说明

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