gpt4 book ai didi

c# - 如何在 C# 中更改控制台窗口的完整背景颜色?

转载 作者:可可西里 更新时间:2023-11-01 03:06:04 27 4
gpt4 key购买 nike

在 C# 中,控制台具有可用于更改控制台背景颜色和控制台前景(文本)颜色的属性。

Console.BackgroundColor // the background color
Console.ForegroundColor // the foreground/text color

问题是背景颜色仅适用于书写文本的位置,不适用于空闲空间。

Console.BackgroundColor = ConsoleColor.White; // background color is white
Console.ForegroundColor = ConsoleColor.Blue; // text color is blue

现在,使用上面的代码,它确实将文本变为蓝色,但它只是将文本的背景变为白色,而不是将整个控制台窗口的背景变为白色。

这是我的意思的一个例子: The background only covers the background of the text, not of the entire console window

如您所见,白色背景仅显示在文本后面,并没有改变整个控制台窗口的颜色。

如何更改整个控制台窗口的颜色?

最佳答案

您需要在设置颜色之后但在编写文本之前清除控制台窗口...

Console.ForegroundColor = ConsoleColor.Red;
Console.BackgroundColor = ConsoleColor.Green;

Console.Clear();

Console.WriteLine("Hello World");

Console.ReadLine();

关于c# - 如何在 C# 中更改控制台窗口的完整背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7524057/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com