带有foregroundColor的C#控制台字符串生成器

带有foregroundColor的C#控制台字符串生成器,c#,.net,console-application,C#,.net,Console Application,嗨,我正在尝试为我创建的一个简单控制台应用程序的字符串添加一些颜色。 我希望每个字母都有不同的颜色 const string WelcomeMessage = @" _______ _ _______ _______" + NewLine + @"|__ __(_) |__ __| |__ __| " + NewLine +

嗨,我正在尝试为我创建的一个简单控制台应用程序的字符串添加一些颜色。 我希望每个字母都有不同的颜色

const string WelcomeMessage =     @" _______ _   _______      _______" + NewLine +
                                  @"|__   __(_) |__   __|    |__   __|   " + NewLine +
                                  @"   | |   _  ___| | __ _  ___| | ___   __" + NewLine +
                                  @"   | |  | |/ __| |/ _` |/ __| |/ _ \ / _ \    " + NewLine +
                                  @"   | |  | | (__| | (_| | (__| | (_) |  __/    " + NewLine +
                                  @"   |_|  |_|\___|_|\__,_|\___|_|\___/ \___|    "
我知道我可以用

Console.ForegroundColor = ConsoleColor.Blue; 
Console.Write(" _______"); 
然后写下信的每一部分,但这会使我的代码几乎无法阅读和维护。
因此,我只想知道是否存在某种用于控制台输出的StringBuilder设计集,其中可能包含foregroundColor信息。

我怀疑是否存在任何已知的API。但是您可以创建一个标记每个字母的矩形列表。以下示例为前三个字母演示了这一点:

