C++ 如何使用 ANSI 转义码在控制台上输出彩色文本

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

How to use the ANSI Escape code for outputting colored text on Console

c++ccolors

提问by goldenmean

I read about ANSI-C escape codes here. Tried to use it in C/C++ printf/cout to colorize the text outputted to consolde but without sucess.

在这里阅读了 ANSI-C 转义码。试图在 C/C++ printf/cout 中使用它对输出到 consolde 的文本进行着色,但没有成功。

Code:

代码:

#include <iostream>

 #include <cstdio>

int main()
{

    int a=3, b=5;
    int &ref = a;

    ref = b;

    //cout << "5\m" << a << b <<'\n'; //here it prints m→m 5, no colored text
    printf("5\m %d",a); //here to it prints same - m→m 5, 

    getchar();

}

How to use these escape codes to output colored text to console?

如何使用这些转义码将彩色文本输出到控制台?

Am i missing something?

我错过了什么吗?

EDIT:In some C++ code I saw a call to this function

编辑:在一些 C++ 代码中,我看到了对这个函数的调用

textcolor(10);

But it gives compilation errors in g++ and in Visual Studio. Which compiler had this function available? Any details?

但是它在 g++ 和 Visual Studio 中给出了编译错误。哪个编译器有这个功能?任何细节?

回答by pau.estalella

I'm afraid you forgot the ESC character:

恐怕你忘记了 ESC 字符:

#include <cstdio>

int main()
{
    printf("%c[%dmHELLO!\n", 0x1B, 32);
}

Unfortunately it will only work on consoles that support ANSI escape sequences (like a linux console using bash, or old Windows consoles that used ansi.sys)

不幸的是,它只适用于支持 ANSI 转义序列的控制台(如使用 bash 的 linux 控制台,或使用 ansi.sys 的旧 Windows 控制台)

回答by Baltasarq

I created a very simple text-management librarysome time ago, being multiplatform, it uses native API calls for Windows and ANSI escape sequences for the rest of the platforms. It is fully documented and you can also browse the source code.

前段时间我创建了一个非常简单的文本管理库,它是多平台的,它对 Windows 使用本机 API 调用,对其余平台使用 ANSI 转义序列。它有完整的文档,您还可以浏览源代码。

About your specific question, I think you are missing some codes. For example, in order to change the color of text, you should use something like:

关于您的具体问题,我认为您缺少一些代码。例如,为了改变文本的颜色,你应该使用类似的东西:

static const char * CSI = "[";
printf( "%s%s", CSI, "31m" );   // RED

Hope this helps.

希望这可以帮助。

回答by Shades

A note to anybody reading this post: https://en.wikipedia.org/wiki/ANSI_escape_code#DOS_and_Windows

任何阅读这篇文章的人的注意事项:https: //en.wikipedia.org/wiki/ANSI_escape_code#DOS_and_Windows

In 2016, Microsoft released the Windows 10 Version 1511 update which unexpectedly implemented support for ANSI escape sequences. The change was designed to complement the Windows Subsystem for Linux, adding to the Windows Console Host used by Command Prompt support for character escape codes used by terminal-based software for Unix-like systems. This is not the default behavior and must be enabled programmatically with the Win32 API via SetConsoleMode(handle, ENABLE_VIRTUAL_TERMINAL_PROCESSING)

2016 年,微软发布了 Windows 10 Version 1511 更新,意外地实现了对 ANSI 转义序列的支持。此更改旨在补充 Linux 的 Windows 子系统,为命令提示符使用的 Windows 控制台主机添加对基于终端的软件用于类 Unix 系统的字符转义码的支持。这不是默认行为,必须通过 Win32 API 以编程方式启用SetConsoleMode(handle, ENABLE_VIRTUAL_TERMINAL_PROCESSING)

回答by Shades

ANSI escape codes worked on DOS using the ansi.sys device driver. They won't work windows xp or higher. You need to use the console APISetConsoleTextAttribute()

ANSI 转义码使用 ansi.sys 设备驱动程序在 DOS 上工作。它们不能在 windows xp 或更高版本上运行。您需要使用控制台 APISetConsoleTextAttribute()

textcolorwas available in the borland turbo c++ compiler.

textcolor在 borland turbo c++ 编译器中可用。

回答by stansy

Windows 10 supports ANSI Escape Sequences on the VT100 and derived terminal emulator technologies with 256 color extension. Description and examples are on the page Console Virtual Terminal Sequences.

Windows 10 支持 VT100 上的 ANSI 转义序列和具有 256 色扩展的衍生终端仿真器技术。描述和示例位于控制台虚拟终端序列页面上。

std::ostringstream ss;
for (int i = 0; i < 10; ++i)
    ss << "\x1b[38;2;" << 5 * i << ";" << 255 - 10 * i << ";220m" 
        << "ANSI Escape Sequence " << i << std::endl;   
std::cout << ss.str();

回答by user2019716

under windows 10 one can use VT100 style by activating the VT100 mode in the current console :

在 Windows 10 下,可以通过在当前控制台中激活 VT100 模式来使用 VT100 样式:

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

#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
#define DISABLE_NEWLINE_AUTO_RETURN  0x0008

int main(){
   DWORD l_mode;
   HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
   GetConsoleMode(hStdout,&l_mode);
   SetConsoleMode( hStdout, l_mode |
                ENABLE_VIRTUAL_TERMINAL_PROCESSING |
                DISABLE_NEWLINE_AUTO_RETURN );

   for (int i = 0; i < 10; ++i)
      std::cout << "\x1b[38;2;" << 5 * i << ";" << 255 - 10 * i << ";220m" 
             << "ANSI Escape Sequence " << i << std::endl;
}

see msdn page : [https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences][1]

请参阅 msdn 页面:[ https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences][1]

回答by Cubic

ANSI formatting codes aren't supported in windows.

Windows 不支持 ANSI 格式代码。

http://en.wikipedia.org/wiki/ANSI_escape_code

http://en.wikipedia.org/wiki/ANSI_escape_code