C#控制台应用程序中的自定义文本颜色?

C#控制台应用程序中的自定义文本颜色?,c#,visual-studio,console,console-application,textcolor,C#,Visual Studio,Console,Console Application,Textcolor,我刚刚完成了一个项目的C#控制台应用程序代码,希望为我的字体添加一些颜色。我希望能够使用自定义颜色-橙色。有没有办法做到这一点 这是我过去用来更改颜色的代码,但它不提供橙色: Console.ForegroundColor = ConsoleColor.Magenta(and so on); 有没有办法为颜色或类似的东西插入十六进制值?控制台API不支持自定义颜色 您可以通过UI(右键单击控制台标题栏、设置、颜色)在 我相信是控制台中唯一支持的颜色。不允许使用十六进制 Black DarkBl

我刚刚完成了一个项目的C#控制台应用程序代码,希望为我的字体添加一些颜色。我希望能够使用自定义颜色-橙色。有没有办法做到这一点

这是我过去用来更改颜色的代码,但它不提供橙色:

Console.ForegroundColor = ConsoleColor.Magenta(and so on);

有没有办法为颜色或类似的东西插入十六进制值?

控制台API不支持自定义颜色

您可以通过UI(右键单击控制台标题栏、设置、颜色)

我相信是控制台中唯一支持的颜色。不允许使用十六进制

Black
DarkBlue
DarkGreen
DarkCyan
DarkRed
DarkMagenta
DarkYellow
Gray
DarkGray
Blue
Green
Cyan
Red
Magenta
Yellow
White
编辑

从我的公共回购中获取工作项目文件

但在进一步的调查中,你可以做很多工作,把红色和黄色结合起来,得到橙色。按照这里的例子。不打算重新张贴代码墙。 这不会让你获得更多的颜色,但会引导你走向正确的方向。你们将需要做一些PINVOKE的工作,但我很容易就可以得到橙色,或任何其他RGB颜色的控制台。


它不是橙色,因为该颜色不是console支持的颜色之一。我的意思是,即使使用windows API,您也无法获得它。如果要验证它,请查看以下代码:

   public static class Win32
    {
        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern bool SetConsoleTextAttribute(IntPtr hConsoleOutput, short attributes);

        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern IntPtr GetStdHandle(int nStdHandle);
    }

    public class Program
    {
        static void Main(string[] args)
        {
            foreach(var i in Enumerable.Range(0, 100)) // why "100"? it is just any number
            {
                Win32.SetConsoleTextAttribute(Win32.GetStdHandle(-11), (short)i);
                Console.WriteLine("Hello");
            }
        }
    }

进一步证明这不起作用(使用Benjamin链接中的方法):


此方法不允许您添加任何无法通过ConsoleColor访问的内容。这真是太遗憾了,因为我也想在我的应用程序中添加橙色。如果有人找到了一种方法,我会非常感兴趣。

迟做总比不做好,但现在看来这是可能的,至少在Vista和更高版本上是如此。因此,我将添加此内容,以供将来有相同问题的其他人参考

当我打算这么做的时候,我遇到了Hans Passant的:

我现在无法访问Vista,因此无法尝试。但是 这样应该行得通:

CONSOLE_SCREEN_BUFFER_INFOEX info;
info.cbSize = sizeof(info);
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfoEx(hConsole, &info);
info.ColorTable[14] = RGB(255, 128, 0);  // Replace yellow
SetConsoleScreenBufferInfoEx(hConsole, &info);
SetConsoleTextAttribute(hConsole, FOREGROUNDINTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);
这将需要一点p-调用,但应该会给你一些东西继续下去。

你可以使用它,它可以让你使用自定义颜色,甚至有Figlet字体,让你产生ASCII艺术


