如何在Windows控制台上放置颜色

介绍 (Introduction)

Now Windows have support for ANSI colors!

现在Windows支持ANSI颜色!

On old builds (Windows 10 1909 or prior), Windows just recognizes the ESC code 1, but it is possible for Windows to recognize the ESC code \033 (or other depending on the language) with a modification in the record (regedit.exe).

在较旧的版本(Windows 10 1909或更早版本)上,Windows只能识别ESC代码1 ,但是Windows可以通过记录中的修改( regedit.exe )来识别ESC代码\ 033(或其他取决于语言) )。

修改 (The Modification)

For modification, you need to run the command below (on CMD as administrator):

要进行修改,您需要运行以下命令(在CMD上以管理员身份):

reg add HKEY_CURRENT_USER\Console /v VirtualTerminalLevel /t REG_DWORD /d 0x00000001 /f

In case of error (it'll have no error, but...), here is the command to undo: (on CMD as administrator)

发生错误时(不会有错误,但是...),以下是撤消命令:(在CMD上以管理员身份)

reg add HKEY_CURRENT_USER\Console /v VirtualTerminalLevel /t REG_DWORD /d 0x00000000 /f

With this modification, you can compile the same program for Windows and Linux without changing anything; before to color the text of the console on Python for Linux, you used the ESC code, on Windows you used a library, so you had to write the same program twice!

通过此修改,您可以为Windows和Linux编译相同的程序,而无需进行任何更改。 在为Linux上的Python着色控制台文本之前,您使用了ESC代码,在Windows上使用了库,因此您必须编写相同的程序两次!

色彩 (Colors)

ColorForegroundBackground
Black ■<ESC>[30m<ESC>[40m
Red ■<ESC>[31m<ESC>[41m
Green ■<ESC>[32m<ESC>[42m
Yellow ■<ESC>[33m<ESC>[43m
Blue ■<ESC>[34m<ESC>[44m
Magenta ■<ESC>[35m<ESC>[45m
Cyan ■<ESC>[36m<ESC>[46m
Light gray ■<ESC>[37m<ESC>[47m
Dark gray ■<ESC>[90m<ESC>[100m
Light red ■<ESC>[91m<ESC>[101m
Light green ■<ESC>[92m<ESC>[102m
Light yellow ■<ESC>[93m<ESC>[103m
Light blue ■<ESC>[94m<ESC>[104m
Light magenta ■<ESC>[95m<ESC>[105m
Light cyan ■<ESC>[96m<ESC>[106m
White ■<ESC>[97m<ESC>[107m
Bold<ESC>[1m-
Underline<ESC>[4m-
No underline<ESC>[24m-
Negative(reverse the foreground and background)<ESC>[7m-
Positive(no negative)<ESC>[27m-
Default<ESC>[0m-
颜色 前景 背景
黑色■ <ESC> [30m <ESC> [40m
红色■ <ESC> [31m <ESC> [41m
绿色■ <ESC> [32m <ESC> [42m
黄色■ <ESC> [33m <ESC> [43m
蓝色■ <ESC> [34m <ESC> [44m
洋红色■ <ESC> [35m <ESC> [45m
青色■ <ESC> [36m <ESC> [46m
浅灰色■ <ESC> [37m <ESC> [47m
深灰色■ <ESC> [90m <ESC> [100m
浅红色■ <ESC> [91m <ESC> [101m
浅绿色■ <ESC> [92m <ESC> [102m
浅黄色■ <ESC> [93m <ESC> [103m
浅蓝色■ <ESC> [94m <ESC> [104m
浅品红色■ <ESC> [95m <ESC> [105m
浅青色■ <ESC> [96m <ESC> [106m
白■ <ESC> [97m <ESC> [107m
胆大 <ESC> [1m --
强调 <ESC> [4m --
没有下划线 <ESC> [24m --
负片(反转前景和背景) <ESC> [7m --
正(无负) <ESC> [27m --
默认 <ESC> [0m --

To use a color, you need to write the ANSI color, your text and the ANSI color default.

要使用颜色,您需要编写ANSI颜色,文本和ANSI颜色默认值。

Here goes an example of this in Python:

这是Python中的一个示例:

print("\033[31mThis is red\033[0m")

In C:

在C中:

int main() {
    printf("\033[33mThis is yellow\033[0m");
    return 0;
}

In C++:

在C ++中:

#include <iostream>

int main() {
    std::cout << "\033[32mThis is green\033[0m" << std::endl;
    return 0;
}

In C#:

在C#中:

class Program {
    static void Main() {
        System.Console.WriteLine("\u001b[35mThis is purple\u001b[0m");
    }
}

In Java:2

在Java中: 2

public class Color {
    public static void main(String[] args) {
        System.out.println("\033[36mThis is cyan\033[0m");
    }
}

And in batch:

并分批:

echo [34mThis is blue[0m

Access this site for more information and others ANSIs.

访问此站点以获取更多信息和其他ANSI。

它在哪里工作? (Where Does It Work?)

This method of the modification works (that I tested) in Python, C, C++ and Java, but doesn't work in batch, there the <ESC> has to be , and in C#, there the <ESC> has to be \u001b.

这种修改方法在Python,C,C ++和Java中有效(我测试过),但是不能批量工作,必须<ESC>必须存在,在C#中必须<ESC>必须\ u001b。

我为什么要使用它? (Why Would I Use That?)

With this method, you can compile for Linux without modification in Python, C, C++, C# and Java.

使用这种方法,您可以在Linux中进行编译,而无需在Python,C,C ++,C#和Java中进行修改。

笔记 (Notes)

1 The character is a rectangle, if a question appears for you, just copy and paste it into Notepad and it will appear correctly.

1字符是一个矩形,如果出现问题,只需将其复制并粘贴到记事本中,它将正确显示。

2 If you click on jar file, nothing will happen, to run you have to call from the prompt, for that run the command lower in the folder that is jar file:

2如果单击jar文件,将不会发生任何事情,要运行该程序,必须从提示符处调用该命令,然后在jar文件所在的文件夹中运行以下命令:

java -jar "program.jar"

where program.jar is the name of the jar file.

其中program.jar是jar文件的名称。

翻译自: https://www.codeproject.com/Tips/5255355/How-to-Put-Color-on-Windows-Console

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值