- #include "graphics.h"
- #include "conio.h"
- #include "time.h"
- #include "math.h"
- #define PI 3.14159
- #define sz_l 30
- #define sz_w 5
- #define sz_c RGB(255,255,255)
- #define fz_l 50
- #define fz_w 3
- #define fz_c RGB(255,255,255)
- #define mz_l 70
- #define mz_w 2
- #define mz_c RGB(255,255,255)
- time_t tt;
- struct tm tf;
- IMAGE face;
- void Draw(int hour,int minute,int second)
- {
- int x_hour,y_hour,x_minute,y_minute,x_second,y_second;
- double a_hour,a_minute,a_second;
- a_second=second*2*PI/60;
- a_minute=minute*2*PI/60+a_second/60;
- a_hour=hour*2*PI/12+a_minute/12;
- x_second=100+(int)(mz_l*sin(a_second));
- y_second=100-(int)(mz_l*cos(a_second));
- x_minute=100+(int)(fz_l*sin(a_minute));
- y_minute=100-(int)(fz_l*cos(a_minute));
- x_hour=100+(int)(sz_l*sin(a_hour));
- y_hour=100-(int)(sz_l*cos(a_hour));
- setwritemode(R2_XORPEN);
- setlinestyle(PS_SOLID,NULL,sz_w);
- setcolor(sz_c);
- line(100,100,x_hour,y_hour);
- setlinestyle(PS_SOLID,NULL,fz_w);
- setcolor(fz_c);
- line(100,100,x_minute,y_minute);
- setlinestyle(PS_SOLID,NULL,mz_w);
- setcolor(mz_c);
- line(100,100,x_second,y_second);
- setwritemode(R2_BLACK);
- fillcircle(100,100,4);
- }
- void main()
- {
- int hou,min,sec;
- loadimage(&face, "IMAGE", "钟面");
- initgraph(200,200); // 初始化图形窗口
- // 获取窗口句柄
- HWND hWnd = GetHWnd();
- // 获取窗口边框宽高
- int cx = GetSystemMetrics(SM_CXFIXEDFRAME);
- int cy = GetSystemMetrics(SM_CYFIXEDFRAME) + GetSystemMetrics(SM_CYCAPTION);
- // 设置圆形区域
- HRGN rgn = CreateEllipticRgn(0 + cx, 0 + cy, 200 + cx, 200 + cy);
- SetWindowRgn(hWnd, rgn, true);
- putimage(0,0,&face);
- MOUSEMSG m; // 定义鼠标消息
- time (&tt);
- tf=*localtime (&tt);
- hou=tf.tm_hour;
- min=tf.tm_min;
- sec=tf.tm_sec;
- Draw(hou,min,sec);
- while(true)
- {
- time (&tt);
- tf=*localtime (&tt);
- if(hou!=tf.tm_hour||min!=tf.tm_min||sec!=tf.tm_sec)
- {
- Draw(hou,min,sec);
- hou=tf.tm_hour;
- min=tf.tm_min;
- sec=tf.tm_sec;
- Draw(hou,min,sec);
- }
- if (MouseHit())
- {
- m=GetMouseMsg(); // 获取一条鼠标消息
- switch(m.uMsg)
- {
- case WM_LBUTTONDOWN:
- // 如果左键按下,告诉 windows 点在了标题栏上
- PostMessage(hWnd, WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM(m.x, m.y));
- break;
- case WM_RBUTTONUP: // 按鼠标右键退出程序
- closegraph();
- exit(0);
- }
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/270820135343.html
来源: http://www.codesnippet.cn/detail/270820135343.html