web-dev-qa-db-ja.com

C#コンソールアプリケーションのカスタムテキストの色?

プロジェクトのC#コンソールアプリケーションコードを完成させたので、フォントに色を追加したいと思います。オレンジのカスタムカラーを使用できるようになりたいです。これを行う方法はありますか?

これは私が過去に色を変更するために使用していたコードですが、オレンジを提供していません:

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

色などに16進値を挿入する方法はありますか?

31
Thomas

http://msdn.Microsoft.com/en-us/library/system.console.backgroundcolor.aspx にあるリスト

コンソールでサポートされている唯一の色だと思います。ヘックスは許可されていません。

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

[〜#〜] edit [〜#〜]

作業中のプロジェクトファイルを公開リポジトリから取得します

https://bitbucket.org/benskolnick/color-console/

しかし、さらに詳しく調べると、赤と黄色を組み合わせてオレンジになるために多くの作業を行うことができます。こちらの例をご覧ください。コードの壁を再投稿するつもりはありません。 http://support.Microsoft.com/kb/31988 それはあなたに多くの色へのアクセスを与えませんが、正しい方向に導きます。 PINVOKEを使用する必要がありますが、オレンジや他のRGBカラーをコンソールに簡単に取り込むことができました。 http://pinvoke.net/default.aspx/kernel32.SetConsoleScreenBufferInfoEx

// Copyright Alex Shvedov
// Modified by MercuryP with color specifications
// Use this code in any way you want

using System;
using System.Diagnostics;                // for Debug
using System.Drawing;                    // for Color (add reference to  System.Drawing.Assembly)
using System.Runtime.InteropServices;    // for StructLayout

class SetScreenColorsApp
{
    [StructLayout(LayoutKind.Sequential)]
    internal struct COORD
    {
        internal short X;
        internal short Y;
    }

    [StructLayout(LayoutKind.Sequential)]
    internal struct SMALL_RECT
    {
        internal short Left;
        internal short Top;
        internal short Right;
        internal short Bottom;
    }

    [StructLayout(LayoutKind.Sequential)]
    internal struct COLORREF
    {
        internal uint ColorDWORD;

        internal COLORREF(Color color)
        {
            ColorDWORD = (uint) color.R + (((uint) color.G) << 8) + (((uint) color.B) << 16);
        }

        internal COLORREF(uint r, uint g, uint b)
        {
            ColorDWORD = r + (g << 8) + (b << 16);
        }

        internal Color GetColor()
        {
            return Color.FromArgb((int) (0x000000FFU & ColorDWORD),
                                  (int) (0x0000FF00U & ColorDWORD) >> 8, (int) (0x00FF0000U & ColorDWORD) >> 16);
        }

        internal void SetColor(Color color)
        {
            ColorDWORD = (uint) color.R + (((uint) color.G) << 8) + (((uint) color.B) << 16);
        }
    }

    [StructLayout(LayoutKind.Sequential)]
    internal struct CONSOLE_SCREEN_BUFFER_INFO_EX
    {
        internal int cbSize;
        internal COORD dwSize;
        internal COORD dwCursorPosition;
        internal ushort wAttributes;
        internal SMALL_RECT srWindow;
        internal COORD dwMaximumWindowSize;
        internal ushort wPopupAttributes;
        internal bool bFullscreenSupported;
        internal COLORREF black;
        internal COLORREF darkBlue;
        internal COLORREF darkGreen;
        internal COLORREF darkCyan;
        internal COLORREF darkRed;
        internal COLORREF darkMagenta;
        internal COLORREF darkYellow;
        internal COLORREF gray;
        internal COLORREF darkGray;
        internal COLORREF blue;
        internal COLORREF green;
        internal COLORREF cyan;
        internal COLORREF red;
        internal COLORREF Magenta;
        internal COLORREF yellow;
        internal COLORREF white;
    }

    const int STD_OUTPUT_HANDLE = -11;                                        // per WinBase.h
    internal static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);    // per WinBase.h

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

    [DllImport("kernel32.dll", SetLastError = true)]
    private static extern bool GetConsoleScreenBufferInfoEx(IntPtr hConsoleOutput, ref CONSOLE_SCREEN_BUFFER_INFO_EX csbe);

    [DllImport("kernel32.dll", SetLastError = true)]
    private static extern bool SetConsoleScreenBufferInfoEx(IntPtr hConsoleOutput, ref CONSOLE_SCREEN_BUFFER_INFO_EX csbe);

    // Set a specific console color to an RGB color
    // The default console colors used are gray (foreground) and black (background)
    public static int SetColor(ConsoleColor consoleColor, Color targetColor)
    {
        return SetColor(consoleColor, targetColor.R, targetColor.G, targetColor.B);
    }

    public static int SetColor(ConsoleColor color, uint r, uint g, uint b)
    {
        CONSOLE_SCREEN_BUFFER_INFO_EX csbe = new CONSOLE_SCREEN_BUFFER_INFO_EX();
        csbe.cbSize = (int)Marshal.SizeOf(csbe);                    // 96 = 0x60
        IntPtr hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);    // 7
        if (hConsoleOutput == INVALID_HANDLE_VALUE)
        {
            return Marshal.GetLastWin32Error();
        }
        bool brc = GetConsoleScreenBufferInfoEx(hConsoleOutput, ref csbe);
        if (!brc)
        {
            return Marshal.GetLastWin32Error();
        }

        switch (color)
        {
            case ConsoleColor.Black:
                csbe.black = new COLORREF(r, g, b);
                break;
            case ConsoleColor.DarkBlue:
                csbe.darkBlue = new COLORREF(r, g, b);
                break;
            case ConsoleColor.DarkGreen:
                csbe.darkGreen = new COLORREF(r, g, b);
                break;
            case ConsoleColor.DarkCyan:
                csbe.darkCyan = new COLORREF(r, g, b);
                break;
            case ConsoleColor.DarkRed:
                csbe.darkRed = new COLORREF(r, g, b);
                break;
            case ConsoleColor.DarkMagenta:
                csbe.darkMagenta = new COLORREF(r, g, b);
                break;
            case ConsoleColor.DarkYellow:
                csbe.darkYellow = new COLORREF(r, g, b);
                break;
            case ConsoleColor.Gray:
                csbe.gray = new COLORREF(r, g, b);
                break;
            case ConsoleColor.DarkGray:
                csbe.darkGray = new COLORREF(r, g, b);
                break;
            case ConsoleColor.Blue:
                csbe.blue = new COLORREF(r, g, b);
                break;
            case ConsoleColor.Green:
                csbe.green = new COLORREF(r, g, b);
                break;
            case ConsoleColor.Cyan:
                csbe.cyan = new COLORREF(r, g, b);
                break;
            case ConsoleColor.Red:
                csbe.red = new COLORREF(r, g, b);
                break;
            case ConsoleColor.Magenta:
                csbe.Magenta = new COLORREF(r, g, b);
                break;
            case ConsoleColor.Yellow:
                csbe.yellow = new COLORREF(r, g, b);
                break;
            case ConsoleColor.White:
                csbe.white = new COLORREF(r, g, b);
                break;
        }
        ++csbe.srWindow.Bottom;
        ++csbe.srWindow.Right;
        brc = SetConsoleScreenBufferInfoEx(hConsoleOutput, ref csbe);
        if (!brc)
        {
            return Marshal.GetLastWin32Error();
        }
        return 0;
    }

    public static int SetScreenColors(Color foregroundColor, Color backgroundColor)
    {
        int irc;
        irc = SetColor(ConsoleColor.Gray, foregroundColor);
        if (irc != 0) return irc;
        irc = SetColor(ConsoleColor.Black, backgroundColor);
        if (irc != 0) return irc;

        return 0;
    }
}

