Console.BackgroundColor 属性

定义

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

C#
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public static ConsoleColor BackgroundColor { get; set; }
C#
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
[System.Runtime.Versioning.UnsupportedOSPlatform("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatform("tvos")]
public static ConsoleColor BackgroundColor { get; set; }
C#
public static ConsoleColor BackgroundColor { get; set; }

属性值

ConsoleColor

一个值,指定控制台的背景色;也就是显示在每个字符后面的颜色。 默认为黑色。

属性

例外

设置操作中指定的颜色不是 ConsoleColor 的有效成员。

用户没有执行此操作的权限。

出现 I/O 错误。

示例

下面的示例将枚举的值保存 ConsoleColor 到数组,并将和属性的当前值存储 BackgroundColor ForegroundColor 到变量中。 然后,它会将前景色更改为枚举中的每个颜色 ConsoleColor (除了与当前背景匹配的颜色),并且它会将背景色更改为枚举中的每个颜色, ConsoleColor 但与当前前景匹配的颜色除外。 (如果前景色与背景色相同,则不显示文本。 ) 最后,它会调用 ResetColor 方法来还原原始控制台颜色。

C#
using System;

class Example
{
   public static void Main()
   {
      // Get an array with the values of ConsoleColor enumeration members.
      ConsoleColor[] colors = (ConsoleColor[]) ConsoleColor.GetValues(typeof(ConsoleColor));
      // Save the current background and foreground colors.
      ConsoleColor currentBackground = Console.BackgroundColor;
      ConsoleColor currentForeground = Console.ForegroundColor;

      // Display all foreground colors except the one that matches the background.
      Console.WriteLine("All the foreground colors except {0}, the background color:",
                        currentBackground);
      foreach (var color in colors) {
         if (color == currentBackground) continue;

         Console.ForegroundColor = color;
         Console.WriteLine("   The foreground color is {0}.", color);
      }
      Console.WriteLine();
      // Restore the foreground color.
      Console.ForegroundColor = currentForeground;

      // Display each background color except the one that matches the current foreground color.
      Console.WriteLine("All the background colors except {0}, the foreground color:",
                        currentForeground);
      foreach (var color in colors) {
         if (color == currentForeground) continue;

         Console.BackgroundColor = color;
         Console.WriteLine("   The background color is {0}.", color);
      }

      // Restore the original console colors.
      Console.ResetColor();
      Console.WriteLine("\nOriginal colors restored...");
   }
}
//The example displays output like the following:
//    All the foreground colors except DarkCyan, the background color:
//       The foreground color is Black.
//       The foreground color is DarkBlue.
//       The foreground color is DarkGreen.
//       The foreground color is DarkRed.
//       The foreground color is DarkMagenta.
//       The foreground color is DarkYellow.
//       The foreground color is Gray.
//       The foreground color is DarkGray.
//       The foreground color is Blue.
//       The foreground color is Green.
//       The foreground color is Cyan.
//       The foreground color is Red.
//       The foreground color is Magenta.
//       The foreground color is Yellow.
//       The foreground color is White.
//
//    All the background colors except White, the foreground color:
//       The background color is Black.
//       The background color is DarkBlue.
//       The background color is DarkGreen.
//       The background color is DarkCyan.
//       The background color is DarkRed.
//       The background color is DarkMagenta.
//       The background color is DarkYellow.
//       The background color is Gray.
//       The background color is DarkGray.
//       The background color is Blue.
//       The background color is Green.
//       The background color is Cyan.
//       The background color is Red.
//       The background color is Magenta.
//       The background color is Yellow.
//
//    Original colors restored...

注解

对属性所做的更改 BackgroundColor 只会影响在背景色更改后写入各个字符单元格的输出。 若要整体更改控制台窗口的背景色,请设置 BackgroundColor 属性并调用 Clear 方法。 下面的示例进行了这方面的演示。

C#
using System;

public class Example
{
   public static void Main()
   {
      WriteCharacterStrings(1, 26, true);
      Console.MoveBufferArea(0, Console.CursorTop - 10, 30, 1,
                             Console.CursorLeft, Console.CursorTop + 1);
      Console.CursorTop = Console.CursorTop + 3;
      Console.WriteLine("Press any key...");
      Console.ReadKey();

      Console.Clear();
      WriteCharacterStrings(1, 26, false);
   }

   private static void WriteCharacterStrings(int start, int end,
                                             bool changeColor)
   {
      for (int ctr = start; ctr <= end; ctr++) {
         if (changeColor)
            Console.BackgroundColor = (ConsoleColor) ((ctr - 1) % 16);

         Console.WriteLine(new String((char)(ctr + 64), 30));
      }
   }
}

对于基于 Windows 的应用程序(其中,控制台不存在)的 get 操作返回 ConsoleColor.Black 。 Unix 系统并不提供用于提取当前控制台颜色的任何常规机制。 因此,将 BackgroundColor 返回 (ConsoleColor)-1 直到使用 setter) 以显式方式设置 (。

适用于

产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1