C/C++ 控制台字体的变颜变色

先扔一个链接上来,因为怕忘:

https://blog.csdn.net/stude/article/details/7645056

https://blog.csdn.net/lindorx/article/details/78760610

以下才是代码。

做了一个控制台进度条,并显示百分比。

 

UPDATE: 添加定向修改字符的备忘。19.3.21  18:20

  1 #include <iostream>
  2 #include <windows.h>
  3 #include <random>
  4 #include <time.h>
  5 #include <string>
  6 
  7 #define COLOR_WHITE 0x0F
  8 //#define COLOR_GREEN 0x0A
  9 
 10 #define PROCESS_SIGN ">"
 11 #define PROCESS_SPEED 6
 12 #define PROCESS_MAX 100
 13 #define PROCESS_RATE 9        //MAX IS 10
 14 
 15 static std::mt19937_64 randnum;
 16 static CONSOLE_SCREEN_BUFFER_INFO csbi;
 17 static HANDLE hstdout;
 18 
 19 int RandNum(int min, int max)
 20 {
 21     if (min < 0)
 22     {
 23         std::cout << "RandNum Function Parameter Error: The min is wrong number! " << std::endl;
 24         return -1;
 25     }
 26 
 27     if (min >= max)
 28     {
 29         std::cout << "RandNum Function Parameter Error: The min and max are wrong numbers! " << std::endl;
 30         return -1;
 31     }
 32 
 33     uint64_t tmpNum = randnum();
 34     return tmpNum % (max - min) + min;
 35 }
 36 
 37 
 38 int PrintWith_(const char* const str, int count, WORD color = COLOR_WHITE)
 39 {
 40     int ret = 0;
 41 
 42     SetConsoleTextAttribute(hstdout, color);
 43     for (int i = 0; i < count; i++)
 44     {
 45         ret += printf(str);
 46     }
 47     SetConsoleTextAttribute(hstdout, COLOR_WHITE);
 48 
 49     return ret;
 50 }
 51 
 52 void Backspace(int count)
 53 {
 54     PrintWith_("\b", count);
 55 }
 56 
 57 
 58 bool Init()
 59 {
 60     if (PROCESS_RATE > 10)
 61     {
 62         return false;
 63     }
 64 
 65     if (PROCESS_SPEED > PROCESS_MAX)
 66     {
 67         return false;
 68     }
 69 
 70     hstdout = GetStdHandle(STD_OUTPUT_HANDLE);
 71     GetConsoleScreenBufferInfo(hstdout, &csbi);
 72 
 73     randnum.seed(GetCurrentTime());
 74 
 75     return true;
 76 }
 77 
 78 int main()
 79 {
 80     if (!Init())
 81     {
 82         std::cout << "Config Error!" << std::endl;
 83 
 84         ExitProcess(2);
 85         return 2;
 86     }
 87 
 88     int percent = 0;
 89     int spc = 0;
 90     while (percent < 100)
 91     {
 92         int inc = RandNum(1, PROCESS_SPEED);
 93         percent += inc;
 94         if (percent > PROCESS_MAX)
 95         {
 96             inc = percent - PROCESS_MAX;
 97             percent = PROCESS_MAX;
 98         }
 99 
100         Backspace(spc);
101 
102         PrintWith_(PROCESS_SIGN, inc, FOREGROUND_INTENSITY);
103 
104         char percentText[32];
105         sprintf_s(percentText, "  %d%%%%", percent);
106         spc = PrintWith_(percentText, 1, FOREGROUND_GREEN);
107 
108         Sleep((10 - PROCESS_RATE) * 100);
109     }
110 
111     ////TEST:
112     //COORD pos = { 34, 10 };
113     //DWORD dWrited = 0;
114     //FillConsoleOutputCharacter(hstdout, '*', 5, pos, &dWrited);
115     //FillConsoleOutputAttribute(hstdout, FOREGROUND_BLUE, 3, pos, &dWrited);
116     //WriteConsoleOutputCharacter(hstdout, "■", 2, pos, &dWrited);
117 
118     std::cout << std::endl;
119     system("pause");
120 
121     CloseHandle(hstdout);
122 
123     return 0;
124 }

 

posted on 2019-03-21 16:28  __Even  阅读(717)  评论(0编辑  收藏  举报

导航