多功能电子时钟

多功能电子时钟

编译运行结果在本文最后以图片方式呈现
编译器需支持graphics.h以实现图形化界面
如果没有则需要在官网上下载easyx

#include <graphics.h>               // 引用图形库头文件
#include <conio.h> 
#include <stdio.h>
#include <windows.h>		    	//用到了定时函数sleep()
#include <math.h>
#include <string.h>
#include <mmsystem.h>              //导入声音头文件
#pragma comment(lib, "Winmm.lib")  //音效头文件

#define PI 3.1416
#pragma warning(disable:4996)
int r[3][4] = { {30,20,130,60},{170,20,220,60},{260,20,310,60} };//三个按钮的二维数组
int shi, fen, miao, sum;

//画表盘
  void DralDial()
  {
	int i;
	int x1, y1, x2, y2, x5, y5; //表心坐标系坐标 
	char rome[][3] = { "12","1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11" }; 
	//画四个圆 
	setlinestyle(PS_SOLID, 4);
	setcolor(RGB(255, 255, 224));
	circle(300, 300, 250);
	circle(300, 300, 30);
	circle(300, 300, 260);
	circle(300, 300, 270);


	for (i = 0; i < 60; i++) {

		// 画60条短线 
		//setbkmode(OPAQUE);//文字背景
		setcolor(RGB(255, 105, 180));
		x1 = (int)(300 + (sin(i * PI / 30) * 250));
		y1 = (int)(300 - (cos(i * PI / 30) * 250));
		x2 = (int)(300 + (sin(i * PI / 30) * 260));
		y2 = (int)(300 - (cos(i * PI / 30) * 260));
		line(x1, y1, x2, y2);


		//写表盘数字 
		if (i % 5 == 0) {
			x5 = (int)(290 + (sin((i - 0.2) * PI / 30) * 220));
			y5 = (int)(290 - (cos((i - 0.2) * PI / 30) * 220));
			setfont(24, 0, "黑体");
			setcolor(RGB(255, 255, 0));
			outtextxy(x5, y5, rome[i / 5]);
		}
	}
}

void DrawHand(int hour, int minute, int second)
{


	int xhour, yhour, xminute, yminute, xsecond, ysecond; //表心坐标系指针坐标 

	xhour = (int)(80 * sin(hour * PI / 6 + minute * PI / 360 + second * PI / 1800));
	yhour = (int)(80 * cos(hour * PI / 6 + minute * PI / 360 + second * PI / 1800));
	xminute = (int)(145 * sin(minute * PI / 30 + second * PI / 1800));
	yminute = (int)(145 * cos(minute * PI / 30 + second * PI / 1800));
	xsecond = (int)(200 * sin(second * PI / 30));
	ysecond = (int)(200 * cos(second * PI / 30));


	//画时针 
	setlinestyle(PS_SOLID, 13);
	setcolor(RGB(0,255,255));
	line(300 + xhour, 300 - yhour, 300 - xhour / 6, 300 + yhour / 6);


	//画分针 
	setlinestyle(PS_SOLID, 7);
	setcolor(RGB(222, 158, 107));
	line(300 + xminute, 300 - yminute, 300 - xminute / 4, 300 + yminute / 4);


	//画秒针 
	setlinestyle(PS_SOLID, 3);
	setcolor(RGB(225,215,0));
	line(300 + xsecond, 300 - ysecond, 300 - xsecond / 3, 300 + ysecond / 3);
}


void DrawDigitalClock(int hour, int minute, int second)
{
	char str[20];
	sprintf(str, "时间:%2d:%2d:%2d", hour, minute, second);
	setfont(50, 0, "隶书");
	setcolor(RGB(255, 215, 0));
	outtextxy(130, 680, str);
}


void DrawDecoration()
{
	char s[] = "制作者:eason邱嘎嘎joseph";
	setfont(35, 0, "隶书");
	setcolor(RGB(255, 250, 205));
	outtextxy(80, 650, s);
}


//按钮判断函数
int button_judge(int x, int y)
{
	if (x > r[0][0] && x<r[0][2] && y>r[0][1] && y < r[0][3])return 1;
	if (x > r[1][0] && x<r[1][2] && y>r[1][1] && y < r[1][3])return 2;
	if (x > r[2][0] && x<r[2][2] && y>r[2][1] && y < r[2][3])return 3;
	return 0;
}

//初始化图像
void init_figure()
{
	int i;
	setrop2(R2_COPYPEN);//当前颜色
	setlinecolor(BLACK);
	setlinestyle(PS_SOLID);//实线
	rectangle(30, 100, 420, 330);//外框线
	setlinestyle(PS_DOT);//点线
	for (i = 30 + 39; i < 420; i += 39)
	{
		line(i, 100, i, 330);//竖直辅助线
	}
	for (i = 100 + 23; i < 330; i += 23)
	{
		line(30, i, 420, i);//水平辅助线
	}
}

