Exporter (0) Imprimer
Développer tout

Console.ForegroundColor, propriété

Remarque : cette propriété est nouvelle dans le .NET Framework version 2.0.

Obtient ou définit la couleur de premier plan de la console.

Espace de noms : System
Assembly : mscorlib (dans mscorlib.dll)

public static ConsoleColor ForegroundColor { get; set; }
/** @property */
public static ConsoleColor get_ForegroundColor ()

/** @property */
public static void set_ForegroundColor (ConsoleColor value)

public static function get ForegroundColor () : ConsoleColor

public static function set ForegroundColor (value : ConsoleColor)

Valeur de la propriété

ConsoleColor qui spécifie la couleur de premier plan de la console ; autrement dit, la couleur de chaque caractère affiché. La valeur par défaut est gris.

Type d'exceptionCondition

ArgumentException

La valeur spécifiée pour une opération de définition n'est pas un membre valide de System.ConsoleColor.

SecurityException

L'utilisateur n'a pas l'autorisation d'effectuer cette action.

IOException

Une erreur d'E/S s'est produite.

Une opération d'obtention pour une application Windows, dans laquelle n'existe pas de console, retourne Gray.

L'exemple de code suivant illustre l'utilisation conjointe de l'énumération System.ConsoleColor, des propriétés Console.ForegroundColor et Console.BackgroundColor, et de la méthode Console.ResetColor. Vous devez exécuter l'exemple sur une console afin de pouvoir observer les effets de couleur décrits.

Deux constantes de l'exemple, Black et White, sont spécifiées explicitement et toutes les autres constantes (Blue, DarkRed, etc.) sont spécifiées indirectement. Dans un premier temps, la méthode GetNames, héritée de la classe Enum, est utilisée pour obtenir les noms des constantes. Ensuite, la méthode Enum.Parse utilise chaque nom pour créer la constante énumérée correspondante.

// This example demonstrates the ConsoleColor enumeration.
using System;

class Sample 
{
    public static void Main() 
    {
    String nl = Environment.NewLine;
    String[] colorNames = ConsoleColor.GetNames(typeof(ConsoleColor));

// ---------------------------------------------------------------------------------------
    Console.WriteLine("{0}All the foreground colors on a constant black background.", nl);
    Console.WriteLine("  (Black on black is not readable.){0}", nl);

    for (int x = 0; x < colorNames.Length; x++)
    {
    Console.Write("{0,2}: ", x);
    Console.BackgroundColor = ConsoleColor.Black;
    Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), colorNames[x]);
    Console.Write("This is foreground color {0}.", colorNames[x]);
    Console.ResetColor();
    Console.WriteLine();
    }
// ---------------------------------------------------------------------------------------
    Console.WriteLine("{0}A constant white foreground on all the background colors.", nl);
    Console.WriteLine("  (White on white is not readable.){0}", nl);

    for (int x = 0; x < colorNames.Length; x++)
    {
    Console.Write("{0,2}: ", x);
    Console.ForegroundColor = ConsoleColor.White;
    Console.BackgroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), colorNames[x]);
    Console.Write("This is background color {0}.", colorNames[x]);
    Console.ResetColor();
    Console.WriteLine();
    }
// ---------------------------------------------------------------------------------------
    }
}
/*
This example produces the following results:

All the foreground colors on a constant black background.
  (Black on black is not readable.)

 0: This is foreground color Black.
 1: This is foreground color DarkBlue.
 2: This is foreground color DarkGreen.
 3: This is foreground color DarkCyan.
 4: This is foreground color DarkRed.
 5: This is foreground color DarkMagenta.
 6: This is foreground color DarkYellow.
 7: This is foreground color Gray.
 8: This is foreground color DarkGray.
 9: This is foreground color Blue.
10: This is foreground color Green.
11: This is foreground color Cyan.
12: This is foreground color Red.
13: This is foreground color Magenta.
14: This is foreground color Yellow.
15: This is foreground color White.

A constant white foreground on all the background colors.
  (White on white is not readable.)

 0: This is background color Black.
 1: This is background color DarkBlue.
 2: This is background color DarkGreen.
 3: This is background color DarkCyan.
 4: This is background color DarkRed.
 5: This is background color DarkMagenta.
 6: This is background color DarkYellow.
 7: This is background color Gray.
 8: This is background color DarkGray.
 9: This is background color Blue.
10: This is background color Green.
11: This is background color Cyan.
12: This is background color Red.
13: This is background color Magenta.
14: This is background color Yellow.
15: This is background color White.
*/

