Вы можете использовать SetConsoleTextAttribute function:
BOOL WINAPI SetConsoleTextAttribute(
__in HANDLE hConsoleOutput,
__in WORD wAttributes
);
Вот краткий пример, на который вы можете посмотреть.
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <winnt.h>
#include <stdio.h>
using namespace std;
int main(int argc, char* argv[])
{
HANDLE consolehwnd = GetStdHandle(STD_OUTPUT_HANDLE);
cout << "this text is not colorized\n";
SetConsoleTextAttribute(consolehwnd, FOREGROUND_RED);
cout << "this text shows as red\n";
SetConsoleTextAttribute(consolehwnd, FOREGROUND_BLUE);
cout << "this text shows as blue\n";
}
Эта функция влияет на текст, написанный послевызов функции.Итак, наконец, вы, вероятно, хотите восстановить исходный цвет / атрибуты.Вы можете использовать GetConsoleScreenBufferInfo , чтобы записать исходный цвет в самом начале и выполнить сброс w / SetConsoleTextAttribute
в конце.