c语言+easyx写的电子时钟+表盘时钟

背景音乐和背景图片自己找一个


#include  

#include  
#include  
#include  
#include
#pragma comment(lib, "Winmm.lib") //音效头文件


#define PI 3.1415926 
 
void DralDial(); //画表盘 
void DrawHand(int hour, int minute, int second);//画表针
void DrawDigitalClock(int hour, int minute, int second);//画数字式时钟
void DrawDecoration(); //画装饰 
void backgroundmusic();//背景音乐


int main() 
{   
initgraph(600, 800); //初始化600×800的绘图窗口 
setbkcolor(WHITE); //设置背景色 
cleardevice(); //清空屏幕 
loadimage(NULL, "D:\\c语言案例作业\\时钟\\1.jpg",600,800);


DrawDecoration(); //画装饰 
DralDial(); //画表盘  
rectangle(100,650,500,750);


setwritemode(R2_XORPEN); //设置异或绘图方式 
SYSTEMTIME t; //定义变量保存当前时间 
backgroundmusic();


while(!kbhit()){ 
GetLocalTime(&t); //获取当前时间 
DrawDigitalClock(t.wHour, t.wMinute, t.wSecond); //画数字式时钟
DrawHand(t.wHour, t.wMinute, t.wSecond); //画表针 
Sleep(1000); 
DrawHand(t.wHour, t.wMinute, t.wSecond); //擦表针 

closegraph(); //关闭绘图窗口 
return 0; 



/*画表盘*/ 
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,  2);
setcolor(BLACK); 
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(BLACK);
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(RED);
outtextxy(x5 , y5, rome[i / 5]); 





//画指针 
void DrawHand(int hour, int minute, int second) 



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


xhour = (int)(130 * sin( hour * PI / 6 + minute * PI / 360 + second * PI / 1800)); 
yhour = (int)(130 * 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,  10); 
setcolor(LIGHTGRAY); 
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(RED); 
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(RED); 
outtextxy(130, 680, str);
}


void DrawDecoration()
{


char s[]="按任意键结束      制作者:";
setfont(25, 0, "黑体"); 
setcolor(BLACK); 
outtextxy(100, 600, s);


}


void backgroundmusic(){//背景音乐
PlaySound("D:\\c语言案例作业\\时钟\\1.wav", NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);
}

你可能感兴趣的:(c语言+easyx写的电子时钟+表盘时钟)