简体   繁体   中英

How do I change the full background color of the console window in 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();

Pardon the shameless self-promotion, but I've created a small plugin (available on NuGet ) that allows you to add any (if supported by your terminal) color to your console output, without the limitations of the classic solutions.

It works by extending the String object, and the syntax is very simple:

"colorize me".Pastel("#1E90FF");

在此处输入图片说明

internal class Program
    {
        static void Main(string[] args)
        {
            Console.BackgroundColor = ConsoleColor.Red;
            Console.Clear();
            Array marks = Enum.GetValues(typeof(Mark));
            foreach (var mark in marks)
            {
                Console.WriteLine(mark);
                Console.BackgroundColor = ConsoleColor.Yellow;
            }

        }

    }

The running console controls the colors. You're essentially only changing the output of your application's color properties.

It's simple for changing the overall background color: Click on the 'C:\\' icon Select Properties and choose the Colors tab.

Now if you're wanting to do this programmatically, you'll want to launch you're own window:

CMD /T:F[n color index]

Color Value

Black 0 Blue 1 Green 2 Aqua 3 Red 4 Purple 5 Greenish Yellow 6 Light Gray 7 Gray 8 Light Blue 9 Light Green A Light Aqua B Light Red C Light Purple D Light Yellow E Bright White F

Or if you're using PowerShell, refer to this TechNet article: http://technet.microsoft.com/en-us/library/ee156814.aspx

Console.ForegroundColor = ConsoleColor.White;

This will work for you put it after your first open brace

{
        system("cls");
        system("color f3");
}

You can change the colors by number up to 7 I think example f1,f2,f3,f4... .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM