Console.ForegroundColor Console.ForegroundColor Console.ForegroundColor Console.ForegroundColor Property

Définition

Obtient ou définit la couleur de premier plan de la console.Gets or sets the foreground color of the console.

public:
 static property ConsoleColor ForegroundColor { ConsoleColor get(); void set(ConsoleColor value); };
public static ConsoleColor ForegroundColor { get; set; }
member this.ForegroundColor : ConsoleColor with get, set
Public Shared Property ForegroundColor As ConsoleColor

Valeur de propriété

ConsoleColor qui spécifie la couleur de premier plan de la console, c'est-à-dire la couleur de chaque caractère affiché.A ConsoleColor that specifies the foreground color of the console; that is, the color of each character that is displayed. La valeur par défaut est gris.The default is gray.

Exceptions

La couleur spécifiée dans une opération set n’est pas un membre valide de ConsoleColor.The color specified in a set operation is not a valid member of ConsoleColor.

L’utilisateur n’est pas autorisé à effectuer cette action.The user does not have permission to perform this action.

Une erreur d'E/S s'est produite.An I/O error occurred.

Exemples

L’exemple suivant vérifie si la couleur d’arrière-plan de la console est noire et, si c’est le cas, elle modifie la couleur d’arrière-plan en rouge et la couleur de premier plan en noir.The following example checks whether the console's background color is black and, if it is, it changes the background color to red and the foreground color to black.

using System;

public class Example
{
   public static void Main()
   {
      if (Console.BackgroundColor == ConsoleColor.Black) {
         Console.BackgroundColor = ConsoleColor.Red;
         Console.ForegroundColor = ConsoleColor.Black;
         Console.Clear();
      }
   }
}
Module Example
   Public Sub Main()
      If Console.BackgroundColor = ConsoleColor.Black Then
         Console.BackgroundColor = ConsoleColor.Red
         Console.ForegroundColor = ConsoleColor.Black
         Console.Clear()
      End If
   End Sub
End Module

L’exemple suivant enregistre les valeurs de l' ConsoleColor énumération dans un tableau et stocke les valeurs actuelles BackgroundColor des ForegroundColor propriétés et dans des variables.The following example saves the values of the ConsoleColor enumeration to an array and stores the current values of the BackgroundColor and ForegroundColor properties to variables. Il remplace ensuite la couleur de premier plan par chaque couleur ConsoleColor de l’énumération, à l’exception de la couleur qui correspond à l’arrière-plan actuel, et remplace ConsoleColor la couleur d’arrière-plan par chaque couleur de l’énumération, à l’exception de la couleur qui correspond à premier plan actuel.It then changes the foreground color to each color in the ConsoleColor enumeration except to the color that matches the current background, and it changes the background color to each color in the ConsoleColor enumeration except to the color that matches the current foreground. (Si la couleur de premier plan est identique à la couleur d’arrière-plan, le texte n’est pas visible.) Enfin, il appelle la ResetColor méthode pour restaurer les couleurs d’origine de la console.(If the foreground color is the same as the background color, the text isn't visible.) Finally, it calls the ResetColor method to restore the original console colors.

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...
Public Module Example
   Public Sub Main()
      ' Get an array with the values of ConsoleColor enumeration members.
      Dim colors() As ConsoleColor = ConsoleColor.GetValues(GetType(ConsoleColor))
      ' Save the current background and foreground colors.
      Dim currentBackground As ConsoleColor = Console.BackgroundColor
      Dim currentForeground As ConsoleColor = 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)
      For Each color In colors
         If color = currentBackground Then Continue For
          
         Console.ForegroundColor = color
         Console.WriteLine("   The foreground color is {0}.", color)
      Next 
      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)
      For Each color In colors
         If color = currentForeground  then Continue For
         Console.BackgroundColor = color
         Console.WriteLine("   The background color is {0}.", color)
      Next
      ' Restore the original console colors.
      Console.ResetColor
      Console.WriteLine()
      Console.WriteLine("Original colors restored...")
   End Sub
End Module
'The example displays output like the following:
'       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...

Remarques

Une opération d’extraction pour une application Windows, dans laquelle il n’existe pas de console, retourne Gray.A get operation for a Windows-based application, in which a console does not exist, returns Gray.

Sécurité

UIPermission
pour modifier les fenêtres de niveau supérieur et les sous-fenêtres sécurisées.for modifying safe top-level windows and subwindows. Énumération associée:SafeTopLevelWindowsAssociated enumeration: SafeTopLevelWindows

S’applique à