使用 C++ 对控制台中的文本进行着色
如何使用 C++ 将彩色文本写入控制台?也就是说,如何用不同的颜色写出不同的文字?
How can I write colored text to the console with C++? That is, how can I write different text with different colors?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
为控制台文本添加一点颜色
角色属性
以下是“k”值的解释方式。
Add a little Color to your Console Text
Character Attributes
Here is how the "k" value be interpreted.
ANSI 转义颜色代码:
C/C++ 的示例代码:
GCC:
ANSI escape color codes :
Sample code for C/C++ :
GCC :
标准 C++ 没有“颜色”的概念。所以你要问的取决于操作系统。
对于 Windows,您可以查看 SetConsoleTextAttribute 函数。
在 *nix 上,您必须使用 ANSI 转义序列。
Standard C++ has no notion of 'colors'. So what you are asking depends on the operating system.
For Windows, you can check out the SetConsoleTextAttribute function.
On *nix, you have to use the ANSI escape sequences.
我发现适用于 Windows 的仅标头开源 C++ 库
https://github.com/imfl/color-console
颜色控制台:
tabulate:
如果您不仅想更改颜色,还想以更易读的形式(例如以表格形式)打印文本,它也是 https://github.com/p-ranav/tabulate 可以在控制台中更改颜色和绘制表格。
I've found header-only open-source C++ library working for Windows
https://github.com/imfl/color-console
Color Console:
tabulate:
If You want not only to change colors but print text in more readable form (e.g. in form of tabular) it is also https://github.com/p-ranav/tabulate which can change colors and draw tables in console.
您可以像这样编写方法和调用
You can write methods and call like this
在 Windows 10 上,您可以这样使用转义序列:
On Windows 10 you may use escape sequences this way:
这是我的轻量级解决方案,适用于 Windows 和 Linux:
下面是如何使用它的示例:
Here is my solution that is lightweight and works with both Windows and Linux:
And here is an example how to use it:
您可以使用 ANSI 转义序列对控制台文本进行着色,它适用于 Windows 和 Linux。对于Windows,您需要激活虚拟终端。
You can use ANSI escape sequences to colorizing the console text, it works for windows and Linux. For Windows, you need to activate the virtual terminal.
在 Windows 上,执行此操作的最简单方法是:
其中“F”是背景颜色的代码,3 是文本颜色的代码。
尝试一下看看其他颜色组合:
注意:我只在 Windows 上进行了测试。作品。正如所指出的,这不是跨平台的,它不适用于 Linux 系统。
On Windows, the simplest way you can do this is:
Where "F" is the code for the background color and 3 is the code for the text color.
Mess around with it to see other color combinations:
Note: I only tested on Windows. Works. As pointed out, this is not cross-platform, it will not work on Linux systems.
在 Windows 中,您可以在前景(文本)和背景上使用红绿蓝的任意组合。
来源: https:// msdn.microsoft.com/en-us/library/windows/desktop/ms682088(v=vs.85).aspx#_win32_character_attributes
In Windows, you can use any combination of red green and blue on the foreground (text) and the background.
Source: https://msdn.microsoft.com/en-us/library/windows/desktop/ms682088(v=vs.85).aspx#_win32_character_attributes
假设您正在谈论 Windows 控制台窗口,请在 MSDN 库文档中查找控制台功能。
否则,或者更一般地说,它取决于控制台。 C++ 库不支持颜色。但是用于控制台处理的库可能/将会支持颜色。例如谷歌“ncurses 颜色”。
对于连接的串行终端和终端仿真器,您可以通过输出“转义序列”来控制事物。这些通常以 ASCII 27(ASCII 中的转义字符)开头。有一个 ANSI 标准和许多自定义方案。
Assuming you're talking about a Windows console window, look up the console functions in the MSDN Library documentation.
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".
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.
我不确定您真正想要做什么,但我猜测您希望 C++ 程序在控制台中输出彩色文本,对吧?不了解 Windows,但在所有 Unices(包括 Mac OS X)上,您只需使用 ANSI 转义序列为此。
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 sequences for that.
这里 cplusplus 示例 是如何在控制台中使用颜色的示例。
Here cplusplus example is an example how to use colors in console.
如果您不希望整个屏幕充满颜色,请不要使用“system(”Color …”)”。这是制作彩色文本所需的脚本:
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:
我制作了一个方便的跨平台解决方案,可以在Windows和Linux上运行
此代码使用宏来分离Windows和Linux之间的逻辑
I have made a convenient cross-platform solution that works on Windows and on Linux
This code uses macros to separate logic between windows and linux
您不需要使用任何库。只需编写 system("color 4f");
You don't need to use any library. Just only write system("color 4f");