Color.ToString 메서드
정의
public:
override System::String ^ ToString();
public override string ToString ();
override this.ToString : unit -> string
Public Overrides Function ToString () As String
반환
Color 메서드 또는 Color 메서드를 사용하여 미리 정의된 색으로 FromName(String)를 만들 경우 이 FromKnownColor(KnownColor)의 이름인 문자열을 반환하고, 그렇지 않으면 ARGB 구성 요소 이름과 그 값으로 구성된 문자열을 반환합니다.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.
예제
다음 코드 예제는 Windows Forms와 함께 사용 하도록 설계 되었으며 PaintEventArgs e
이벤트 처리기의 매개 변수인가 필요 합니다 Paint .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. 코드는 다음 작업을 수행합니다.The code performs the following actions:
KnownColor열거형 요소를 반복 하 여 0이 아닌 녹색 구성 요소와 영 (0) 빨강 구성 요소가 있고 시스템 색이 아닌 모든 알려진 색을 찾습니다.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.
반복 하는 동안는 KnownColor 배열에서 조건과 일치 하는 경우 요소를 저장 합니다.During each iteration, saves the KnownColor element - if it matches the criteria - in an array.
브러시를 사용 하 여 사각형을 그립니다.Uses a brush to paint rectangles. 각 사각형은 KnownColor 첫 번째 단계에 명시 된 조건과 일치 하는를 그립니다.Each of the rectangles is painted a KnownColor that matches the criteria stated in the first step. 의 이름과 KnownColor 해당 구성 요소 값도 표시 됩니다.The name of the KnownColor and its component values are also displayed.
이 예에서는 알려진 특정 색을 표시 하 고를 사용 하 여 ToString 색의 이름과 네 가지 구성 요소 값을 표시 합니다.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
설명
미리 정의 된 색을 알려진 색이 라고도 하며, 열거형의 요소로 표시 됩니다 KnownColor .A predefined color is also called a known color and is represented by an element of the KnownColor enumeration. 메서드를 ToString 사용 하 여 만든 구조에 메서드를 적용 하는 경우 Color FromArgb ToString argb 값이 미리 정의 된 색의 argb 값과 일치 하더라도는 argb 구성 요소 이름과 해당 값으로 구성 된 문자열을 반환 합니다.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.