gpt4 book ai didi

c++ - 是否可以将控制台中的文本颜色和背景颜色更改为不同的颜色,同时仍在同一行上? C++

转载 作者:行者123 更新时间:2023-11-30 05:12:32 24 4
gpt4 key购买 nike

我发现的一些解决方案似乎无法在我的 Windows 终端上运行。仅将整行的文本更改为一种颜色。示例:

HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, 0 | 15);
cout << 1;

hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole,5 | 0);
cout << 2;

仅将文本和背景更改为一种颜色。感谢您的帮助!

最佳答案

是的,这是可能的。它不适合你的原因是因为你的代码是错误的。您永远不会设置背景颜色;您只需操纵前景色即可。

问题的根源几乎可以肯定是您使用的是魔数(Magic Number)( 0 | 155 | 0 )而不是 <Windows.h> 中定义的常量:

#define FOREGROUND_BLUE      0x0001 // text color contains blue.
#define FOREGROUND_GREEN 0x0002 // text color contains green.
#define FOREGROUND_RED 0x0004 // text color contains red.
#define FOREGROUND_INTENSITY 0x0008 // text color is intensified.
#define BACKGROUND_BLUE 0x0010 // background color contains blue.
#define BACKGROUND_GREEN 0x0020 // background color contains green.
#define BACKGROUND_RED 0x0040 // background color contains red.
#define BACKGROUND_INTENSITY 0x0080 // background color is intensified.

下面的代码适合我:

const HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

// yellow on blue
SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY | BACKGROUND_BLUE);
std::cout << 1;

// blue on bright green
SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_INTENSITY);
std::cout << 2;

// reset to black on white
SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
std::cout << std::endl;

这是我的控制台窗口的屏幕截图,证明它有效。 (但请注意,我的控制台的颜色已针对 Tango 主题进行了自定义,因此您的可能会使用略有不同的颜色。)

我建议通过定义枚举让自己的生活更轻松:

enum ConsoleColors
{
BlackFore = 0,
MaroonFore = FOREGROUND_RED,
GreenFore = FOREGROUND_GREEN,
NavyFore = FOREGROUND_BLUE,
TealFore = FOREGROUND_GREEN | FOREGROUND_BLUE,
OliveFore = FOREGROUND_RED | FOREGROUND_GREEN,
PurpleFore = FOREGROUND_RED | FOREGROUND_BLUE,
GrayFore = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
SilverFore = FOREGROUND_INTENSITY,
RedFore = FOREGROUND_INTENSITY | FOREGROUND_RED,
LimeFore = FOREGROUND_INTENSITY | FOREGROUND_GREEN,
BlueFore = FOREGROUND_INTENSITY | FOREGROUND_BLUE,
AquaFore = FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE,
YellowFore = FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN,
FuchsiaFore = FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE,
WhiteFore = FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,

BlackBack = 0,
MaroonBack = BACKGROUND_RED,
GreenBack = BACKGROUND_GREEN,
NavyBack = BACKGROUND_BLUE,
TealBack = BACKGROUND_GREEN | BACKGROUND_BLUE,
OliveBack = BACKGROUND_RED | BACKGROUND_GREEN,
PurpleBack = BACKGROUND_RED | BACKGROUND_BLUE,
GrayBack = BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE,
SilverBack = BACKGROUND_INTENSITY,
RedBack = BACKGROUND_INTENSITY | BACKGROUND_RED,
LimeBack = BACKGROUND_INTENSITY | BACKGROUND_GREEN,
BlueBack = BACKGROUND_INTENSITY | BACKGROUND_BLUE,
AquaBack = BACKGROUND_INTENSITY | BACKGROUND_GREEN | BACKGROUND_BLUE,
YellowBack = BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN,
FuchsiaBack = BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_BLUE,
WhiteBack = BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE,
};

然后像这样设置颜色:

SetConsoleTextAttribute(hConsole, ConsoleColors::BlackBack |
ConsoleColors::RedFore));

关于c++ - 是否可以将控制台中的文本颜色和背景颜色更改为不同的颜色,同时仍在同一行上? C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44481821/

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