用easyx写抗锯齿了的有表盘的时钟
时间: 2023-05-11 15:04:44 浏览: 144
easyx做的时钟
5星 · 资源好评率100%
非常感谢您的提问!以下是用easyx写抗锯齿了的有表盘的时钟的代码:
```cpp
#include <graphics.h>
#include <time.h>
#include <math.h>
#define PI 3.14159265358979323846
int main()
{
initgraph(640, 480);
// 设置字体
LOGFONT font;
ZeroMemory(&font, sizeof(font));
font.lfHeight = 24;
font.lfWeight = FW_BOLD;
_tcscpy_s(font.lfFaceName, _T("微软雅黑"));
HFONT hFont = CreateFontIndirect(&font);
SelectObject(GetImageHDC(), hFont);
// 获取当前时间
time_t t = time(NULL);
struct tm* tm = localtime(&t);
while (true)
{
// 获取当前时间
t = time(NULL);
tm = localtime(&t);
// 清空画布
setbkcolor(WHITE);
cleardevice();
// 绘制表盘
setlinecolor(BLACK);
setlinestyle(PS_SOLID, 2);
circle(320, 240, 200);
for (int i = 0; i < 60; i++)
{
double angle = i * PI / 30;
int x1 = 320 + 180 * sin(angle);
int y1 = 240 - 180 * cos(angle);
int x2 = 320 + 190 * sin(angle);
int y2 = 240 - 190 * cos(angle);
if (i % 5 == 0)
{
setlinestyle(PS_SOLID, 4);
line(x1, y1, x2, y2);
}
else
{
setlinestyle(PS_SOLID, 2);
line(x1, y1, x2, y2);
}
}
// 绘制时针
setlinecolor(BLACK);
setlinestyle(PS_SOLID, 6);
double angle = (tm->tm_hour % 12 + tm->tm_min / 60.0) * PI / 6;
int x = 320 + 100 * sin(angle);
int y = 240 - 100 * cos(angle);
line(320, 240, x, y);
// 绘制分针
setlinecolor(BLACK);
setlinestyle(PS_SOLID, 4);
angle = tm->tm_min * PI / 30;
x = 320 + 140 * sin(angle);
y = 240 - 140 * cos(angle);
line(320, 240, x, y);
// 绘制秒针
setlinecolor(RED);
setlinestyle(PS_SOLID, 2);
angle = tm->tm_sec * PI / 30;
x = 320 + 160 * sin(angle);
y = 240 - 160 * cos(angle);
line(320, 240, x, y);
// 绘制数字
settextcolor(BLACK);
settextstyle(24, 0, _T("微软雅黑"));
for (int i = 1; i <= 12; i++)
{
angle = (i - 3) * PI / 6;
x = 320 + 160 * sin(angle);
y = 240 - 160 * cos(angle);
TCHAR str[3];
_stprintf_s(str, _T("%d"), i);
outtextxy(x - 10, y - 12, str);
}
// 刷新画面
Sleep(1000);
flushbatchdraw();
}
return 0;
}
```
希望这个代码对您有所帮助!
阅读全文