It can be done and the whole background can be set to desired color with SetConsoleScreenBufferInfoEx. The code below should not mess with the previous console output, especially if it used colors:
#include "Windows.h"
void FlashConsoleBackgroundColor(int cntFlashes, int flashInterval_ms, COLORREF color)
{
CONSOLE_SCREEN_BUFFER_INFOEX sbInfoEx;
sbInfoEx.cbSize = sizeof(CONSOLE_SCREEN_BUFFER_INFOEX);
HANDLE consoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfoEx(consoleOut, &sbInfoEx);
COLORREF storedBG = sbInfoEx.ColorTable[0];
for (int i = 0; i < cntFlashes; ++i)
{
//-- set BG color
Sleep(flashInterval_ms);
sbInfoEx.ColorTable[0] = color;
SetConsoleScreenBufferInfoEx(consoleOut, &sbInfoEx);
//-- restore previous color
Sleep(flashInterval_ms);
sbInfoEx.ColorTable[0] = storedBG;
SetConsoleScreenBufferInfoEx(consoleOut, &sbInfoEx);
}
}
int main()
{
printf("Flashing console BG: RED");
FlashConsoleBackgroundColor(20, 50, RGB(255, 0, 0));
printf("\rFlashing console BG: ORANGE\n");
FlashConsoleBackgroundColor(10, 100, RGB(255, 105, 0));
return 0;
}
color 4f
, and that's it. :-) – Harlanharlandsystem("cmd /c \"color 4F\"")
. – Breathtaking