C++课程设计--模拟时钟程序(MFC)

课设题目

模拟时钟程序

课设要求

①基于MFC的WINDOWS应用程序设计方法,编写一个模拟时钟程序;
②在屏幕中间有一个指针式时钟;
③这个时钟的下方或者上方以数字方式显示时间,该时间应与指针显示的时间一致;
用菜单选项打开的对话框,或者主界面里的控件设置时间。时间不必与机器系统时间相同,可任意设置。

程序运行界面

在这里插入图片描述
在这里插入图片描述

核心代码

// WeClockView.cpp : implementation of the CWeClockView class
//

#include "stdafx.h"
#include "WeClock.h"
#include <ctime>//
#include "WeClockDoc.h"
#include "WeClockView.h"
#include "SetTimeDlg.h"//
#include "math.h"//
#define PI 3.1415926//
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CWeClockView

IMPLEMENT_DYNCREATE(CWeClockView, CView)

BEGIN_MESSAGE_MAP(CWeClockView, CView)
	//{{AFX_MSG_MAP(CWeClockView)
	ON_COMMAND(ID_START, OnStart)
	ON_COMMAND(ID_STOP, OnStop)
	ON_COMMAND(ID_SETTIME, OnSettime)
	ON_WM_LBUTTONDOWN()
	ON_WM_TIMER()
	//}}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)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWeClockView construction/destruction

CWeClockView::CWeClockView()
{
	// TODO: add construction code here
	//设定时间的初始值为系统时间
	SYSTEMTIME  st;
	GetLocalTime(&st);
	day = st.wDay;
	hour = st.wHour;
	minute = st.wMinute;
	month = st.wMonth;
	second = st.wSecond;
	year = st.wYear;
	//设定画笔/画刷
	m_HouPen.CreatePen (PS_SOLID,5,RGB (0,0,0));//时针画笔,实心,宽度为5,黑色
	m_MinPen.CreatePen (PS_SOLID,3,RGB (0,0,0));//分针画笔
	m_SecPen.CreatePen (PS_SOLID,1,RGB (255,0,0));//秒针画笔
	m_LinePen.CreatePen (PS_SOLID,1,RGB (200,0,0));//矩形画笔
	m_MarkBrush.CreateSolidBrush (RGB (250,0,0));//刻度点画笔
	//设定表心位置
	m_Center.x=500;
	m_Center.y=150;
	//设定时钟半径
	m_Radius=125;
	//计算指针位置
	SetClock (hour,minute,second);
}

CWeClockView::~CWeClockView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CWeClockView drawing

CPoint CWeClockView::GetPoint(int nLenth,int nValue)
{
	CPoint p;
	double angle = nValue*PI/30-PI/2;
	p.x=m_Center.x+(int) (nLenth*cos(angle));
	p.y=m_Center.y+(int) (nLenth*sin(angle));
	return p;
}


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







void CWeClockView::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default

	if(nIDEvent)//定时器发送过来消息
	{
		second++;//秒增加
		if(second>59)
		{
			second=0;
			minute++;//分增加
		}
		if(minute>59)
		{
			minute=0;
			hour++;//小时增加
		}
		if(hour>23)
		{
			hour=0;
			day++;//日增加
		}
		switch(month)
		{
        //大月
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			if(day>31)
			{
				day=1;
				month++;//月增加
			}
			break;
			//小月
		case 4:
		case 6:
		case 9:
		case 11:
			if(day>30)
			{
				day=1;
				month++;//月增加
			}
			break;
		case 2:
			if((year%4==0&&year%100!=0&&day>29)||(year%400==0&&day>29))//闰二月
			{
				day=1;
				month++;//月增加
			}
			if(year%4!=0||year%400!=0&&day>28)//二月
			{
				day=1;
				month++;
			}
			break;
		}
		if (month>12)
		{
			year++;//年增加
			month=1;
		}
		SetClock(hour,minute,second);

		Invalidate(false);//重新绘制
	}
	CView::OnTimer(nIDEvent);
}

源码及课设报告

需要的小伙伴请在海轰的微信公众号:海轰Pro
回复:海轰
O(∩_∩)O哈哈~

发布了155 篇原创文章 · 获赞 110 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/weixin_44225182/article/details/103673068