Freigeben über


Color.ToString-Methode

Konvertiert diese Color-Struktur in eine Klartextzeichenfolge.

Namespace: System.Drawing
Assembly: System.Drawing (in system.drawing.dll)

Syntax

'Declaration
Public Overrides Function ToString As String
'Usage
Dim instance As Color
Dim returnValue As String

returnValue = instance.ToString
public override string ToString ()
public:
virtual String^ ToString () override
public String ToString ()
public override function ToString () : String

Rückgabewert

Eine Zeichenfolge, die der Name dieser Color ist, wenn die Color aus einer vordefinierten Farbe mithilfe der FromName-Methode oder der FromKnownColor-Methode erstellt wurde, andernfalls eine Zeichenfolge, die aus den ARGB-Komponentennamen und -werten besteht.

Hinweise

Vordefinierte Farben werden auch als bekannte Farben bezeichnet und durch ein Element der KnownColor-Enumeration dargestellt. Wenn die ToString-Methode auf eine Color-Struktur angewendet wird, die mithilfe der FromArgb-Methode erstellt wurde, gibt ToString eine Zeichenfolge zurück, die die Namen der ARGB-Komponenten und ihre Werte enthält, auch wenn der ARGB-Wert mit dem ARGB-Wert einer vordefinierten Farbe übereinstimmt.

Beispiel

Das folgende Codebeispiel ist für die Verwendung mit Windows Forms vorgesehen und erfordert PaintEventArgse, wobei es sich um einen Parameter des Paint-Ereignishandlers handelt. Der Code führt die folgenden Aktionen aus:

  • Durchläuft die KnownColor-Enumerationselemente, um alle bekannten Farben zu suchen, deren Grünanteil nicht 0 (null) ist, deren Rotanteil 0 ist und die keine Systemfarben sind.

  • In jedem Durchlauf wird das KnownColor-Element in einem Array gespeichert, sofern es den Kriterien entspricht.

  • Verwendet einen Pinsel zum Zeichnen von Rechtecken. Jedes Rechteck wird in einer KnownColor gezeichnet, die den im ersten Schritt angegebenen Kriterien entspricht. Der Name der KnownColor und ihre Komponentenwerte werden ebenfalls angezeigt.

In diesem Beispiel werden bestimmte bekannte Farben dargestellt, und ToString wird zum Anzeigen der Namen der Farben und ihrer vier Komponentenwerte verwendet.

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
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;
    }
}
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.get_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 = (KnownColor)0; 
            enumValue.CompareTo(KnownColor.YellowGreen) <= 0; enumValue++) {

        someColor = Color.FromKnownColor(enumValue);
        if (someColor.get_G() != 0 && someColor.get_R() == 0 && 
            !(someColor.get_IsSystemColor())) {
            colorMatches.set_Item(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(
            (KnownColor)colorMatches.get_Item(i));
        myBrush1.set_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.get_Black(), 
         x + 55, y);
        someColor = Color.FromArgb(someColor.ToArgb());
        g.DrawString(someColor.ToString(), myFont, Brushes.get_Black(),
         x + 55, y + 15);
        y += 40;
    }
} //ToArgbToStringExample2

Plattformen

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

Color-Struktur
Color-Member
System.Drawing-Namespace