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

时间:2022-07-01 11:18:59

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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#控制台应用程序中输出彩色字体

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