使用 C++ 为控制台中的文本着色

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

Colorizing text in the console with C++

c++colorscolorize

提问by Sudantha

How can I write colored text to the console with C++? That is, how can I write different text with different colors?

如何使用 C++ 将彩色文本写入控制台?也就是说,如何用不同的颜色编写不同的文本?

回答by Sheen

Add a little Color to your Console Text

为您的控制台文本添加一点颜色

  HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  // you can loop k higher to see more color choices
  for(int k = 1; k < 255; k++)
  {
    // pick the colorattribute k you want
    SetConsoleTextAttribute(hConsole, k);
    cout << k << " I want to be nice today!" << endl;
  }

alt text

替代文字

Character AttributesHere is how the "k" value be interpreted.

字符属性下面是解释“k”值的方式。

回答by user225312

Standard C++ has no notion of 'colors'. So what you are asking depends on the operating system.

标准 C++ 没有“颜色”的概念。所以你问的是什么取决于操作系统。

For Windows, you can check out the SetConsoleTextAttributefunction.

对于 Windows,您可以查看SetConsoleTextAttribute函数。

On *nix, you have to use the ANSIescape sequences.

在 *nix 上,您必须使用ANSI转义序列。

回答by Mehdi Mohammadpour

ANSI escape color codes :

ANSI 转义颜色代码:

Name            BG  FG
Black           30  40
Red             31  41
Green           32  42
Yellow          33  43
Blue            34  44
Magenta         35  45
Cyan            36  46
White           37  47
Bright Black    90  100
Bright Red      91  101
Bright Green    92  102
Bright Yellow   93  103
Bright Blue     94  104
Bright Magenta  95  105
Bright Cyan     96  106
Bright White    97  107

Sample code for C/C++ :

C/C++ 示例代码:

#include <iostream>
#include <string>

int main(int argc, char ** argv){

    printf("\n");
    printf("\x1B[31mTexting3[0m\t\t");
    printf("\x1B[32mTexting3[0m\t\t");
    printf("\x1B[33mTexting3[0m\t\t");
    printf("\x1B[34mTexting3[0m\t\t");
    printf("\x1B[35mTexting3[0m\n");

    printf("\x1B[36mTexting3[0m\t\t");
    printf("\x1B[36mTexting3[0m\t\t");
    printf("\x1B[36mTexting3[0m\t\t");
    printf("\x1B[37mTexting3[0m\t\t");
    printf("\x1B[93mTexting3[0m\n");

    printf("3[3;42;30mTexting3[0m\t\t");
    printf("3[3;43;30mTexting3[0m\t\t");
    printf("3[3;44;30mTexting3[0m\t\t");
    printf("3[3;104;30mTexting3[0m\t\t");
    printf("3[3;100;30mTexting3[0m\n");

    printf("3[3;47;35mTexting3[0m\t\t");
    printf("3[2;47;35mTexting3[0m\t\t");
    printf("3[1;47;35mTexting3[0m\t\t");
    printf("\t\t");
    printf("\n");

    return 0;
}

GCC :

海湾合作委员会:

g++ cpp_interactive_terminal.cpp -o cpp_interactive_terminal.cgi
chmod +x cpp_interactive_terminal.cgi
./cpp_interactive_terminal.cgi

回答by Sudantha

You can write methods and call like this

您可以编写方法并像这样调用



HANDLE  hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
int col=12;

// color your text in Windows console mode
// colors are 0=black 1=blue 2=green and so on to 15=white  
// colorattribute = foreground + background * 16
// to get red text on yellow use 4 + 14*16 = 228
// light red on yellow would be 12 + 14*16 = 236

FlushConsoleInputBuffer(hConsole);
SetConsoleTextAttribute(hConsole, col);

cout << "Color Text";

SetConsoleTextAttribute(hConsole, 15); //set back to black background and white text

回答by Cheers and hth. - Alf

Assuming you're talking about a Windows console window, look up the console functions in the MSDN Library documentation.

