Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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: The background only covers the background of the text, not of the entire console window

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?

like image 217
Alper Avatar asked Sep 23 '11 03:09

Alper


People also ask

How do I change my console background color?

To change the background color of the console window as a whole, set the BackgroundColor property and call the Clear method.

How do I change the background color of my console in C++?

Colorizing text and console background in C++ In C++ programming, the default background of the output screen is black and the text color is the white color, the task is to color both the background and text color in the output screen. console_color = GetStdHandle(STD_OUTPUT_HANDLE);

How do you change the background in code?

To add background color in HTML, use the CSS background-color property. Set it to the color name or code you want and place it inside a style attribute. Then add this style attribute to an HTML element, like a table, heading, div, or span tag.


2 Answers

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();
like image 141
Ryan Alford Avatar answered Sep 28 '22 19:09

Ryan Alford


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");

enter image description here

like image 25
silkfire Avatar answered Sep 28 '22 18:09

silkfire