问题描述
在 C# 中,控制台具有可用于更改控制台背景颜色和控制台前景色(文本)颜色的属性.
In C#, the console has properties that can be used to change the background color of the console, and the foreground (text) color of the console.
Console.BackgroundColor // the background color
Console.ForegroundColor // the foreground/text color
问题是背景颜色仅适用于写入文本的地方,而不适用于可用空间.
The issue is that background color applies only where text is written, not to free space.
Console.BackgroundColor = ConsoleColor.White; // background color is white
Console.ForegroundColor = ConsoleColor.Blue; // text color is blue
现在,使用上面的代码,它确实将文本变成蓝色,但它只会将文本的背景变成白色,而不是整个控制台窗口的背景.
Now, with the above code, it does indeed turn the text blue, but it only turns the background of the text white, instead of the entire console window's background.
这是我的意思的一个例子:
Here's an example of what I mean:
如您所见,白色背景仅显示在文本后面,并不会改变整个控制台窗口的颜色.
As you can see, the white background only displays behind the text, and does not change the color of the entire console window.
如何更改整个控制台窗口的颜色?
How do I change the color of the entire console window?
推荐答案
您需要在设置颜色之后但在编写文本之前清除控制台窗口...
You need to clear the console window AFTER setting the colors but BEFORE you write the text...
Console.ForegroundColor = ConsoleColor.Red;
Console.BackgroundColor = ConsoleColor.Green;
Console.Clear();
Console.WriteLine("Hello World");
Console.ReadLine();
这篇关于如何在 C# 中更改控制台窗口的完整背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!