語言

Color.FromName(String) 方法

定義

從預設顏色的指定名稱建立 Color 結構。

public:
 static System::Drawing::Color FromName(System::String ^ name);
public static System.Drawing.Color FromName(string name);
static member FromName : string -> System.Drawing.Color
Public Shared Function FromName (name As String) As Color

參數

name
String

一個字串,是預設顏色的名稱。 有效名稱與列舉中 KnownColor 元素的名稱相同。

傳回

這個 Color 方法所產生的。

範例

以下程式碼範例展示了 Aa 、 、 RGB 、 和 成員,以及 成員。FromNameColorImplicit

此範例設計用於 Windows 表單。 將程式碼貼入表單,並從表單的ShowPropertiesOfSlateBlue事件處理方法呼叫該Paint方法,傳遞e為 PaintEventArgs。

void ShowPropertiesOfSlateBlue( PaintEventArgs^ e )
{
   Color slateBlue = Color::FromName( "SlateBlue" );
   Byte g = slateBlue.G;
   Byte b = slateBlue.B;
   Byte r = slateBlue.R;
   Byte a = slateBlue.A;
   array<Object^>^temp0 = {a,r,g,b};
   String^ text = String::Format( "Slate Blue has these ARGB values: Alpha:{0}, "
   "red:{1}, green: {2}, blue {3}", temp0 );
   e->Graphics->DrawString( text, gcnew System::Drawing::Font( this->Font,FontStyle::Italic ), gcnew SolidBrush( slateBlue ), RectangleF(PointF(0.0F,0.0F),this->Size) );
}
private void ShowPropertiesOfSlateBlue(PaintEventArgs e)
{
    Color slateBlue = Color.FromName("SlateBlue");
    byte g = slateBlue.G;
    byte b = slateBlue.B;
    byte r = slateBlue.R;
    byte a = slateBlue.A;
    string text = String.Format("Slate Blue has these ARGB values: Alpha:{0}, " +
        "red:{1}, green: {2}, blue {3}", new object[]{a, r, g, b});
    e.Graphics.DrawString(text, 
        new Font(this.Font, FontStyle.Italic), 
        new SolidBrush(slateBlue), 
        new RectangleF(new PointF(0.0F, 0.0F), this.Size));
}
Private Sub ShowPropertiesOfSlateBlue(ByVal e As PaintEventArgs)
    Dim slateBlue As Color = Color.FromName("SlateBlue")
    Dim g As Byte = slateBlue.G
    Dim b As Byte = slateBlue.B
    Dim r As Byte = slateBlue.R
    Dim a As Byte = slateBlue.A
    Dim text As String = _
    String.Format("Slate Blue has these ARGB values: Alpha:{0}, " _
       & "red:{1}, green: {2}, blue {3}", New Object() {a, r, g, b})
    e.Graphics.DrawString(text, New Font(Me.Font, FontStyle.Italic), _
        New SolidBrush(slateBlue), _
        New RectangleF(New PointF(0.0F, 0.0F), _
        Size.op_Implicit(Me.Size)))
End Sub

備註

預先定義的顏色也稱為已知顏色,並由列舉中的 KnownColor 一個元素表示。 若參數 name 不是預設顏色的有效名稱,該 FromName 方法會建立 Color 一個 ARGB 值為 0(即所有 ARGB 元件皆為 0)的結構。

適用於