欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#控制台应用程序中输出彩色字体

程序员文章站 2023-12-06 12:10:58
本文实例为大家分享了c#控制台输出彩色字体的具体代码,供大家参考,具体内容如下 using system; class example { publi...

本文实例为大家分享了c#控制台输出彩色字体的具体代码,供大家参考,具体内容如下

using system;

class example
{
 public static void main() 
 {
  // get a string array with the names of consolecolor enumeration members.
  string[] colornames = consolecolor.getnames(typeof(consolecolor));

  // display each foreground color except black on a constant black background.
  console.writeline("all the foreground colors (except black) on a constant black background:");

  foreach (string colorname in colornames)
  {
   // convert the string representing the enum name to the enum value.
   consolecolor color = (consolecolor) enum.parse(typeof(consolecolor), colorname);

   if (color == consolecolor.black) continue;

   console.write("{0,11}: ", colorname);
   console.backgroundcolor = consolecolor.black;
   console.foregroundcolor = color;
   console.writeline("this is foreground color {0}.", colorname);
   // restore the original foreground and background colors.
   console.resetcolor();
  }
  console.writeline();

  // display each background color except white with a constant white foreground.
  console.writeline("all the background colors (except white) with a constant white foreground:");

  foreach (string colorname in colornames)
  {
   // convert the string representing the enum name to the enum value.
   consolecolor color = (consolecolor) enum.parse(typeof(consolecolor), colorname);

   if (color == consolecolor.white) continue;

   console.write("{0,11}: ", colorname);
   console.foregroundcolor = consolecolor.white;
   console.backgroundcolor = (consolecolor) enum.parse(typeof(consolecolor), colorname);
   console.writeline("this is background color {0}.", colorname);
   console.resetcolor();
  }
 }
}

效果图:

C#控制台应用程序中输出彩色字体

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。