代码效果如图
#undef UNICODE
#undef _UNICODE
#include<graphics.h>
#include<conio.h>
#include<math.h>#define width 640
#define high 480
#define PI 3.14159int main()
{initgraph(width, high);int center_x, center_y;center_x = width / 2;center_y = high / 2;int secondlen = width / 5;int minutelen = width / 6;int hourlen = width / 7;int secondend_x, secondend_y;int minuteend_x, minuteend_y;int hourend_x, hourend_y;float secondangle;float minuteangle;float hourangle;SYSTEMTIME ti;BeginBatchDraw();while (1){setbkcolor(RGB(0,200,200));//只是设置背景色,还未填充背景cleardevice();//用背景色来清空背景setlinestyle(PS_SOLID, 2);//画表盘setcolor(WHITE);circle(center_x, center_y, width / 4);int x, y, i;//画刻度for (i = 0; i < 60; i++){x = center_x + int(width / 4.3 * sin(PI * 2 * i / 60));y = center_y - int(width / 4.3 * cos(PI * 2 * i / 60));if (i % 15 == 0)solidrectangle(x - 5, y - 5, x + 5, y + 5);else if (i % 5 == 0)circle(x, y, 3);elseputpixel(x, y, WHITE);}outtextxy(center_x - 25, center_y + width / 6, "我的时钟");GetLocalTime(&ti);//获取当前电脑时间secondangle = ti.wSecond * 2 * PI / 60;minuteangle = ti.wMinute * 2 * PI / 60 + secondangle / 60;hourangle = ti.wHour * 2 * PI / 12 + minuteangle / 12;secondend_x = center_x + secondlen * sin(secondangle);secondend_y = center_y - secondlen * cos(secondangle);minuteend_x = center_x + minutelen * sin(minuteangle);minuteend_y = center_y - minutelen * cos(minuteangle);hourend_x = center_x + hourlen * sin(hourangle);hourend_y = center_y - hourlen * cos(hourangle);setlinestyle(PS_SOLID, 2);//画时针分针和秒针setcolor(YELLOW);line(center_x, center_y, secondend_x, secondend_y);setlinestyle(PS_SOLID, 4);setcolor(BLUE);line(center_x, center_y, minuteend_x, minuteend_y);setlinestyle(PS_SOLID, 6);setcolor(RED);line(center_x, center_y, hourend_x, hourend_y);FlushBatchDraw();Sleep(10);setcolor(BLACK);//隐藏前一秒的时针分针和秒针setlinestyle(PS_SOLID, 2);line(center_x, center_y, secondend_x, secondend_y);setlinestyle(PS_SOLID, 4);line(center_x, center_y, minuteend_x, minuteend_y);setlinestyle(PS_SOLID, 6);line(center_x, center_y, hourend_x, hourend_y);}EndBatchDraw();_getch();closegraph();return 0;
}