gpt4 book ai didi

c++ - 更改 C++ 控制台应用程序的背景颜色

转载 作者:行者123 更新时间:2023-11-30 04:49:01 25 4
gpt4 key购买 nike

如果我想使用绿色、红色、蓝色以外的颜色,我该如何更改控制台应用程序的前景色?以下是我的部分代码:

case GreenFlag:
indicator = GreenFlag;
SetConsoleTextAttribute(GetStdHandle (STD_OUTPUT_HANDLE), BACKGROUND_GREEN);
cout << "Green message" << endl;
break;
case OrangeFlag:
indicator = OrangeFlag;
// SetConsoleTextAttribute(GetStdHandle (STD_OUTPUT_HANDLE), BACKGROUND_ORANGE);
cout << "Orange message" << endl;
break;
case RedFlag:
indicator = RedFlag;
SetConsoleTextAttribute(GetStdHandle (STD_OUTPUT_HANDLE), BACKGROUND_RED);
cout << "Red message" << endl;
break;
case WhiteFlag:
indicator = WhiteFlag;
// SetConsoleTextAttribute(GetStdHandle (STD_OUTPUT_HANDLE), BACKGROUND_WHITE);
cout << "White message" << endl;
break;
etc...

最佳答案

你问:

how to use colors other than green, red, blue?

您可以组合这些标志来创建新颜色:

An application can combine the foreground and background constants to achieve different colors. For example, the following combination results in bright cyan text on a blue background.

FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY | BACKGROUND_BLUE

If no background constant is specified, the background is black, and if no foreground constant is specified, the text is black. For example, the following combination produces black text on a white background.

BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED

除此之外,您可以在每个字符上单独设置颜色和/或修改屏幕缓冲区属性:

Each screen buffer character cell stores the color attributes for the colors used in drawing the foreground (text) and background of that cell. An application can set the color data for each character cell individually, storing the data in the Attributes member of the CHAR_INFO structure for each cell. The current text attributes of each screen buffer are used for characters subsequently written or echoed by the high-level functions.

An application can use GetConsoleScreenBufferInfo to determine the current text attributes of a screen buffer and the SetConsoleTextAttribute function to set the character attributes. Changing a screen buffer's attributes does not affect the display of characters previously written. These text attributes do not affect characters written by the low-level console I/O functions (such as the WriteConsoleOutput or WriteConsoleOutputCharacter function), which either explicitly specify the attributes for each cell that is written or leave the attributes unchanged.

有关文档和示例,请参阅: https://learn.microsoft.com/en-us/windows/console/using-the-high-level-input-and-output-functions

关于c++ - 更改 C++ 控制台应用程序的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55571202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com