在C#控制台应用程序中更改背景颜色

15

我在网上搜索过,但是似乎找不到解决方法。 我希望我的整个控制台应用窗口都是特定的颜色,例如蓝色。 我该怎么做?


1
你是如何搜索的?Console.BackgroundColor 属性 - Hamlet Hakobyan
1
你是在寻找一种方法来将整个黑色背景变成特定的颜色,还是在寻找Soner(带有截图的)提供的答案? - bas
在您的 try 块中,您可能忘记在设置 Console.BackgroundColor 后添加 Console.Clear() 行。 - Janaka R Rajapaksha
4个回答

47

只需设置背景颜色并调用 Console.Clear()

class Program {
    static void Main(string[] args) {
        Console.BackgroundColor = ConsoleColor.Blue;
        Console.Clear();
        Console.ForegroundColor = ConsoleColor.White;
        Console.Write("Press any key to continue");
        Console.ReadKey();
    }
}

在此输入图片描述


7

这个问题是要求如何将整个背景颜色设置为蓝色。其他示例都没有正确显示。以下是正确的方法:

namespace ClearConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.BackgroundColor = ConsoleColor.Blue;
            Console.Clear();

        }
    }
}

7
您可以设置 Console.BackgroundColor 属性为 ConsoleColor 枚举。
获取或设置控制台的背景颜色。若要更改整个控制台窗口的背景颜色,请设置 BackgroundColor 属性并调用 Clear 方法。
Console.BackgroundColor = ConsoleColor.Blue;
Console.Clear();

enter image description here

如果您想更改控制台的前景色,可以使用Console.ForegroundColor属性。

获取或设置控制台的前景色。

Console.ForegroundColor = ConsoleColor.Blue;

enter image description here


3
@SonerGönül的第一张截图前有一个console.clear();命令,因此整个背景应该是蓝色的。 - Janaka R Rajapaksha

1
Console.ForegroundColor = Color.Blue;

Console.WriteLine("这个字符串是蓝色的!");


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接