Color.FromKnownColor(KnownColor) 方法

定義

從指定的預先定義色彩建立 Color 結構。

public:
 static System::Drawing::Color FromKnownColor(System::Drawing::KnownColor color);
public static System.Drawing.Color FromKnownColor (System.Drawing.KnownColor color);
static member FromKnownColor : System.Drawing.KnownColor -> System.Drawing.Color
Public Shared Function FromKnownColor (color As KnownColor) As Color

參數

color
KnownColor

KnownColor 列舉型別的元素。

傳回

這個方法建立的 Color

範例

下列程式碼範例是設計來搭配Windows Forms使用,而且需要 PaintEventArgse ,這是事件處理常式的參數 Paint 。 此程式碼會執行下列動作:

  • 建立結構 redShade 實例 Color ,以用於比較。

  • 逐一查看 KnownColor 列舉專案,以尋找所有與 相同的 redShade 已知色彩。 找到 15 個相符專案,或迴圈計數器的值大於最後 KnownColor 一個專案時,就會終止反復專案。

  • 在每個反復專案期間, KnownColor 如果專案符合準則,則會儲存在陣列中。

  • 使用筆刷繪製矩形。

第一個矩形會繪製以 表示的 redShade 色彩。 每一個其他矩形都會繪製 KnownColor 符合 的 redShade 光線。

void KnownColorBrightnessExample1( PaintEventArgs^ e )
{
   Graphics^ g = e->Graphics;

   // Color structures. One is a variable used for temporary storage. The other
   // is a constant used for comparisons.
   Color someColor = Color::FromArgb( 0 );
   Color redShade = Color::FromArgb( 255, 200, 0, 100 );

   // Array to store KnownColor values that match the brightness of the
   // redShade color.
   array<KnownColor>^colorMatches = gcnew array<KnownColor>(15);

   // Number of matches found.
   int count = 0;

   // Iterate through the KnownColor enums until 15 matches are found.
   for ( KnownColor enumValue = (KnownColor)0; enumValue <= KnownColor::YellowGreen && count < 15; enumValue = enumValue + (KnownColor)1 )
   {
      someColor = Color::FromKnownColor( enumValue );
      if ( someColor.GetBrightness() == redShade.GetBrightness() )
               colorMatches[ count++ ] = enumValue;
   }

   // Display the redShade color and its argb value.
   SolidBrush^ myBrush1 = gcnew SolidBrush( redShade );
   System::Drawing::Font^ myFont = gcnew System::Drawing::Font( "Arial",12 );
   int x = 20;
   int y = 20;
   someColor = redShade;
   g->FillRectangle( myBrush1, x, y, 100, 30 );
   g->DrawString( someColor.ToString(), myFont, Brushes::Black, (float)x + 120, (float)y );
   
   // 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.
   for ( int i = 0; i < count; i++ )
   {
      y += 40;
      someColor = Color::FromKnownColor( colorMatches[ i ] );
      myBrush1->Color = someColor;
      g->FillRectangle( myBrush1, x, y, 100, 30 );
      g->DrawString( someColor.ToString(), myFont, Brushes::Black, (float)x + 120, (float)y );
   }
}
public void KnownColorBrightnessExample1(PaintEventArgs e)
{
    Graphics     g = e.Graphics;
             
    // Color structures. One is a variable used for temporary storage. The other
    // is a constant used for comparisons.
    Color   someColor = Color.FromArgb(0);
    Color   redShade = Color.FromArgb(255, 200, 0, 100);
             
    // Array to store KnownColor values that match the brightness of the
    // redShade color.
    KnownColor[]  colorMatches = new KnownColor[15];
     
    // Number of matches found.
    int  count = 0;   
          
    // Iterate through the KnownColor enums until 15 matches are found.
    for (KnownColor enumValue = 0;
        enumValue <= KnownColor.YellowGreen && count < 15; enumValue++)
    {
        someColor = Color.FromKnownColor(enumValue);
        if (someColor.GetBrightness() == redShade.GetBrightness())
            colorMatches[count++] = enumValue;
    }
             
    // Display the redShade color and its argb value.
    SolidBrush  myBrush1 = new SolidBrush(redShade);
    Font        myFont = new Font("Arial", 12);
    int         x = 20;
    int         y = 20;
    someColor = redShade;
    g.FillRectangle(myBrush1, x, y, 100, 30);
    g.DrawString(someColor.ToString(), myFont, Brushes.Black, x + 120, y);
             
    // 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.
    for (int i = 0; i < count; i++)
    {
        y += 40;
        someColor = Color.FromKnownColor(colorMatches[i]);
        myBrush1.Color = someColor;
        g.FillRectangle(myBrush1, x, y, 100, 30);
        g.DrawString(someColor.ToString(), myFont, Brushes.Black, x + 120, y);
    }
}
Public Sub KnownColorBrightnessExample1(ByVal e As PaintEventArgs)
    Dim g As Graphics = e.Graphics

    ' Color structures. One is used for temporary storage. The other
    ' is a constant used for comparisons.
    Dim someColor As Color = Color.FromArgb(0)
    Dim redShade As Color = Color.FromArgb(255, 200, 0, 100)

    ' Array to store KnownColor values that match the brightness of the
    ' redShade color.
    Dim colorMatches(15) As KnownColor

    ' Number of matches found.
    Dim count As Integer = 0

    ' iterate through the KnownColor enums until 15 matches are found.
    Dim enumValue As KnownColor
    For enumValue = 0 To KnownColor.YellowGreen
        someColor = Color.FromKnownColor(enumValue)
        If (someColor.GetBrightness()) = (redShade.GetBrightness()) Then
            colorMatches(count) = enumValue
            count += 1
            If count > 15 Then
                Exit For
            End If
        End If
    Next enumValue

    ' Display the redShade color and its argb value.
    Dim myBrush1 As New SolidBrush(redShade)
    Dim myFont As New Font("Arial", 12)
    Dim x As Integer = 20
    Dim y As Integer = 20
    someColor = redShade
    g.FillRectangle(myBrush1, x, y, 100, 30)
    g.DrawString(someColor.ToString(), myFont, Brushes.Black, _
    x + 120, y)

    ' Iterate through the matches that were found and display each
    ' color that corresponds with the enum value in the array.
    ' Display the name of the KnownColor.
    Dim i As Integer
    For i = 0 To count - 1
        y += 40
        someColor = Color.FromKnownColor(colorMatches(i))
        myBrush1.Color = someColor
        g.FillRectangle(myBrush1, x, y, 100, 30)
        g.DrawString(someColor.ToString(), myFont, Brushes.Black, _
        x + 120, y)
    Next i
End Sub

備註

預先定義的色彩也稱為已知色彩,並以 列舉的 KnownColor 元素表示。

適用於