编写时环境: VS2010,ege15.04
以下是源代码,临时兴起所写,有些小BUG,作为示例项目使用,目前还没有添加退出,请自行任务栏关闭程序
#include <graphics.h> #include <Windows.h> #include <math.h> #include <time.h> #define PI 3.1416 typedef struct PROP { //圆心 ege_point round; //屏幕分辨率 ege_point scr; //各个刻度半径 float radiusH; float radiusM; float radiusS; //刻度线长度 float caliLeng; //刻度坐标 ege_point pHStart[360]; ege_point pHEnd[360]; ege_point pMStart[360]; ege_point pMEnd[360]; ege_point pSStart[360]; ege_point pSEnd[360]; struct tm *pTime; float ms; PIMAGE img[60]; }PROP; PROP prop; static inline int GetScrX(){return GetSystemMetrics(SM_CXSCREEN);} static inline int GetScrY(){return GetSystemMetrics(SM_CYSCREEN);} //初始化界面上线条数据 void Init() { //设置分辨率 prop.scr.x = GetScrX(); prop.scr.y = GetScrY(); //设置圆心 prop.round.y = prop.scr.y / 2.0; prop.round.x = prop.scr.x / 2.0; //设置各个刻度半径 prop.radiusS = prop.scr.y / 2.0 - prop.scr.y / 20.0; prop.radiusM = prop.scr.y / 2.0 - prop.scr.y / 10.0; prop.radiusH = prop.scr.y / 2.0 - (prop.scr.y / 20.0) * 3; //设置刻度线长度 prop.caliLeng = prop.scr.y / 20.0; //设置刻度坐标 for (float r = 0.0;r < 360.0 ; r = r + 1.0) { //计算秒针 prop.pSStart[int(r)].x = prop.round.x + prop.radiusS * cos(r * 1.0 * PI / 180.0); prop.pSStart[int(r)].y = prop.round.y + prop.radiusS * sin(r * 1.0 * PI / 180.0); prop.pSEnd[int(r)].x = prop.round.x + (prop.radiusS - prop.scr.y / 40.0) * cos(r * 1.0 * PI / 180.0); prop.pSEnd[int(r)].y = prop.round.y + (prop.radiusS - prop.scr.y / 40.0) * sin(r * 1.0 * PI / 180.0); //计算分针 prop.pMStart[int(r)].x = prop.round.x + prop.radiusM * cos(r * 1.0 * PI / 180.0); prop.pMStart[int(r)].y = prop.round.y + prop.radiusM * sin(r * 1.0 * PI / 180.0); prop.pMEnd[int(r)].x = prop.round.x + (prop.radiusM - prop.scr.y / 40.0) * cos(r * 1.0 * PI / 180.0); prop.pMEnd[int(r)].y = prop.round.y + (prop.radiusM - prop.scr.y / 40.0) * sin(r * 1.0 * PI / 180.0); //计算时针 prop.pHStart[int(r)].x = prop.round.x + prop.radiusH * cos(r * 1.0 * PI / 180.0); prop.pHStart[int(r)].y = prop.round.y + prop.radiusH * sin(r * 1.0 * PI / 180.0); prop.pHEnd[int(r)].x = prop.round.x + (prop.radiusH - prop.scr.y / 40.0) * cos(r * 1.0 * PI / 180.0); prop.pHEnd[int(r)].y = prop.round.y + (prop.radiusH - prop.scr.y / 40.0) * sin(r * 1.0 * PI / 180.0); } } //初始化文字图片 void InitNumber() { setfont(prop.scr.y / 50.0,0,"宋体"); for (int i = 0 ; i < 60 ; i++) { xyprintf(prop.scr.y / 50.0, prop.scr.y / 50.0,"%d",i); prop.img[i] = newimage(); getimage(prop.img[i],prop.scr.y / 50.0,prop.scr.y / 50.0, prop.scr.y / 50.0,prop.scr.y / 50.0); cleardevice(); //putimage_rotate(NULL,prop.img[i],20 * i,100,0.5f,0.5f,i / 60.0 * (float)i,0,255,0); //delay_ms(1000); } } //获取本地时间 void GetLocalTime() { time_t timep; time(&timep); prop.pTime = localtime(&timep); } void GetMs() { DWORD pt = GetTickCount(); prop.ms = ((float)(pt % 1000)) / 1000.0; } //绘制秒 刻度 void DrawSecLine(float sec) { for (int i = 359 ; i >= 0; i --) { if ((i + 360 - ((int)(sec * 6.0))) % 6 == 0) { ege_line(prop.pSStart[i].x, prop.pSStart[i].y, prop.pSEnd[i].x, prop.pSEnd[i].y); putimage_rotate(NULL,prop.img[((i/6)+ 15 - (int)(sec) + 60)%60], prop.pSEnd[i].x, prop.pSEnd[i].y,0.5f,0.5f, (((float)-i) + 270.0) * PI / 180.0, 0,255,0); } } } //绘制分 刻度 void DrawMinLine(float min) { for (int i = 0 ; i < 360; i ++) { if ((i + 360 - ((int)(min * 6.0))) % 6 == 0) { ege_line(prop.pMStart[i].x, prop.pMStart[i].y, prop.pMEnd[i].x, prop.pMEnd[i].y); putimage_rotate(NULL,prop.img[((i/6)+ 15 - (int)(min) + 60)%60], prop.pMEnd[i].x, prop.pMEnd[i].y,0.5f,0.5f, (((float)-i) + 270.0) * PI / 180.0, 0,255,0); } } } //绘制时 刻度 void DrawHourLine(float hour) { for (int i = 0 ; i < 360; i ++) { if ((i + 360 - ((int)(hour * 6.0))) % 30 == 0) { ege_line(prop.pHStart[i].x, prop.pHStart[i].y, prop.pHEnd[i].x, prop.pHEnd[i].y); int j = (((i/6)+ 15 - (int)(hour) + 60)%60)/ 5; putimage_rotate(NULL,prop.img[j?j:12], prop.pHEnd[i].x, prop.pHEnd[i].y,0.5f,0.5f, (((float)-i) + 270.0) * PI / 180.0, 0,255,0); } } } //绘制指针 void DrawPoint() { setfillcolor(EGEACOLOR(0xF0, RED)); ege_point ep[3] = { { prop.round.x-5,prop.round.y }, { prop.round.x+5,prop.round.y }, { prop.round.x,prop.round.y - prop.scr.y / 3 } }; ege_fillpoly(3,ep); //ege_line(prop.round.x,prop.round.y,prop.round.x,prop.round.y - prop.scr.y / 3); } int main(void) { setinitmode(INIT_NOBORDER); initgraph(-1,-1); setcolor(EGEACOLOR(0xF0, GREEN)); //设置抗锯齿 ege_enable_aa(true); //初始化数据 Init(); //初始化图片 InitNumber(); while (true) { GetMs(); GetLocalTime(); DrawSecLine(60.0 - float(prop.pTime->tm_sec) - prop.ms); DrawMinLine(60.0 - float(prop.pTime->tm_min) - float(prop.pTime->tm_sec / 60.0)); DrawHourLine(60.0 - (prop.pTime->tm_hour % 12 + prop.pTime->tm_min / 60.0) * 5); DrawPoint(); xyprintf(0,240,"毫秒:%.3f",prop.ms); xyprintf(0,200,"%d年%d月%d时 星期%d %d时%d分%d秒", prop.pTime->tm_year + 1900, prop.pTime->tm_mon + 1, prop.pTime->tm_mday, prop.pTime->tm_wday, prop.pTime->tm_hour, prop.pTime->tm_min, prop.pTime->tm_sec ); delay_fps(10); cleardevice(); } }
项目:EGE_动态时钟屏保程序