在 C# 控制台应用程序上更改背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14792066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change Background color on C# console application
提问by user2057693
Ive searched the Web, but i cant seem to find the solution. I want my whole console application window to be a specific color, for example blue. How do I do that?
我在网上搜索过,但我似乎无法找到解决方案。我希望我的整个控制台应用程序窗口都是特定的颜色,例如蓝色。我怎么做?
回答by John
The OP question was asking for how to set the entire background color to blue. None of the other samples shows this correctly. Here's how:
OP 问题是询问如何将整个背景颜色设置为蓝色。其他样本均未正确显示这一点。就是这样:
namespace ClearConsole
{
class Program
{
static void Main(string[] args)
{
Console.BackgroundColor = ConsoleColor.Blue;
Console.Clear();
}
}
}
回答by Soner G?nül
You can set Console.BackgroundColor
property to ConsoleColor
enumeration..
您可以将Console.BackgroundColor
属性设置为ConsoleColor
枚举..
Gets or sets the background color of the console. To change the background color of the > console window as a whole, set the BackgroundColor property and call the
Clear
method.
获取或设置控制台的背景颜色。要整体更改 > 控制台窗口的背景颜色,请设置 BackgroundColor 属性并调用该
Clear
方法。
Console.BackgroundColor = ConsoleColor.Blue;
Console.Clear();
And you can use Console.ForegroundColor
property for
你可以使用Console.ForegroundColor
财产
Gets or sets the foreground color of the console.
获取或设置控制台的前景色。
Console.ForegroundColor = ConsoleColor.Blue;
回答by Hans Passant
Simply set the background color and call Console.Clear()
:
只需设置背景颜色并调用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();
}
}
回答by user6436606
Console.ForegroundColor = Color.Blue;
Console.WriteLine("This string is blue!");
Console.WriteLine("This string is blue!");