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

示例

下面的代码示例演示 了 A和 成员的 ColorRFromNameGBImplicit 成员。

此示例旨在与 Windows 窗体一起使用。 将代码粘贴到窗体中, ShowPropertiesOfSlateBlue 然后从窗体的事件 Paint 处理方法调用 方法,作为 ePaintEventArgs传递。

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) 。

适用于