Change entire console background color (Win32 C++)
Try something like:
system("color c2");
I think the FillConsoleOutputAttribute
function will do what you need. Set it to the starting coordinate of the console, and set nLength
to the number of characters in the console (width * length
).
BOOL WINAPI FillConsoleOutputAttribute(
__in HANDLE hConsoleOutput,
__in WORD wAttribute,
__in DWORD nLength,
__in COORD dwWriteCoord,
__out LPDWORD lpNumberOfAttrsWritten
);
I know this is an old question, but how about this code:
#include <windows.h>
#include <iostream>
VOID WINAPI SetConsoleColors(WORD attribs);
int main() {
SetConsoleColors(BACKGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY);
std::cout << "Hello, world!" << std::endl;
std::cin.get();
return 0;
}
VOID WINAPI SetConsoleColors(WORD attribs) {
HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFOEX cbi;
cbi.cbSize = sizeof(CONSOLE_SCREEN_BUFFER_INFOEX);
GetConsoleScreenBufferInfoEx(hOutput, &cbi);
cbi.wAttributes = attribs;
SetConsoleScreenBufferInfoEx(hOutput, &cbi);
}
As far as I know this code should work on Windows Vista and later versions. By the way, this code is based on this article (mainly the sources on the article): http://cecilsunkure.blogspot.fi/2011/12/windows-console-game-set-custom-color.html