Color.ToString 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將這個 Color 結構轉換成人類可讀取的字串。
public:
override System::String ^ ToString();
public override string ToString ();
override this.ToString : unit -> string
Public Overrides Function ToString () As String
傳回
如果 Color 是使用 FromName(String) 方法或 FromKnownColor(KnownColor) 方法,從預先定義的色彩建立 Color名稱的字串;否則,由 ARGB 元件名稱及其值所組成的字串。
範例
下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse
,這是 Paint 事件處理程式的參數。 程式代碼會執行下列動作:
逐一查看 KnownColor 列舉元素,以尋找具有非零綠色元件和零值紅色元件且不是系統色彩的所有已知色彩。
在每個反覆項目期間,會將 KnownColor 項目儲存在陣列中,如果它符合準則則為 。
使用筆刷繪製矩形。 每個矩形都會繪製 KnownColor,以符合第一個步驟中所述的準則。 也會顯示 KnownColor 及其元件值的名稱。
本範例會顯示特定已知色彩,並使用 ToString 來顯示色彩的名稱及其四個元件值。
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 列舉的 元素表示。 當 ToString 方法套用至使用 FromArgb 方法建立的 Color 結構時,ToString 會傳回包含 ARGB 元件名稱和其值的字串,即使 ARGB 值符合預先定義色彩的 ARGB 值也一樣。