Console.BackgroundColor = ConsoleColor.Blue; //设置背景色 Console.ForegroundColor = ConsoleColor.White; //设置前景色,即字体颜色 Console.WriteLine("第一行白蓝."); Console.ResetColor(); //将控制台的前景色和背景色设为默认值 Console.WriteLine("第二行前景色和背景色设为默认值."); Console.BackgroundColor = ConsoleColor.Red; Console.ForegroundColor = ConsoleColor.Blue; string value = "第三行 红、蓝"; Console.WriteLine(value.PadRight(Console.WindowWidth - value.Length)); //设置一整行的背景色 //显示出Console中支持的背景色及前景色 Type type = typeof(ConsoleColor); Console.ForegroundColor = ConsoleColor.White; foreach (string name in Enum.GetNames(type)) { Console.BackgroundColor = (ConsoleColor)Enum.Parse(type, name); Console.WriteLine(name); } Console.BackgroundColor = ConsoleColor.Black; foreach (string name in Enum.GetNames(type)) { Console.ForegroundColor = (ConsoleColor)Enum.Parse(type, name); Console.WriteLine(name); } Console.ReadKey();
运行结果:
文章评论