【问题标题】:Multiple colors in one line in console application控制台应用程序中一行中的多种颜色
【发布时间】:2018-02-26 21:40:17
【问题描述】:

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

但这是我现在的结果:

这是我的代码:

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();
}

如何在控制台应用程序中每行获得多种颜色?

【问题讨论】:

  • 是的,这是可能的,但我不认为你可以在这样的循环中做到这一点。您可以使用Console.ForegroundColorConsole.Write(可能还有Console.SetCursorPosition)的组合来用不同的颜色书写您想要的字符。

标签: c# colors


【解决方案1】:

一种方式是这样的:

            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;
                    }
                }
            }

我使用以 3 开头的颜色来避免黑色;

【讨论】:

  • 哇!漂亮!!
【解决方案2】:

你需要:

  1. 定义您要使用的颜色
  2. 向控制台输出一些东西

代码:

using System;
using System.Linq;

namespace ConsoleApp1
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var colors = Enum.GetValues(typeof(ConsoleColor)).Cast<ConsoleColor>().ToArray();

            foreach (var color in colors)
            {
                Console.ForegroundColor = color;

                Console.Write("ABC");
            }
        }
    }
}

或者

您可以将您的控制台着色提升到一个新的水平,我已经发布了一个免费的开源包装器,围绕 Windows 10 中的新控制台功能,即 24 位颜色 :)

源和 NuGet 包:

https://github.com/aybe/TrueColorConsole

https://www.nuget.org/packages/TrueColorConsole/

【讨论】:

    【解决方案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]);
        }
    }
    

    然后你可以用你想写的不同形状,连同起始位置和颜色来调用这个方法,例如:

    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();
    }
    

    输出

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 2013-01-26
      • 2020-01-18
      • 2014-09-06
      相关资源
      最近更新 更多