この Color 構造体の 32 ビットの ARGB 値を取得します。
Public Function ToArgb() As Integer
[C#]
public int ToArgb();
[C++]
public: int ToArgb();
[JScript]
public function ToArgb() : int;
戻り値
この Color 構造体の 32 ビットの ARGB 値。
解説
32 ビットの ARGB 値のバイト順は AARRGGBB です。AA で表している最上位バイト (MSB) はアルファ コンポーネントの値です。RR、GG、BB で表している 2 番目、3 番目、4 番目のバイトは、それぞれ赤、緑、青のカラー コンポーネントです。たとえば、色を 4 つのコンポーネントの値から作成したとします。 ToArgb メソッドを使用すると次のように 32 ビットの ARGB 値を取得できます。
使用例
[Visual Basic, C#] 次の例は、Windows フォームでの使用を意図してデザインされており、 Paint イベント ハンドラのパラメータである PaintEventArgs e が必要です。このコードは次のアクションを実行します。
- KnownColor 列挙体要素を反復処理して、値が 0 以外の緑のコンポーネントと値が 0 の赤のコンポーネントを持ち、システム カラーでない既知の色をすべて検索します。
- 各反復処理中に、基準と一致する場合は、配列の KnownColor 要素を保存します。
- ブラシを使用して四角形を塗りつぶします。
[Visual Basic, C#] 各四角形は、最初のステップの基準に一致する KnownColor で塗りつぶされます。KnownColor の名前とそのコンポーネントの値も表示されます。
[Visual Basic, C#] この例では、一部の既知の色とその名前、およびそれぞれの 4 つのコンポーネントの値を示します。各コンポーネントの値を表示するには、まず ToArgb メソッドを使用します。
Public Sub ToArgbToStringExample(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
Dim count As Integer = 0 ' number of matches found
' 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
[C#]
public void ToArgbToStringExample(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];
int count = 0; // number of matches found
// 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;
}
}
[C++, JScript] C++ および JScript のサンプルはありません。Visual Basic および C# のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET