好久没敲代码了,手有点生——一个小小的时钟

  1 /********************************************
  2  * 程序名称:MR.DUAN 的数字时钟
  3  * 作  者:WindAutumn <duanxu@outlook.com>
  4  * 最后修改:2012-7-25-PM
  5  * 版 本 号:1.0
  6  *
  7  *   //仅显示时钟
  8  *
  9  * 以后有空再修改
 10  * *****************************************/
 11 
 12 #include<stdio.h>
 13 #include<Windows.h>
 14 #include<time.h>
 15 
 16 #define X_OFFSET 5
 17 #define Y_OFFSET 5
 18 #define MAXWIDE 80
 19 #define MAXHIGH 24
 20 
 21 void InitScreen(HANDLE hClock);
 22 void HideCursor(HANDLE hClock);
 23 void GotoXY(HANDLE hClock, int x, int y);
 24 void PrintNumber(HANDLE hClock, int x, int y, int n, int mode);
 25 void PrintTime(HANDLE hClock);
 26 
 27 const char number[11][5]= {{0xe0,0xa0,0xa0,0xa0,0xe0},{0x20,0x20,0x20,0x20,0x20},
 28     {0xe0,0x20,0xe0,0x80,0xe0},{0xe0,0x20,0xe0,0x20,0xe0},
 29     {0xa0,0xa0,0xe0,0x20,0x20},{0xe0,0x80,0xe0,0x20,0xe0},
 30     {0xe0,0x80,0xe0,0xa0,0xe0},{0xe0,0x20,0x20,0x20,0x20},
 31     {0xe0,0xa0,0xe0,0xa0,0xe0},{0xe0,0xa0,0xe0,0x20,0xe0},
 32     {0x00,0x40,0x00,0x40,0x00}
 33 };
 34 
 35 void main()
 36 {
 37     HANDLE hClock = GetStdHandle(STD_OUTPUT_HANDLE);
 38     system("color 7b");
 39     SetConsoleTitle("MR.DUAN 的时钟");
 40     HideCursor(hClock);
 41     InitScreen(hClock);
 42     PrintTime(hClock);
 43 }
 44 
 45 void InitScreen(HANDLE hClock)// 打印周围红框框
 46 {
 47     int i;
 48     SetConsoleTextAttribute(hClock, FOREGROUND_INTENSITY | FOREGROUND_RED |
 49                             BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED);// 红色字体,白色背景
 50     GotoXY(hClock, 0, 0);
 51     printf("");
 52     for(i=1; i<(MAXWIDE-1)/2; i++)
 53         printf("");
 54     printf("");
 55 
 56     for(i=1; i< MAXHIGH; i++)
 57     {
 58         GotoXY(hClock, 0, i);
 59         printf("");
 60         GotoXY(hClock, MAXWIDE-2, i);
 61         printf("");
 62     }
 63 
 64     GotoXY(hClock, 0, MAXHIGH);
 65     printf("");
 66     for(i=1; i<(MAXWIDE-1)/2; i++)
 67         printf("");
 68     printf("");
 69 
 70     GotoXY(hClock, 0, 0);
 71 }
 72 
 73 void HideCursor(HANDLE hClock)
 74 {
 75     CONSOLE_CURSOR_INFO cursor_info = {1, 0};
 76     SetConsoleCursorInfo(hClock, &cursor_info);
 77 }
 78 
 79 void GotoXY(HANDLE hClock, int x, int y)
 80 {
 81     COORD coord;
 82     coord.X = x;
 83     coord.Y = y;
 84     SetConsoleCursorPosition(hClock, coord);
 85 }
 86 
 87 void PrintNumber(HANDLE hClock, int x, int y, int n, int mode)
 88 {
 89     int i,j;
 90     char temp;
 91     char * string = mode? "":"  ";
 92     for(i=0; i<5; i++)
 93     {
 94         GotoXY(hClock,x,y+i);
 95         for(j=0; j<3; j++)
 96         {
 97             temp = number[n][i]<<j & 0x80;
 98             if(temp == (char) 0x80)
 99                 printf(string);
100             else printf("  ");
101         }
102     }
103 }
104 
105 void PrintTime(HANDLE hClock)
106 {
107     time_t rawtime = time(0);
108     struct tm * timenow = localtime(&rawtime);
109     SetConsoleTextAttribute(hClock,FOREGROUND_INTENSITY | FOREGROUND_BLUE |
110                             BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED);
111     while(1)
112     {
113         while(rawtime != time(0))
114         {
115             rawtime = time(0);
116             timenow = localtime(&rawtime);
117 
118             PrintNumber(hClock,X_OFFSET,Y_OFFSET,timenow->tm_hour/10,1);
119             PrintNumber(hClock,X_OFFSET+7,Y_OFFSET,timenow->tm_hour%10,1);
120             PrintNumber(hClock,X_OFFSET+14,Y_OFFSET,10,timenow->tm_sec%2);
121             PrintNumber(hClock,X_OFFSET+21,Y_OFFSET,timenow->tm_min/10,1);
122             PrintNumber(hClock,X_OFFSET+28,Y_OFFSET,timenow->tm_min%10,1);
123             PrintNumber(hClock,X_OFFSET+36,Y_OFFSET,10,timenow->tm_sec%2);
124             PrintNumber(hClock,X_OFFSET+42,Y_OFFSET,timenow->tm_sec/10,1);
125             PrintNumber(hClock,X_OFFSET+49,Y_OFFSET,timenow->tm_sec%10,1);
126         }
127     }
128 }
原文地址:https://www.cnblogs.com/doodle777/p/3160004.html