Color.ToString Méthode

Définition

Convertit cette structure Color en chaîne lisible par l'utilisateur.Converts this Color structure to a human-readable string.

public:
 override System::String ^ ToString();
public override string ToString ();
override this.ToString : unit -> string
Public Overrides Function ToString () As String

Retours

String

Chaîne représentant le nom de ce Color, si Color est créé à partir d'une couleur prédéfinie à l'aide de la méthode FromName(String) ou de la méthode FromKnownColor(KnownColor) ; sinon, chaîne composée des noms des composants ARVB et de leurs valeurs.A string that is the name of this Color, if the Color is created from a predefined color by using either the FromName(String) method or the FromKnownColor(KnownColor) method; otherwise, a string that consists of the ARGB component names and their values.

Exemples

L’exemple de code suivant est conçu pour être utilisé avec Windows Forms, et il requiert PaintEventArgs e , qui est un paramètre du Paint Gestionnaire d’événements.The following code example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. Le code effectue les actions suivantes :The code performs the following actions:

  • Itère au sein des KnownColor éléments de l’énumération pour rechercher toutes les couleurs connues qui ont un composant vert différent de zéro et un composant rouge de valeur zéro et qui ne sont pas des couleurs système.Iterates through the KnownColor enumeration elements to find all known colors that have a non-zero green component and a zero-value red component and that are not system colors.

  • Pendant chaque itération, enregistre l' KnownColor élément, s’il correspond aux critères, dans un tableau.During each iteration, saves the KnownColor element - if it matches the criteria - in an array.

  • Utilise un pinceau pour peindre des rectangles.Uses a brush to paint rectangles. Chacun des rectangles est peint un KnownColor qui correspond aux critères spécifiés dans la première étape.Each of the rectangles is painted a KnownColor that matches the criteria stated in the first step. Le nom de KnownColor et ses valeurs de composant sont également affichés.The name of the KnownColor and its component values are also displayed.

Cet exemple affiche certaines couleurs connues et utilise ToString pour afficher les noms des couleurs et leurs quatre valeurs de composant.This example displays certain known colors and uses ToString to display the names of the colors and their four component values.

