C# 控制台应用程序中一行中的多种颜色

C# 控制台应用程序中一行中的多种颜色,c#,colors,C#,Colors,我正在尝试在控制台应用程序中获得颜色的结果: 但这是我现在的结果: 这是我的密码: static void Main(string[] args) { string[] str = new string[] { " ________ __ ", "/ | / | ", "$$$$$$$$/______ _____

我正在尝试在控制台应用程序中获得颜色的结果:

但这是我现在的结果:

这是我的密码:

static void Main(string[] args)
{
    string[] str = new string[] 
    {
        " ________                     __      ",
        "/        |                   /  |     ",
        "$$$$$$$$/______    _______  _$$ |_    ",
        "   $$ | /      \\  /       |/ $$   |  ",
        "   $$ |/$$$$$$  |/$$$$$$$/ $$$$$$/    ",
        "   $$ |$$    $$ |$$      \\   $$ | __ ",
        "   $$ |$$$$$$$$/  $$$$$$  |  $$ |/  | ",
        "   $$ |$$       |/     $$/   $$  $$/  ",
        "   $$/  $$$$$$$/ $$$$$$$/     $$$$/   "
    };

    Console.ForegroundColor = ConsoleColor.Green;

    for (int i = 0; i < str.Length; i++)
    {
        for (int j = 0; j < str[i].Length; j++)
        {
            if (i >= 4 && i < 7 && j > 3 && j < 5)
            {
                Console.ForegroundColor = ConsoleColor.Blue;   
            }   

            Console.Write(str[i][j]);
        }

        Console.WriteLine();
    }

    Console.ResetColor();
}
static void Main(字符串[]args)
{
字符串[]str=新字符串[]
{
" ________                     __      ",
"/        |                   /  |     ",
"$$$$$$$$/______    _______  _$$ |_    ",
"   $$ | /      \\  /       |/ $$   |  ",
"   $$ |/$$$$$$  |/$$$$$$$/ $$$$$$/    ",
"   $$ |$$    $$ |$$      \\   $$ | __ ",
"   $$ |$$$$$$$$/  $$$$$$  |  $$ |/  | ",
"   $$ |$$       |/     $$/   $$  $$/  ",
"   $$/  $$$$$$$/ $$$$$$$/     $$$$/   "
};
Console.ForegroundColor=ConsoleColor.Green;
对于(int i=0;i=4&&i<7&&j>3&&j<5)
{
Console.ForegroundColor=ConsoleColor.Blue;
}   
Console.Write(str[i][j]);
}
Console.WriteLine();
}
Console.ResetColor();
}
如何在控制台应用程序上获得每行的多种颜色?

您需要:

  • 定义要使用的颜色
  • 输出一些东西到控制台
  • 代码:

    使用系统;
    使用System.Linq;
    名称空间控制台EAPP1
    {
    内部课程计划
    {
    私有静态void Main(字符串[]args)
    {
    var colors=Enum.GetValues(typeof(ConsoleColor)).Cast().ToArray();
    foreach(颜色中的颜色变量)
    {
    Console.ForegroundColor=颜色;
    控制台。写入(“ABC”);
    }
    }
    }
    }
    

    你可以把你的控制台颜色提升到一个新的层次,我已经发布了一个免费的开源包装,它围绕着Windows10中新的控制台功能,即24位颜色:)

    源代码和NuGet包:

    一种方法是这样的:

                var index = 3;
                foreach (var item in str)
                {
                    for (int i = 0; i < item.Length; i++)
                    {
    
                        Console.Write(item[i]);
    
                        Console.ForegroundColor = (ConsoleColor)index;
                        index++;
                        if (index == 15)
                            index = 3;
                        if (i == item.Length - 1)
                        {
                            Console.Write("\n");
                            continue;
                        }
                    }
                }
    
    var指数=3;
    foreach(str中的var项)
    {
    for(int i=0;i
    我用了从3开始的颜色来避免黑色;

    您可以做的另一件事是编写一个助手方法,该方法将从特定位置开始,以特定颜色向控制台写入字符串数组:

    private static void WriteColoredLines(string[] lines, Point start, ConsoleColor color)
    {
        Console.ForegroundColor = color;
    
        for (int row = 0; row < lines.Length; row++)
        {
            Console.SetCursorPosition(start.X, start.Y + row);
            Console.Write(lines[row]);
        }
    }
    
    输出


    是的,这是可能的,但我认为你不可能像那样循环完成。您可以使用
    Console.ForegroundColor
    Console.Write
    (可能还有
    Console.SetCursorPosition
    )的组合来编写不同颜色的字符。哇!美丽的!!
    private static void WriteColoredLines(string[] lines, Point start, ConsoleColor color)
    {
        Console.ForegroundColor = color;
    
        for (int row = 0; row < lines.Length; row++)
        {
            Console.SetCursorPosition(start.X, start.Y + row);
            Console.Write(lines[row]);
        }
    }
    
    private static void Main()
    {
        string[] topOfT =
        {
            " ________ ",
            "/        |",
            "$$$$$$$$/",
            "   $$ |",
            "   $$ |",
        };
    
        string[] bottomOfT =
        {
            "   $$ |",
            "   $$ |",
            "   $$ |",
            "   $$/",
        };
    
        string[] letterE =
        {
            "  ______",
            " /      \\",
            "/$$$$$$  |",
            "$$    $$ |",
            "$$$$$$$$/",
            "$$       |",
            " $$$$$$$/",
        };
    
        string[] lettersSandT =
        {
            "              __",
            "             /  |",
            "  _______   _$$ |_    ",
            " /       | / $$   |  ",
            "/$$$$$$$/  $$$$$$/    ",
            "$$      \\    $$ | __ ",
            "  $$$$$$ |   $$ |/  | ",
            "/      $$/   $$  $$/  ",
            "$$$$$$$/     $$$$/   "
        };
    
        WriteColoredLines(topOfT, new Point(0, 0), ConsoleColor.Green);
        WriteColoredLines(bottomOfT, new Point(0, 5), ConsoleColor.Blue);
        WriteColoredLines(letterE, new Point(9, 2), ConsoleColor.Blue);
        WriteColoredLines(lettersSandT, new Point(20, 0), ConsoleColor.Gray);
    
        Console.Write("\n\n\nDone!\nPress any key to exit...");
        Console.ReadKey();
    }