自从Windows 10周年更新以来,控制台可以使用ANSI/VT100颜色代码

  • 您需要通过SetConsoleMode设置标志启用虚拟终端处理(0x4)
  • 使用顺序:

    “\x1b[48;5;”+s+“m”
    -按表(0-255)中的索引设置背景色

    “\x1b[38;5;”+s+“m”
    -按表(0-255)中的索引设置前景色

    “\x1b[48;2;“+r+”;“+g+”;“+b+”m”
    -通过r、g、b值设置背景

    “\x1b[38;2;“+r+”;“+g+”;“+b+”m”
    -通过r、g、b值设置前景

  • 重要提示:内部窗口在表中只有256(或88)种颜色,窗口将使用最接近表中(r、g、b)值的颜色

    示例代码:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp1
    {
        class Program
        {
            [DllImport( "kernel32.dll", SetLastError = true )]
            public static extern bool SetConsoleMode( IntPtr hConsoleHandle, int mode );
            [DllImport( "kernel32.dll", SetLastError = true )]
            public static extern bool GetConsoleMode( IntPtr handle, out int mode );
    
            [DllImport( "kernel32.dll", SetLastError = true )]
            public static extern IntPtr GetStdHandle( int handle );
    
            static void Main( string[] args )
            {
                var handle = GetStdHandle( -11 );
                int mode;
                GetConsoleMode( handle, out mode );
                SetConsoleMode( handle, mode | 0x4 );
    
                for (int i=0;i<255;i++ )
                {
                    Console.Write( "\x1b[48;5;" + i + "m*" );
                }
    
                Console.ReadLine();
            }
        }
    }
    
    使用系统;
    使用System.Collections.Generic;
    使用System.IO;
    使用System.Linq;
    使用System.Runtime.InteropServices;
    使用系统文本;
    使用System.Threading.Tasks;
    名称空间控制台EAPP1
    {
    班级计划
    {
    [DllImport(“kernel32.dll”,SetLastError=true)]
    公共静态外部bool SetConsoleMode(IntPtr hConsoleHandle,int模式);
    [DllImport(“kernel32.dll”,SetLastError=true)]
    公共静态外部bool GetConsoleMode(IntPtr句柄,out int模式);
    [DllImport(“kernel32.dll”,SetLastError=true)]
    公共静态外部IntPtr GetStdHandle(inthandle);
    静态void Main(字符串[]参数)
    {
    var handle=GetStdHandle(-11);
    int模式;
    GetConsoleMode(句柄,输出模式);
    SetConsoleMode(手柄,模式| 0x4);
    
    对于(int i=0;i扩展Alexei Shcherbakov Windows 10 ENABLE_VIRTUAL_TERMINAL_PROCESSING)的答案,这里有一个完整的颜色代码映射,因此您可以将所有颜色及其各自的数字放在一个位置:


    我知道有点晚了。但实现C#Console中通常不可用的颜色的一种方法是更改注册表中某个颜色的颜色代码。但请注意,这可能是最不可接受的方法。 只需打开regedit(Win+R,然后键入“regedit”),转到HKEY_CURRENT_USER,打开“Console”键(此时您应该导出“Console”键以稍后还原)。在那里您将看到从ColorTable00到ColoTable15的值列表。如果您将ColorTable10从0x0000ff00更改为0x0000a5ff(或从65280更改为42495)重新启动控制台后,在控制台中使用ConsoleColor.Green时,将显示橙色。您也可以通过代码更改此值

    using System;
    using Microsoft.Win32;
    
    namespace colorChanger
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Hello World");
                RegistryKey regKey = Registry.CurrentUser.CreateSubKey("Console");
                regKey.SetValue("ColorTable10", 42495, RegistryValueKind.DWord);
                Console.ReadKey();
            }
        }
    }
    

    当然,这适用于所有其他颜色表值和颜色代码,但它只针对您电脑上的用户进行更改。

    是的,我知道这些,我希望有另一种方法来实现自定义颜色。奇怪的是,它们有深色等颜色,但没有橙色(我们学校的颜色是黑色和橙色)无论如何感谢您的帮助!对于控制台forecolor,.Net framework不支持自定义RGB颜色这一点非常奇怪,因为它似乎是本机支持的。非常好的解决方案benjamin!我曾尝试编译此代码,但遇到了一个错误:“其他信息:在DLL‘kernel32.DLL’中找不到名为‘GetConsoleScreenBufferInfoEx’的入口点。”“这一行似乎是问题所在:bool brc=GetConsoleScreenBufferInfoEx(hConsoleOutput,ref csbe);”在Vista上,稍后请参阅SetConsoleScreenBufferInfoEx API函数。"我正在使用Windows XP…XP有什么解决办法吗?编辑我下面的答案以包含此链接。蓝色和红色显示为紫色。只需使用红色和黄色显示为橙色。我以前的答案有缺陷,因为它不允许您覆盖16种预定义颜色,我在pinvoke.net上找到了真实答案,并在下面更新了我的答案我以前的答案是有缺陷的,因为它不允许你超越
    CONSOLE_SCREEN_BUFFER_INFOEX info;
    info.cbSize = sizeof(info);
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleScreenBufferInfoEx(hConsole, &info);
    info.ColorTable[14] = RGB(255, 128, 0);  // Replace yellow
    SetConsoleScreenBufferInfoEx(hConsole, &info);
    SetConsoleTextAttribute(hConsole, FOREGROUNDINTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);
    
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp1
    {
        class Program
        {
            [DllImport( "kernel32.dll", SetLastError = true )]
            public static extern bool SetConsoleMode( IntPtr hConsoleHandle, int mode );
            [DllImport( "kernel32.dll", SetLastError = true )]
            public static extern bool GetConsoleMode( IntPtr handle, out int mode );
    
            [DllImport( "kernel32.dll", SetLastError = true )]
            public static extern IntPtr GetStdHandle( int handle );
    
            static void Main( string[] args )
            {
                var handle = GetStdHandle( -11 );
                int mode;
                GetConsoleMode( handle, out mode );
                SetConsoleMode( handle, mode | 0x4 );
    
                for (int i=0;i<255;i++ )
                {
                    Console.Write( "\x1b[48;5;" + i + "m*" );
                }
    
                Console.ReadLine();
            }
        }
    }
    
    using System;
    using Microsoft.Win32;
    
    namespace colorChanger
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Hello World");
                RegistryKey regKey = Registry.CurrentUser.CreateSubKey("Console");
                regKey.SetValue("ColorTable10", 42495, RegistryValueKind.DWord);
                Console.ReadKey();
            }
        }
    }