// This example demonstrates the ConsoleColor enumeration.
import System.*;

class Sample
{
    public static void main(String[] args)
    {
        String nl = Environment.get_NewLine();
        String colorNames[] = ConsoleColor.GetNames(ConsoleColor.class.ToType());
        // ----------------------------------------------------------------------
        Console.WriteLine("{0}All the foreground colors on a constant black "
            + " background.", nl);
        Console.WriteLine("  (Black on black is not readable.){0}", nl);

        for (int x = 0; x < colorNames.get_Length(); x++) {
            Console.Write("{0,2}: ", System.Convert.ToString(x));
            Console.set_BackgroundColor(ConsoleColor.Black);
            Console.set_ForegroundColor(((ConsoleColor)(Enum.Parse(
                ConsoleColor.class.ToType(), System.Convert.ToString(
                colorNames.get_Item(x))))));
            Console.Write("This is foreground color {0}.",
                colorNames.get_Item(x));
            Console.ResetColor();
            Console.WriteLine();
        }
        // ----------------------------------------------------------------------
        Console.WriteLine("{0}A constant white foreground on all the "
            + "background colors.", nl);
        Console.WriteLine("  (White on white is not readable.){0}", nl);

        for (int x = 0; x < colorNames.get_Length(); x++) {
            Console.Write("{0,2}: ", System.Convert.ToString(x));
            Console.set_ForegroundColor(ConsoleColor.White);
            Console.set_BackgroundColor(((ConsoleColor)(Enum.Parse(
                ConsoleColor.class.ToType(), 
                System.Convert.ToString(colorNames.get_Item(x))))));
            Console.Write("This is background color {0}.", 
                colorNames.get_Item(x));
            Console.ResetColor();
            Console.WriteLine();
        }
        // ----------------------------------------------------------------------
    } //main
} //Sample
/*
This example produces the following results:

All the foreground colors on a constant black background.
  (Black on black is not readable.)

 0: This is foreground color Black.
 1: This is foreground color DarkBlue.
 2: This is foreground color DarkGreen.
 3: This is foreground color DarkCyan.
 4: This is foreground color DarkRed.
 5: This is foreground color DarkMagenta.
 6: This is foreground color DarkYellow.
 7: This is foreground color Gray.
 8: This is foreground color DarkGray.
 9: This is foreground color Blue.
10: This is foreground color Green.
11: This is foreground color Cyan.
12: This is foreground color Red.
13: This is foreground color Magenta.
14: This is foreground color Yellow.
15: This is foreground color White.

A constant white foreground on all the background colors.
  (White on white is not readable.)

 0: This is background color Black.
 1: This is background color DarkBlue.
 2: This is background color DarkGreen.
 3: This is background color DarkCyan.
 4: This is background color DarkRed.
 5: This is background color DarkMagenta.
 6: This is background color DarkYellow.
 7: This is background color Gray.
 8: This is background color DarkGray.
 9: This is background color Blue.
10: This is background color Green.
11: This is background color Cyan.
12: This is background color Red.
13: This is background color Magenta.
14: This is background color Yellow.
15: This is background color White.
*/

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile pour Pocket PC, Windows Mobile pour Smartphone, Windows Server 2003, Windows XP Édition Media Center, Windows XP Professionnel Édition x64, Windows XP SP2, Windows XP Starter Edition

Le .NET Framework ne prend pas en charge toutes les versions de chaque plate-forme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise.

.NET Framework

Prise en charge dans : 2.0

Ajouts de la communauté

AJOUTER
Afficher:
© 2014 Microsoft