windows 更改整个控制台背景颜色(Win32 C++)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6460932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change entire console background color (Win32 C++)
提问by Smurf64
How can I change the entire console's background color? I've tried SetConsoleTextAttribute
and it only changes the background color of new text.
如何更改整个控制台的背景颜色?我试过了SetConsoleTextAttribute
,它只会改变新文本的背景颜色。
I effectively want the entire console to turn red when a serious error arises.
当出现严重错误时,我实际上希望整个控制台变成红色。
Thanks to everyone who attempts to help.
感谢所有试图提供帮助的人。
采纳答案by wxffles
Try something like:
尝试类似:
system("color c2");
回答by Adam Maras
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
).
我认为该FillConsoleOutputAttribute
功能可以满足您的需求。将其设置nLength
为控制台的起始坐标,并设置为控制台中的字符数(width * length
)。
BOOL WINAPI FillConsoleOutputAttribute(
__in HANDLE hConsoleOutput,
__in WORD wAttribute,
__in DWORD nLength,
__in COORD dwWriteCoord,
__out LPDWORD lpNumberOfAttrsWritten
);
回答by MaGetzUb
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
据我所知,这段代码应该适用于 Windows Vista 和更高版本。顺便说一下,这段代码是基于这篇文章(主要是文章上的来源):http: //cecilsunkure.blogspot.fi/2011/12/windows-console-game-set-custom-color.html
回答by TheMaster Moh
HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(out, 0x9 | 0x70);
// text color from 0x1-0x9
// text background color from 0x10-0x90
system("color d1");
/*
Sets the default console foreground and background colors
COLOR [attr]
attr Specifies color attribute of console output
Color attributes are specified by TWO hex digits -- the first
corresponds to the background; the second the foreground. Each digit
can be any of the following values:
0 = Black 8 = Gray
1 = Blue 9 = Light Blue
2 = Green A = Light Green
3 = Aqua B = Light Aqua
4 = Red C = Light Red
5 = Purple D = Light Purple
6 = Yellow E = Light Yellow
7 = White F = Bright White
If no argument is given, this command restores the color to what it was
when CMD.EXE started. This value either comes from the current console
window, the /T command line switch or from the DefaultColor registry
value.
The COLOR command sets ERRORLEVEL to 1 if an attempt is made to execute
the COLOR command with a foreground and background color that are the
same.
/*
回答by Killzone Kid
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:
它可以完成,并且可以使用SetConsoleScreenBufferInfoEx将整个背景设置为所需的颜色。下面的代码不应与之前的控制台输出混淆,特别是如果它使用了颜色:
#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;
}
回答by grizzly
回答by Pulseczar
This works for me. It changes the background color without messing up the foreground color of text already displayed, by changing each console character cell, one at a time. You will need to get the handle to your console output buffer, which I believe is done with GetStdHandle().
这对我有用。它通过一次更改每个控制台字符单元格来更改背景颜色,而不会弄乱已显示文本的前景色。您需要获取控制台输出缓冲区的句柄,我相信这是通过GetStdHandle() 完成的。
DWORD written = 0;
COORD writeCoord = {0};
WORD attribute;
for (int y = 0; y < consoleBufferLength; y++) // rows
{
for (int x = 0; x < consoleBufferWidth; x++) // columns
{
writeCoord.X = x; writeCoord.Y = y;
ReadConsoleOutputAttribute(consoleOutputHandle, &attribute, 1, writeCoord, &written);
attribute &= 0xFF0F; // zero the background color
attribute |= 12 << 4; // change the background color to red
FillConsoleOutputAttribute(consoleOutputHandle, attribute, 1, writeCoord, &written);
}
}