在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:59:56  来源:igfitidea点击:

Change Background color on C# console application

c#colorsbackgroundconsole

提问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.BackgroundColorproperty to ConsoleColorenumeration..

您可以将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 Clearmethod.

获取或设置控制台的背景颜色。要整体更改 > 控制台窗口的背景颜色,请设置 BackgroundColor 属性并调用该Clear方法。

Console.BackgroundColor = ConsoleColor.Blue;
Console.Clear();

enter image description here

在此处输入图片说明

And you can use Console.ForegroundColorproperty for

你可以使用Console.ForegroundColor财产

Gets or sets the foreground color of the console.

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

Console.ForegroundColor = ConsoleColor.Blue;

enter image description here

在此处输入图片说明

回答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();
    }
}

enter image description here

在此处输入图片说明

回答by user6436606

Console.ForegroundColor = Color.Blue;

Console.WriteLine("This string is blue!");

Console.WriteLine("This string is blue!");