다음을 통해 공유


Color.ToString 메서드

Color 구조체를 사람이 인식할 수 있는 문자열로 변환합니다.

네임스페이스: System.Drawing
어셈블리: System.Drawing(system.drawing.dll)

구문

‘선언
Public Overrides Function ToString As String
‘사용 방법
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

반환 값

FromName 메서드 또는 FromKnownColor 메서드를 사용하여 미리 정의된 색으로 Color를 만들 경우 이 Color의 이름인 문자열을 반환하고, 그렇지 않으면 ARGB 구성 요소 이름과 그 값으로 구성된 문자열을 반환합니다.

설명

미리 정의된 색은 이미 알려진 색이라고도 부르며, KnownColor 열거형의 요소로 나타냅니다. FromArgb 메서드를 사용하여 만든 Color 구조체에 ToString 메서드를 적용하면, ARGB 값이 미리 정의된 색의 ARGB 값과 일치하더라도 ToString은 ARGB 구성 요소 이름과 그 값으로 구성된 문자열을 반환합니다.

예제

다음 코드 예제는 Windows Forms에 적용되며, 여기에는 Paint 이벤트 처리기의 매개 변수인 PaintEventArgse가 필요합니다. 이 코드는 다음 작업을 수행합니다.

  • 녹색 구성 요소 값이 0이 아니고 빨강 구성 요소 값이 0이며 시스템 색이 아닌 이미 알려진 모든 색을 찾기 위해 KnownColor 열거형 요소 전체를 반복합니다.

  • 반복할 때마다 KnownColor 요소가 기준과 일치하면 이 요소를 배열에 저장합니다.

  • 네모를 그리는 데 브러시를 사용합니다. 각각의 다른 네모는 단계 1에 명시된 기준과 일치하는 KnownColor으로 그려집니다. KnownColor의 이름과 구성 요소 값도 표시됩니다.

이 예제에서는 몇 가지 이미 알려진 색을 표시하고, ToString을 사용하여 색의 이름과 네 개의 구성 요소 값을 표시합니다.

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

플랫폼

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에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0, 1.0에서 지원

참고 항목

참조

Color 구조체
Color 멤버
System.Drawing 네임스페이스