Keep on going never give up.

Let's Go

C#【练习】更改控制台前景色和背景色

C#Lonely2019-05-20 01:54:15128次0条

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();

运行结果:

image.png

暗锚,解决锚点偏移

文章评论

    嘿,来试试登录吧!