【问题标题】:C# Console string builder with foregroundColor带有foregroundColor的C#控制台字符串生成器
【发布时间】:2010-10-21 12:48:07
【问题描述】:

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

const string WelcomeMessage =     @" _______ _   _______      _______" + NewLine +
                                  @"|__   __(_) |__   __|    |__   __|   " + NewLine +
                                  @"   | |   _  ___| | __ _  ___| | ___   __" + NewLine +
                                  @"   | |  | |/ __| |/ _` |/ __| |/ _ \ / _ \    " + NewLine +
                                  @"   | |  | | (__| | (_| | (__| | (_) |  __/    " + NewLine +
                                  @"   |_|  |_|\___|_|\__,_|\___|_|\___/ \___|    "

我知道我可以使用

Console.ForegroundColor = ConsoleColor.Blue; 
Console.Write(" _______"); 

然后写下这封信的每一部分,但这会使我的代码几乎无法阅读和维护。 所以我只想知道是否存在某种用于控制台输出的 StringBuilder 设计,其中可能包含前景颜色信息。

【问题讨论】:

  • 我没有你的问题的答案,但我有一个提示来清理你的字符串。当您将 @ 添加到字符串文字的开头时,您可以在换行符上继续字符串。所以字符串 hw = @"Hello World";里面会有新线。呸!注释不允许换行格式。我试图在 Hello 和 World 之间建立一个

标签: c# .net console-application


【解决方案1】:

我怀疑是否有任何众所周知的 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();      
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-22
    • 2010-10-06
    • 1970-01-01
    相关资源
    最近更新 更多