そして、オレンジまたは他の色を使用する場合は、SetScreenColorを簡単に呼び出すことができます。

static void Main(string[] args)
    {

        Color screenTextColor = Color.Orange;
        Color screenBackgroundColor = Color.Black;
        int irc = SetScreenColorsApp.SetScreenColors(screenTextColor, screenBackgroundColor);
        Debug.Assert(irc == 0, "SetScreenColors failed, Win32Error code = " + irc + " = 0x" + irc.ToString("x"));

        Debug.WriteLine("LargestWindowHeight=" + Console.LargestWindowHeight + " LargestWindowWidth=" + Console.LargestWindowWidth);
        Debug.WriteLine("BufferHeight=" + Console.BufferHeight + " WindowHeight=" + Console.WindowHeight + " BufferWidth=" + Console.BufferWidth + " WindowWidth=" + Console.WindowWidth);
        //// these are relative to the buffer, not the screen:
        //Debug.WriteLine("WindowTop=" + Console.WindowTop + " WindowLeft=" + Console.WindowLeft);
        Debug.WriteLine("ForegroundColor=" + Console.ForegroundColor + " BackgroundColor=" + Console.BackgroundColor);
        Console.WriteLine("Some text in a console window");
        Console.BackgroundColor = ConsoleColor.Cyan;
        Console.ForegroundColor = ConsoleColor.Yellow;
        Debug.WriteLine("ForegroundColor=" + Console.ForegroundColor + " BackgroundColor=" + Console.BackgroundColor);
        Console.Write("Press ENTER to exit...");
        Console.ReadLine();

        // Note: If you use SetScreenColors, the RGB values of gray and black are changed permanently for the console window.
        // Using i.e. Console.ForegroundColor = ConsoleColor.Gray afterwards will switch the color to whatever you changed gray to

        // It's best to use SetColor for the purpose of choosing the 16 colors you want the console to be able to display, then use
        // Console.BackgroundColor and Console.ForegrondColor to choose among them.
    }