static void Main(string[] args)
{
  string WelcomeMessage =
                              @" _______ _   _______      _______          " + Environment.NewLine +
                              @"|__   __(_) |__   __|    |__   __|         " + Environment.NewLine +
                              @"   | |   _  ___| | __ _  ___| | ___   __   " + Environment.NewLine +
                              @"   | |  | |/ __| |/ _` |/ __| |/ _ \ / _ \ " + Environment.NewLine +
                              @"   | |  | | (__| | (_| | (__| | (_) |  __/ " + Environment.NewLine +
                              @"   |_|  |_|\___|_|\__,_|\___|_|\___/ \___| ";

  List<Rectangle> list = new List<Rectangle>();
  list.Add(new Rectangle(new Point(0, 0), new Size(7, 6)));
  list.Add(new Rectangle(new Point(8, 0), new Size(2, 6)));
  list.Add(new Rectangle(new Point(10, 2), new Size(4, 4)));

  Dictionary<Rectangle, ConsoleColor> colors = new Dictionary<Rectangle, ConsoleColor>();
  colors.Add(list[0], ConsoleColor.DarkBlue);
  colors.Add(list[1], ConsoleColor.DarkRed);
  colors.Add(list[2], ConsoleColor.DarkGreen);
  Console.WriteLine(WelcomeMessage);

  // NOTE: you might want to save the lines in an array where you define it:
  string[] lines = WelcomeMessage.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
  for (int y = 0; y < lines.Length; y++)
  {
    string line = lines[y];
    for (int x = 0; x < line.Length; x++)
    {
      Rectangle rect = list.Where(c =>
        x >= c.X && x <= c.X + c.Width && 
        y >= c.Y && y <= c.Y + c.Height).FirstOrDefault();

      if (rect == Rectangle.Empty)
        break ; // TODO Not implemented yet           
      else
      {            
        Console.ForegroundColor = colors[rect];
        Console.Write(line[x]);
      }
    }
    Console.WriteLine();
  }

  Console.ReadKey();      
}
static void Main(字符串[]args)
{
字符串消息=
@“uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu+
@“| | | | | | | | | | | |”+Environment.NewLine+
@“| | | | | | | | | | | | | | | | | | | | | | | | | | | |+
@“| | | |/124; |/124;/124;/124;/124;/124;/124;”+Environment.NewLine+
@“| | | | | | | | | | | | | | |/”+Environment.NewLine+
@"   |_|  |_|\___|_|\__,_|\___|_|\___/ \___| ";
列表=新列表();
添加(新矩形(新点(0,0),新大小(7,6));
添加(新矩形(新点(8,0),新大小(2,6));
添加(新矩形(新点(10,2),新尺寸(4,4));
字典颜色=新字典();
添加(列表[0],控制台颜色.DarkBlue);
添加(列表[1],控制台颜色.DarkRed);
添加(列表[2],控制台颜色.DarkGreen);
Console.WriteLine(WelcomeMessage);
//注意:您可能希望将这些行保存在定义它的数组中:
string[]lines=WelcomeMessage.Split(新字符串[]{Environment.NewLine},StringSplitOptions.None);
对于(int y=0;y

x>=c.x&&x=c.Y&&Y我怀疑是否有任何著名的API。但您可以创建一个标记每个字母的矩形列表。以下示例为前三个字母演示了这一点:

static void Main(string[] args)
{
  string WelcomeMessage =
                              @" _______ _   _______      _______          " + Environment.NewLine +
                              @"|__   __(_) |__   __|    |__   __|         " + Environment.NewLine +
                              @"   | |   _  ___| | __ _  ___| | ___   __   " + Environment.NewLine +
                              @"   | |  | |/ __| |/ _` |/ __| |/ _ \ / _ \ " + Environment.NewLine +
                              @"   | |  | | (__| | (_| | (__| | (_) |  __/ " + Environment.NewLine +
                              @"   |_|  |_|\___|_|\__,_|\___|_|\___/ \___| ";

  List<Rectangle> list = new List<Rectangle>();
  list.Add(new Rectangle(new Point(0, 0), new Size(7, 6)));
  list.Add(new Rectangle(new Point(8, 0), new Size(2, 6)));
  list.Add(new Rectangle(new Point(10, 2), new Size(4, 4)));

  Dictionary<Rectangle, ConsoleColor> colors = new Dictionary<Rectangle, ConsoleColor>();
  colors.Add(list[0], ConsoleColor.DarkBlue);
  colors.Add(list[1], ConsoleColor.DarkRed);
  colors.Add(list[2], ConsoleColor.DarkGreen);
  Console.WriteLine(WelcomeMessage);

  // NOTE: you might want to save the lines in an array where you define it:
  string[] lines = WelcomeMessage.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
  for (int y = 0; y < lines.Length; y++)
  {
    string line = lines[y];
    for (int x = 0; x < line.Length; x++)
    {
      Rectangle rect = list.Where(c =>
        x >= c.X && x <= c.X + c.Width && 
        y >= c.Y && y <= c.Y + c.Height).FirstOrDefault();

      if (rect == Rectangle.Empty)
        break ; // TODO Not implemented yet           
      else
      {            
        Console.ForegroundColor = colors[rect];
        Console.Write(line[x]);
      }
    }
    Console.WriteLine();
  }

  Console.ReadKey();      
}
static void Main(字符串[]args)
{
字符串消息=
@“uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu+
@“| | | | | | | | | | | |”+Environment.NewLine+
@“| | | | | | | | | | | | | | | | | | | | | | | | | | | |+
@“| | | |/124; |/124;/124;/124;/124;/124;/124;”+Environment.NewLine+
@“| | | | | | | | | | | | | | |/”+Environment.NewLine+
@"   |_|  |_|\___|_|\__,_|\___|_|\___/ \___| ";
列表=新列表();
添加(新矩形(新点(0,0),新大小(7,6));
添加(新矩形(新点(8,0),新大小(2,6));
添加(新矩形(新点(10,2),新尺寸(4,4));
字典颜色=新字典();
添加(列表[0],控制台颜色.DarkBlue);
添加(列表[1],控制台颜色.DarkRed);
添加(列表[2],控制台颜色.DarkGreen);
Console.WriteLine(WelcomeMessage);
//注意:您可能希望将这些行保存在定义它的数组中:
string[]lines=WelcomeMessage.Split(新字符串[]{Environment.NewLine},StringSplitOptions.None);
对于(int y=0;y

x>=c.x&&x=c.Y&&Y我对你的问题没有答案,但我有一个技巧来清理你的字符串。当你将@添加到字符串文字的开头时,你可以在换行符上继续字符串。因此字符串hw=@“Hello World”;将包含换行符。Gah!注释不允许换行符格式。我试图在Hello和World之间设置一个
。我对您的问题没有答案,但我有一个提示来清理您的字符串。当您将@添加到字符串文字的开头时,您可以在换行符上继续字符串。因此字符串hw=@“Hello World”将包含换行符。啊!注释不允许换行符格式。我正在尝试在Hello和World之间设置一个