Color.FromName(String) Color.FromName(String) Color.FromName(String) Color.FromName(String) Method

定义

基于预定义颜色的指定名称创建 Color 结构。Creates a Color structure from the specified name of a predefined 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 String String String

作为预定义颜色名称的字符串。A string that is the name of a predefined color. 有效名称与 KnownColor 枚举的元素名称相同。Valid names are the same as the names of the elements of the KnownColor enumeration.

返回

此方法创建的 ColorThe Color that this method creates.

示例

下面的代码示例演示ARGB,并FromName成员Color,和Implicit成员。The following code example demonstrates the A, R, G, B, and FromName members of a Color, and the Implicit member.

此示例旨在与 Windows 窗体一起使用。This example is designed to be used with a Windows Form. 将代码粘贴到窗体并调用ShowPropertiesOfSlateBlue方法从窗体的Paint事件处理方法,传递e作为PaintEventArgsPaste the code into the form and call the ShowPropertiesOfSlateBlue method from the form's Paint event-handling method, passing e as 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枚举。A predefined color is also called a known color and is represented by an element of the KnownColor enumeration. 如果name参数不是有效的名称的预定义颜色FromName方法创建ColorARGB 值为 0 的结构 (即,所有 ARGB 组件都均为 0)。If the name parameter is not the valid name of a predefined color, the FromName method creates a Color structure that has an ARGB value of 0 (that is, all ARGB components are 0).

适用于