C++ 如何更改 Windows 控制台应用程序中的文本或背景颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8285825/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:19:16  来源:igfitidea点击:

How to change text or background color in a Windows console application

c++textbackground-color

提问by Wizard

Which C++ function changes text or background color (MS Visual studio)? For example cout<<"This text";how to make "This text" red color.

哪个 C++ 函数会更改文本或背景颜色(MS Visual Studio)?例如cout<<"This text";如何使“此文本”为红色。

回答by Nasreddine

You can change the colors for a console application using Win32 and here's an example on how to:

您可以使用 Win32 更改控制台应用程序的颜色,以下是有关如何操作的示例:

#include "stdafx.h"
#include <Windows.h>
#include <iostream>

using namespace std; 

int main(void) 
{ 
    HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); 
    if (hStdout == INVALID_HANDLE_VALUE) 
    {
        cout << "Error while getting input handle" << endl;
        return EXIT_FAILURE;
    }
    //sets the color to intense red on blue background
    SetConsoleTextAttribute(hStdout, FOREGROUND_RED | BACKGROUND_BLUE | FOREGROUND_INTENSITY);

    cout << "This is intense red text on blue background" << endl;
    //reverting back to the normal color
    SetConsoleTextAttribute(hStdout, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);

    return EXIT_SUCCESS;
}

Look at the MSDN documentation for the SetConsoleTextAttributefunction and Console Screen Buffersfor more information.

有关更多信息,请查看该SetConsoleTextAttribute函数的 MSDN 文档和控制台屏幕缓冲区

A more complete example on console applications using Win32 is available here.

此处提供有关使用 Win32 的控制台应用程序的更完整示例。

回答by Kerrek SB

Colour isn't a C++ thing, but a property of your terminal. If your terminal speaks ANSI (e.g. any Linux terminal, or DOS or Windows NT if you add DEVICE=C:\DOS\ansi.systo your config.sys, or later Windows if you call the shell with cmd.exe /kansicon), then you can try the following gimmick:

颜色不是 C++ 的东西,而是终端的属性。如果您的终端使用 ANSI(例如,任何 Linux 终端,或 DOS 或 Windows NT,如果您添加DEVICE=C:\DOS\ansi.sys到您的config.sys,或更高版本的 Windows,如果您使用 调用外壳cmd.exe /kansicon),那么您可以尝试以下噱头:

#define ANSI_COLOR_RED     "\x1b[31m"
#define ANSI_COLOR_GREEN   "\x1b[32m"
#define ANSI_COLOR_YELLOW  "\x1b[33m"
#define ANSI_COLOR_BLUE    "\x1b[34m"
#define ANSI_COLOR_MAGENTA "\x1b[35m"
#define ANSI_COLOR_CYAN    "\x1b[36m"

#define ANSI_COLOR_BRIGHT  "\x1b[1m"
#define ANSI_COLOR_RESET   "\x1b[0m"


std::cout << ANSI_COLOR_RED "Hello World\n" ANSI_COLOR_RESET;

Wikipedia has a list of ANSI escape sequences.

维基百科有一个ANSI 转义序列列表

回答by Daniel Trebbien

I believe that you are looking for the SetConsoleTextAttributefunction. The first parameter, hConsoleOutput, would be the standard output handle obtained via GetStdHandle(STD_OUTPUT_HANDLE). The second parameter is a bitwise-OR (|) combination of the desired character attributes.

我相信您正在寻找该SetConsoleTextAttribute功能。第一个参数hConsoleOutput将是通过 获得的标准输出句柄GetStdHandle(STD_OUTPUT_HANDLE)。第二个参数是|所需字符属性的按位或 ( ) 组合。

See also: KB319883 How to change foreground colors and background colors of text in a Console window by using Visual C#

另请参阅:KB319883 如何使用 Visual C# 更改控制台窗口中文本的前景色和背景色