假设您在谈论 Windows 控制台窗口,请在 MSDN 库文档中查找控制台功能。

Otherwise, or more generally, it depends on the console. Colors are not supported by the C++ library. But a library for console handling may/will support colors. E.g. google "ncurses colors".

否则,或更一般地说,这取决于控制台。C++ 库不支持颜色。但是用于控制台处理的库可能/将支持颜色。例如谷歌“ncurses 颜色”。

For connected serial terminals and terminal emulators you can control things by outputting "escape sequences". These typically start with ASCII 27 (the escape character in ASCII). There is an ANSI standard and a lot of custom schemes.

对于连接的串行终端和终端仿真器,您可以通过输出“转义序列”来控制事物。这些通常以 ASCII 27(ASCII 中的转义字符)开头。有一个 ANSI 标准和许多自定义方案。

回答by DarkDust

I'm not sure what you really want to do, but my guess is you want your C++ program to output colored text in the console, right ? Don't know about Windows, but on all Unices (including Mac OS X), you'd simply use ANSI escape sequencesfor that.

我不确定您真正想要做什么,但我猜您希望 C++ 程序在控制台中输出彩色文本,对吗?不了解 Windows,但在所有 Unices(包括 Mac OS X)上,您只需为此使用ANSI 转义序列

回答by Krzysztof Buchacz

On Windows 10 you may use escape sequences this way:

在 Windows 10 上,您可以通过以下方式使用转义序列:

#ifdef _WIN32
SetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), ENABLE_VIRTUAL_TERMINAL_PROCESSING);
#endif
// print in red and restore colors default
std::cout << "3[32m" << "Error!" << "3[0m" << std::endl;

回答by cambunctious

In Windows, you can use any combination of red green and blue on the foreground (text) and the background.

在 Windows 中,您可以在前景(文本)和背景上使用红绿蓝的任意组合。

/* you can use these constants
FOREGROUND_BLUE
FOREGROUND_GREEN
FOREGROUND_RED
FOREGROUND_INTENSITY
BACKGROUND_BLUE
BACKGROUND_GREEN
BACKGROUND_RED
BACKGROUND_INTENSITY
*/

HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
std::cout << "I'm cyan! Who are you?" << std::endl;

Source: https://msdn.microsoft.com/en-us/library/windows/desktop/ms682088(v=vs.85).aspx#_win32_character_attributes

来源:https: //msdn.microsoft.com/en-us/library/windows/desktop/ms682088(v=vs.85).aspx#_win32_character_attributes

回答by Inconnu

The simplest way you can do is:

你可以做的最简单的方法是:

#include <stdlib.h>

system("Color F3");

Where "F" is the code for the background color and 3 is the code for the text color.

其中“F”是背景颜色的代码,3 是文本颜色的代码。

Mess around with it to see other color combinations:

弄乱它以查看其他颜色组合:

system("Color 1A");
std::cout << "Hello, what is your name?" << std::endl;
system("Color 3B");
std::cout << "Hello, what is your name?" << std::endl;
system("Color 4c");
std::cout << "Hello, what is your name?" << std::endl;

回答by Microprod Software

Do not use "system("Color …")" if you don't want the entire screen to be filled up with color. This is the script needed to make colored text:

如果您不想让整个屏幕充满颜色,请不要使用 "system("Color ...")"。这是制作彩色文本所需的脚本:

#include <iostream>
#include <windows.h>

int main()
{
const WORD colors[] =
{
0x1A, 0x2B, 0x3C, 0x4D, 0x5E, 0x6F,
0xA1, 0xB2, 0xC3, 0xD4, 0xE5, 0xF6
};

HANDLE hstdin = GetStdHandle(STD_INPUT_HANDLE);
HANDLE hstdout = GetStdHandle(STD_OUTPUT_HANDLE);
WORD   index = 0;


    SetConsoleTextAttribute(hstdout, colors[index]);
    std::cout << "Hello world" << std::endl;
FlushConsoleInputBuffer(hstdin);
return 0;
}