Color.ToArgb メソッド

定義

この Color 構造体の 32 ビットの ARGB 値を取得します。

public:
 int ToArgb();
public int ToArgb ();
member this.ToArgb : unit -> int
Public Function ToArgb () As Integer

戻り値

この Color の 32 ビットの ARGB 値。

次のコード例は、Windows フォームで使用するように設計されており、イベント ハンドラーのPaintパラメーターである が必要PaintEventArgseです。 コードは、次のアクションを実行します。

  • 列挙要素を KnownColor 反復処理して、0 以外の緑のコンポーネントと 0 値の赤のコンポーネントを持ち、システムの色ではないすべての既知の色を検索します。

  • 各繰り返しの間に、 要素が条件と KnownColor 一致する場合は、 配列に保存されます。

  • ブラシを使用して四角形を描画します。

各四角形には、最初の行頭文字に記載されている条件に一致する が描画 KnownColor されます。 の名前 KnownColor とそのコンポーネント値も表示されます。

次の使用例は、特定の既知の色、色の名前、および 4 つのコンポーネント値を表示します。 メソッドは ToArgb 、コンポーネント値を表示するための準備段階として使用されます。

void ToArgbToStringExample1( 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 ToArgbToStringExample1(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 ToArgbToStringExample1(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

注釈

32 ビット ARGB 値のバイト順は AARRGGBB です。 AA で表される最上位バイト (MSB) はアルファ 成分値です。 RR、GG、BB で表される 2 番目、3 番目、4 番目のバイトはそれぞれ赤、緑、青の色成分です

適用対象