int main()
{
	int i, event = 0;
	char s[30];//输入字符串变量
	initgraph(480, 360);//初始化窗口(黑屏)
	setbkcolor(RGB(255, 205, 180));//设置背景色,原来默认黑色
	cleardevice();//清屏(取决于背景色)
	
	RECT R1 = { r[0][0],r[0][1],r[0][2],r[0][3] };
	RECT R2 = { r[1][0],r[1][1],r[1][2],r[1][3] };
	RECT R3 = { r[2][0],r[2][1],r[2][2],r[2][3] };
	LOGFONT f;//字体样式指针
	gettextstyle(&f);					//获取字体样式
	_tcscpy(f.lfFaceName, _T("宋体"));	//设置字体为宋体
	f.lfQuality = ANTIALIASED_QUALITY;    // 设置输出效果为抗锯齿  
	settextstyle(&f);                     // 设置字体样式
	settextcolor(BLACK);				//BLACK在graphic.h头文件里面被定义为黑色的颜色常量
	drawtext("显示时钟", &R1, DT_CENTER | DT_VCENTER | DT_SINGLELINE);//在矩形区域R1内输入文字,水平居中,垂直居中,单行显示
	drawtext("倒计时", &R2, DT_CENTER | DT_VCENTER | DT_SINGLELINE);//在矩形区域R2内输入文字,水平居中,垂直居中,单行显示
	drawtext("退出", &R3, DT_CENTER | DT_VCENTER | DT_SINGLELINE);//在矩形区域R3内输入文字,水平居中,垂直居中,单行显示
	setlinecolor(BLACK);
	rectangle(r[0][0], r[0][1], r[0][2], r[0][3]);
	rectangle(r[1][0], r[1][1], r[1][2], r[1][3]);
	rectangle(r[2][0], r[2][1], r[2][2], r[2][3]);
	MOUSEMSG m;//鼠标指针

	while (true)
	{
		m = GetMouseMsg();//获取一条鼠标消息

		switch (m.uMsg)
		{
		case WM_MOUSEMOVE:
			setrop2(R2_XORPEN);
			setlinecolor(LIGHTCYAN);//线条颜色为亮青色
			setlinestyle(PS_SOLID, 3);//设置画线样式为实现,10磅
			setfillcolor(WHITE);//填充颜色为白色
			if (button_judge(m.x, m.y) != 0)
			{
				if (event != button_judge(m.x, m.y))
				{
					event = button_judge(m.x, m.y);//记录这一次触发的按钮
					fillrectangle(r[event - 1][0], r[event - 1][1], r[event - 1][2], r[event - 1][3]);//有框填充矩形(X1,Y1,X2,Y2)
				}
			}
			else
			{
				if (event != 0)//上次触发的按钮未被修正为原来的颜色
				{
					fillrectangle(r[event - 1][0], r[event - 1][1], r[event - 1][2], r[event - 1][3]);//两次同或为原来颜色
					event = 0;
				}
			}
			break;
		case WM_LBUTTONDOWN:
			setrop2(R2_NOTXORPEN);//二元光栅——NOT(屏幕颜色 XOR 当前颜色)
			for (i = 0; i <= 10; i++)
			{
				setlinecolor(RGB(25 * i, 25 * i, 25 * i));//设置圆颜色
				circle(m.x, m.y, 2 * i);
				Sleep(20);//停顿30ms
				circle(m.x, m.y, 2 * i);//抹去刚刚画的圆
			}
			//按照按钮判断左键单击后的操作


			switch (button_judge(m.x, m.y))
			{
				//复原按钮原型
			case 1:
				initgraph(600, 800); //初始化600×800的绘图窗口 
				setbkcolor(RGB(255, 105, 180));
				cleardevice();
				setbkmode(TRANSPARENT);

				loadimage(NULL, "C:\\Users\\邱嘎嘎\\Pictures\\Saved Pictures\\QQ图片20201211130117.jpg", 600, 800);

				DrawDecoration(); //画装饰 
				DralDial(); //画表盘  


				setwritemode(R2_XORPEN); //设置异或绘图方式 
				SYSTEMTIME t; //定义变量保存当前时间 
		
				while (1) {
					GetLocalTime(&t); //获取当前时间 
					DrawHand(t.wHour, t.wMinute, t.wSecond); //画表针 
					Sleep(1000);
					DrawHand(t.wHour, t.wMinute, t.wSecond); //擦表针 
				}
				closegraph(); //关闭绘图窗口 
				FlushMouseMsgBuffer();//单击事件后清空鼠标消息
				break;

			case 2:
				
				InputBox(s, 30, "请依次输入小时分钟秒钟");
				
				initgraph(480, 360);//初始化窗口(黑屏)
				setbkcolor(RGB(255, 205, 180));//设置背景色,原来默认黑色
				cleardevice();//清屏(取决于背景色)
				sscanf(s,"%d%d%d", &shi, &fen, &miao);
				sum = 3600 * shi + 60 * fen + miao;
				miao = sum % 60;
				fen = ((sum % 3600) - miao) / 60;
				shi = (sum - miao - 60 * fen) / 3600;
				while (--sum >= 0)
				{
					miao = sum % 60;
					fen = ((sum % 3600) - miao) / 60;
					shi = (sum - miao - 60 * fen) / 3600;
					char str[20];
					sprintf(str, "时间:%2d:%2d:%2d", shi, fen, miao);
					setfont(50, 0, "隶书");
					setcolor(RGB(0, 0, 225));
					outtextxy(65, 120, str);
					if (sum <= 0)
					{
						break;
					}
					fflush(stdout);
					Sleep(1000);
				}
				PlaySound("刘欢 - 好汉歌.wav", NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);
				getch();
				FlushMouseMsgBuffer();//单击事件后清空鼠标消息
				break;
			case 3:
				closegraph();//关闭绘图环境
				exit(0);//正常退出
			default:			
				//printf("\r\n(%d,%d)",m.x,m.y);//打印鼠标坐标,方便调试时确定区域
				break;
			}
			break;
		}
	}
}

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

在这里插入图片描述

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值