Change entire console background color (Win32 C++)
How can I change the entire console's background color? I've tried SetConsoleTextAttribute
and it only changes the background color of new text.
I effectively want the entire console to turn red when a serious error arises.
Thanks to everyone who attempts to help.
试试像这样:
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
链接地址: http://www.djcxy.com/p/87420.html上一篇: 如何更改控制台中的前景和背景文字颜色?