C语言—用EaxyX绘制实时钟表

 代码效果如图

#undef UNICODE
#undef _UNICODE
#include<graphics.h>
#include<conio.h>
#include<math.h>

#define width 640
#define high 480
#define PI 3.14159

int main()
{
	initgraph(width, high);
	int center_x, center_y;
	center_x = width / 2;
	center_y = high / 2;
	int secondlen = width / 5;
	int minutelen = width / 6;
	int hourlen = width / 7;

	int secondend_x, secondend_y;
	int minuteend_x, minuteend_y;
	int hourend_x, hourend_y;
	float secondangle;
	float minuteangle;
	float hourangle;

	SYSTEMTIME ti;

	BeginBatchDraw();
	while (1)
	{
		setbkcolor(RGB(0,200,200));//只是设置背景色,还未填充背景
		cleardevice();//用背景色来清空背景

		setlinestyle(PS_SOLID, 2);//画表盘
		setcolor(WHITE);
		circle(center_x, center_y, width / 4);

		int x, y, i;//画刻度
		for (i = 0; i < 60; i++)
		{
			x = center_x + int(width / 4.3 * sin(PI * 2 * i / 60));
			y = center_y - int(width / 4.3 * cos(PI * 2 * i / 60));

			if (i % 15 == 0)
				solidrectangle(x - 5, y - 5, x + 5, y + 5);
			else if (i % 5 == 0)
				circle(x, y, 3);
			else
				putpixel(x, y, WHITE);
		}

		outtextxy(center_x - 25, center_y + width / 6, "我的时钟");

		GetLocalTime(&ti);//获取当前电脑时间
		secondangle = ti.wSecond * 2 * PI / 60;
		minuteangle = ti.wMinute * 2 * PI / 60 + secondangle / 60;
		hourangle = ti.wHour * 2 * PI / 12 + minuteangle / 12;

		secondend_x = center_x + secondlen * sin(secondangle);
		secondend_y = center_y - secondlen * cos(secondangle);

		minuteend_x = center_x + minutelen * sin(minuteangle);
		minuteend_y = center_y - minutelen * cos(minuteangle);

		hourend_x = center_x + hourlen * sin(hourangle);
		hourend_y = center_y - hourlen * cos(hourangle);

		setlinestyle(PS_SOLID, 2);//画时针分针和秒针
		setcolor(YELLOW);
		line(center_x, center_y, secondend_x, secondend_y);
		setlinestyle(PS_SOLID, 4);
		setcolor(BLUE);
		line(center_x, center_y, minuteend_x, minuteend_y);
		setlinestyle(PS_SOLID, 6);
		setcolor(RED);
		line(center_x, center_y, hourend_x, hourend_y);

		FlushBatchDraw();
		Sleep(10);

		setcolor(BLACK);//隐藏前一秒的时针分针和秒针
		setlinestyle(PS_SOLID, 2);
		line(center_x, center_y, secondend_x, secondend_y);
		setlinestyle(PS_SOLID, 4);
		line(center_x, center_y, minuteend_x, minuteend_y);
		setlinestyle(PS_SOLID, 6);
		line(center_x, center_y, hourend_x, hourend_y);
	}
	EndBatchDraw();
	_getch();
	closegraph();
	return 0;
}

相关推荐

  1. C语言easyx 做一个《正弦彩环》

    2024-04-04 22:08:03       41 阅读
  2. html css实现钟表简单移动

    2024-04-04 22:08:03       68 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-04 22:08:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-04 22:08:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-04-04 22:08:03       82 阅读
  4. Python语言-面向对象

    2024-04-04 22:08:03       91 阅读

热门阅读

  1. 视频读写和录制

    2024-04-04 22:08:03       39 阅读
  2. Vue-Router 的懒加载如何实现

    2024-04-04 22:08:03       38 阅读
  3. Docker——Dockerfile构建MySQL并初始化数据

    2024-04-04 22:08:03       35 阅读
  4. ES6参数默认值

    2024-04-04 22:08:03       33 阅读
  5. vmware unbuntu22.04卸载与安装vmtools

    2024-04-04 22:08:03       35 阅读
  6. C、C++、C#中.vscode下json文件记录

    2024-04-04 22:08:03       33 阅读