先扔一个链接上来,因为怕忘:
https://blog.csdn.net/stude/article/details/7645056
https://blog.csdn.net/lindorx/article/details/78760610
以下才是代码。
做了一个控制台进度条,并显示百分比。
UPDATE: 添加定向修改字符的备忘。19.3.21 18:20
#include <iostream>
#include <windows.h>
#include <random>
#include <time.h>
#include <string> #define COLOR_WHITE 0x0F
//#define COLOR_GREEN 0x0A #define PROCESS_SIGN ">"
#define PROCESS_SPEED 6
#define PROCESS_MAX 100
#define PROCESS_RATE 9 //MAX IS 10 static std::mt19937_64 randnum;
static CONSOLE_SCREEN_BUFFER_INFO csbi;
static HANDLE hstdout; int RandNum(int min, int max)
{
if (min < )
{
std::cout << "RandNum Function Parameter Error: The min is wrong number! " << std::endl;
return -;
} if (min >= max)
{
std::cout << "RandNum Function Parameter Error: The min and max are wrong numbers! " << std::endl;
return -;
} uint64_t tmpNum = randnum();
return tmpNum % (max - min) + min;
} int PrintWith_(const char* const str, int count, WORD color = COLOR_WHITE)
{
int ret = ; SetConsoleTextAttribute(hstdout, color);
for (int i = ; i < count; i++)
{
ret += printf(str);
}
SetConsoleTextAttribute(hstdout, COLOR_WHITE); return ret;
} void Backspace(int count)
{
PrintWith_("\b", count);
} bool Init()
{
if (PROCESS_RATE > )
{
return false;
} if (PROCESS_SPEED > PROCESS_MAX)
{
return false;
} hstdout = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hstdout, &csbi); randnum.seed(GetCurrentTime()); return true;
} int main()
{
if (!Init())
{
std::cout << "Config Error!" << std::endl; ExitProcess();
return ;
} int percent = ;
int spc = ;
while (percent < )
{
int inc = RandNum(, PROCESS_SPEED);
percent += inc;
if (percent > PROCESS_MAX)
{
inc = percent - PROCESS_MAX;
percent = PROCESS_MAX;
} Backspace(spc); PrintWith_(PROCESS_SIGN, inc, FOREGROUND_INTENSITY); char percentText[];
sprintf_s(percentText, " %d%%%%", percent);
spc = PrintWith_(percentText, , FOREGROUND_GREEN); Sleep(( - PROCESS_RATE) * );
} ////TEST:
//COORD pos = { 34, 10 };
//DWORD dWrited = 0;
//FillConsoleOutputCharacter(hstdout, '*', 5, pos, &dWrited);
//FillConsoleOutputAttribute(hstdout, FOREGROUND_BLUE, 3, pos, &dWrited);
//WriteConsoleOutputCharacter(hstdout, "■", 2, pos, &dWrited); std::cout << std::endl;
system("pause"); CloseHandle(hstdout); return ;
}