27
benjamin

[恥知らずな自己宣伝]

オレンジを含む任意のSystem.Drawing.Colorでコンソール出力のスタイルを設定できるプロジェクトに取り組んでいます。方法は次のとおりです。

Console.WriteLine("writing to the console in orange", Color.Orange);

プロジェクト: http://colorfulconsole.com/

12
Brian Snow

Windows 10 Anniversary Update以降、コンソールはANSI/VT100カラーコードを使用できます

  1. SetConsoleModeでフラグENABLE_VIRTUAL_TERMINAL_PROCESSING(0x4)を設定する必要があります
  2. シーケンスを使用する:

    「\ 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の値で前景を設定

不等の注意:内部的には、Windowsのテーブルには256(または88)色しかなく、Windowsはテーブルの(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();
        }
    }
}

結果:

Windows 10 Colors

MSDNでそれについて読む:記事「コンソール仮想端末シーケンス」

9

Alexei Shcherbakov Windows 10 ENABLE_VIRTUAL_TERMINAL_PROCESSINGの答えを拡張すると、完全なカラーコードマッピングが得られるため、すべての色とそれぞれの番号を1か所にまとめることができます。

enter image description here

3
Jenny

遅くなることはないが、少なくともVista以降では可能になったようだ。したがって、同じ質問を持つ他の人のために将来参照するためにこれを追加します。

これを実行しようとしていたときに、Hans Passantの MSDNに返信

現在、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-Invokingが少し必要になりますが、先に進むために何かを与える必要があります。

3
Ash

Colorful.Console を使用して、カスタムカラーを使用したり、ASCII art!

Colorful.Console Example

これが機能しないというさらなる証拠(ベンジャミンのリンクのアプローチを使用):

using System.Runtime.InteropServices;

namespace
{
    class Program
    {
        [DllImport("kernel32.dll")]
        public static extern bool SetConsoleTextAttribute(IntPtr hConsoleOutput, int wAttributes);
        [DllImport("kernel32.dll")]
        public static extern IntPtr GetStdHandle(uint nStdHandle);

        static void Main(string[] args)
        {
            uint STD_OUTPUT_HANDLE = 0xfffffff5;
            IntPtr hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

            SetConsoleTextAttribute(hConsole, (int)Colour.Red + (int)Colour.Green + (int)Colour.Intensity);
            Console.WriteLine("Red + Green + Intensity == Yellow");

            SetConsoleTextAttribute(hConsole, (int)Colour.Red + (int)Colour.Green + (int)Colour.Intensity + (int)Colour.Red);
            Console.WriteLine("Yellow + Red != Orange");

            SetConsoleTextAttribute(hConsole, 15);
            Console.WriteLine();
            Console.WriteLine("Press Enter to exit ...");
            Console.Read();
        }

        public enum Colour
        {
            Blue = 0x00000001,
            Green = 0x00000002,
            Red = 0x00000004,
            Intensity = 0x00000008
        }
    }
}

このメソッドでは、ConsoleColorを介して到達できないものを追加することはできません。私もアプリにオレンジを追加したいので、それは本当に残念です。誰かが方法を見つけたら、私は非常に興味があるでしょう。

1
Simon

オレンジはありません。その色は、コンソールでサポートされている色の1つではないためです。つまり、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");
            }
        }
    }
1
lontivero

少し遅れて、私は知っています。しかし、通常C#コンソールでは利用できない色を実現する1つの方法は、レジストリ内の色のColorCodeを変更することです。しかし、注意してください、これはおそらく最も受け入れがたい方法です。 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();
        }
    }
}

もちろん、それは他のすべてのColorTable-ValueとColorcodeで機能しますが、PC上のユーザーに対してのみ変更されます。

0
fly_over_32