用C语言实现电子表

2019-04-15 15:13发布

//效果图

/*************************实时显示当前时间 带滴答音   废话不说直接上源程序*********************************/#include
#include         
#include
#include
#pragma comment(lib, "winmm.lib")
#define PI 3.1415926535     //定义π值
/*-------------动态表盘-------------*/
void DrawHand(int hour, int minute, int second)
{
double a_hour, a_min, a_sec;
int x_hour, y_hour, x_min, y_min, x_sec, y_sec;
a_sec = second * 2 * PI / 60;
a_min = minute * 2 * PI / 60 + a_sec / 60;
a_hour = hour * 2 * PI / 12 + a_min / 12;
//计算时分秒末端位置
x_sec = int(120 * sin(a_sec)); y_sec = int(120 * cos(a_sec));
x_min = int(100 * sin(a_min)); y_min = int(100 * cos(a_min));
x_hour = int(70 * sin(a_hour)); y_hour = int(70 * cos(a_hour));
//绘制秒针
setlinestyle(PS_SOLID, 2);
setcolor(RED);
line(320 + x_sec, 240 - y_sec, 320 - x_sec / 3, 240 + y_sec / 3);
//绘制分针
setlinestyle(PS_SOLID, 5);
setcolor(BLUE);
line(320 + x_min, 240 - y_min, 320 - x_min / 5, 240 + y_min / 5);
//绘制时针
setlinestyle(PS_SOLID, 7);
setcolor(GREEN);
line(320 + x_hour, 240 - y_hour, 320 - x_hour / 5, 240+y_hour / 5);
}
/*-------------初始化表盘-------------*/
void DrawDial()
{
//-----------初始化音乐-----------
mciSendString(L"open 秒针.mp3 alias BGM", 0, 0, 0);
mciSendString(L"play BGM repeat", 0, 0, 0);
mciSendString(L"close &BGM", 0, 0, 0);
//-----------初始化表盘-----------
initgraph(640, 480);//绘制窗口
circle(320, 240, 2);
circle(320, 240, 60);
//circle(405,210,30);
circle(320, 240, 160);
settextstyle(25, 0, _T("Bookman Old Style"));
outtextxy(285, 320, _T("ROLEX"));
//--------------3-----------------
settextstyle(15, 0, _T("新宋体"));
outtextxy(430, 233, _T("III"));
//--------------6-----------------
settextstyle(15, 0, _T("新宋体"));
outtextxy(312, 355, _T("VI"));
//--------------9----------------- 
settextstyle(15, 0, _T("新宋体"));
outtextxy(185, 233, _T("IX"));
//--------------12----------------
settextstyle(15, 0, _T("新宋体"));
outtextxy(311, 105, _T("XII"));
//-----------绘制像素点-----------
int x, y;
for (int i = 0; i < 60; i++)
{
x = 320 + int(145 * sin(PI * 2 * i / 60));
y = 240 + int(145 * cos(PI * 2 * i / 60));


if (i % 15 == 0)
bar(x - 5, y - 5, x + 5, y + 5);
else if (i % 5 == 0)
circle(x, y, 3);
else
putpixel(x, y, YELLOW);
/**------------------------------------------**/
for (int i = 0; i < 11; i++)
{
x = 320 + int(28 * sin(PI * 2 * i / 11));
y = 240 + int(28 * cos(PI * 2 * i / 11));
putpixel(x, y, LIGHTCYAN);
}
for (int i = 0; i < 30; i++)
{
x = 320 + int(100 * sin(PI * 2 * i / 30));
y = 240 + int(100 * cos(PI * 2 * i / 30));
putpixel(x, y, RED);
}
}
}


int main()
{
DrawDial();
setwritemode(R2_XORPEN);// 设置 XOR 绘图模式R2_XORPEN
SYSTEMTIME ti;          //定义变量保存当前时间
while (!_kbhit())       //按任意键退出钟表程序
{
GetLocalTime(&ti);
DrawHand(ti.wHour, ti.wMinute, ti.wSecond);//画表针
Sleep(1000);//延时一秒
DrawHand(ti.wHour, ti.wMinute, ti.wSecond);//消除表针
//清除表针
}
closegraph();
return 0;
}