void ToArgbToStringExample2( PaintEventArgs^ e )
{
   Graphics^ g = e->Graphics;

   // Color structure used for temporary storage.
   Color someColor = Color::FromArgb( 0 );

   // Array to store KnownColor values that match the criteria.
   array<KnownColor>^colorMatches = gcnew array<KnownColor>(167);

   // Number of matches found.
   int count = 0;

   // Iterate through the KnownColor enums to find all corresponding colors
   // that have a nonzero green component and zero-value red component and
   // that are not system colors.
   for ( KnownColor enumValue = (KnownColor)0; enumValue <= KnownColor::YellowGreen; enumValue = enumValue + (KnownColor)1 )
   {
      someColor = Color::FromKnownColor( enumValue );
      if ( someColor.G != 0 && someColor.R == 0 &&  !someColor.IsSystemColor )
               colorMatches[ count++ ] = enumValue;
   }
   SolidBrush^ myBrush1 = gcnew SolidBrush( someColor );
   System::Drawing::Font^ myFont = gcnew System::Drawing::Font( "Arial",9 );
   int x = 40;
   int y = 40;

   // Iterate through the matches that were found and display each color that
   // corresponds with the enum value in the array. also display the name of
   // the KnownColor and the ARGB components.
   for ( int i = 0; i < count; i++ )
   {
      // Display the color.
      someColor = Color::FromKnownColor( colorMatches[ i ] );
      myBrush1->Color = someColor;
      g->FillRectangle( myBrush1, x, y, 50, 30 );
      
      // Display KnownColor name and the four component values. To display the
      // component values:  Use the ToArgb method to get the 32-bit ARGB value
      // of someColor, which was created from a KnownColor. Then create a
      // Color structure from the 32-bit ARGB value and set someColor equal to
      // this new Color structure. Then use the ToString method to convert it to
      // a string.
      g->DrawString( someColor.ToString(), myFont, Brushes::Black, (float)x + 55, (float)y );
      someColor = Color::FromArgb( someColor.ToArgb() );
      g->DrawString( someColor.ToString(), myFont, Brushes::Black, (float)x + 55, (float)y + 15 );
      y += 40;
   }
}
public void ToArgbToStringExample2(PaintEventArgs e)
{
    Graphics     g = e.Graphics;
             
    // Color structure used for temporary storage.
    Color   someColor = Color.FromArgb(0);
             
    // Array to store KnownColor values that match the criteria.
    KnownColor[]  colorMatches = new KnownColor[167];
    
    // Number of matches found.
    int  count = 0;   
             
    // Iterate through the KnownColor enums to find all corresponding colors
    // that have a nonzero green component and zero-value red component and
    // that are not system colors.
    for (KnownColor enumValue = 0;
        enumValue <= KnownColor.YellowGreen; enumValue++)
    {
        someColor = Color.FromKnownColor(enumValue);
        if (someColor.G != 0 && someColor.R == 0 && !someColor.IsSystemColor)
            colorMatches[count++] = enumValue;
    }
    SolidBrush  myBrush1 = new SolidBrush(someColor);
    Font        myFont = new Font("Arial", 9);
    int         x = 40;
    int         y = 40;
             
    // Iterate through the matches that were found and display each color that
    // corresponds with the enum value in the array. also display the name of
    // the KnownColor and the ARGB components.
    for (int i = 0; i < count; i++)
    {
        // Display the color.
        someColor = Color.FromKnownColor(colorMatches[i]);
        myBrush1.Color = someColor;
        g.FillRectangle(myBrush1, x, y, 50, 30);
             
        // Display KnownColor name and the four component values. To display the
        // component values:  Use the ToArgb method to get the 32-bit ARGB value
        // of someColor, which was created from a KnownColor. Then create a
        // Color structure from the 32-bit ARGB value and set someColor equal to
        // this new Color structure. Then use the ToString method to convert it to
        // a string.
        g.DrawString(someColor.ToString(), myFont, Brushes.Black, x + 55, y);
        someColor = Color.FromArgb(someColor.ToArgb());
        g.DrawString(someColor.ToString(), myFont, Brushes.Black, x + 55, y + 15);
        y += 40;
    }
}
Public Sub ToArgbToStringExample2(ByVal e As PaintEventArgs)
    Dim g As Graphics = e.Graphics

    ' Color structure used for temporary storage.
    Dim someColor As Color = Color.FromArgb(0)

    ' Array to store KnownColor values that match the criteria.
    Dim colorMatches(167) As KnownColor

    ' Number of matches found.
    Dim count As Integer = 0

    ' Iterate through KnownColor enums to find all corresponding colors
    ' that have a non-zero green component and zero-valued red
    ' component and that are not system colors.
    Dim enumValue As KnownColor
    For enumValue = 0 To KnownColor.YellowGreen
        someColor = Color.FromKnownColor(enumValue)
        If someColor.G <> 0 And someColor.R = 0 And _
        Not someColor.IsSystemColor Then
            colorMatches(count) = enumValue
            count += 1
        End If
    Next enumValue
    Dim myBrush1 As New SolidBrush(someColor)
    Dim myFont As New Font("Arial", 9)
    Dim x As Integer = 40
    Dim y As Integer = 40

    ' Iterate through the matches found and display each color that
    ' corresponds with the enum value in the array. Also display the
    ' name of the KnownColor and the ARGB components.
    Dim i As Integer
    For i = 0 To count - 1

        ' Display the color
        someColor = Color.FromKnownColor(colorMatches(i))
        myBrush1.Color = someColor
        g.FillRectangle(myBrush1, x, y, 50, 30)

        ' Display KnownColor name and four component values. To display
        ' component values:  Use the ToArgb method to get the 32-bit
        ' ARGB value of someColor (created from a KnownColor). Create
        ' a Color structure from the 32-bit ARGB value and set someColor
        ' equal to this new Color structure. Then use the ToString method
        ' to convert it to a string.
        g.DrawString(someColor.ToString(), myFont, Brushes.Black, _
        x + 55, y)
        someColor = Color.FromArgb(someColor.ToArgb())
        g.DrawString(someColor.ToString(), myFont, Brushes.Black, _
        x + 55, y + 15)
        y += 40
    Next i
End Sub

Remarques

Une couleur prédéfinie est également appelée couleur connue et est représentée par un élément de l' KnownColor énumération.A predefined color is also called a known color and is represented by an element of the KnownColor enumeration. Quand la ToString méthode est appliquée à une Color structure créée à l’aide de la FromArgb méthode, ToString retourne une chaîne qui se compose des noms des composants ARVB et de leurs valeurs, même si la valeur ARVB correspond à la valeur ARVB d’une couleur prédéfinie.When the ToString method is applied to a Color structure that is created by using the FromArgb method, ToString returns a string that consists of the ARGB component names and their values, even if the ARGB value matches the ARGB value of a predefined color.

S’applique à