英語で読む

次の方法で共有


Color.ToString メソッド

定義

この Color 構造体を人間が判読できる文字列に変換します。

C#
public override string ToString();

戻り値

FromName(String) メソッドまたは FromKnownColor(KnownColor) メソッドを使用して定義済みの色から Color が作成された場合、この Colorの名前である文字列。それ以外の場合は、ARGB コンポーネント名とその値で構成される文字列。

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

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

  • 反復処理のたびに、KnownColor 要素 (条件と一致する場合) を配列に保存します。

  • ブラシを使用して四角形を塗りつぶします。 各四角形は、最初の手順で示した条件に一致する KnownColor に塗りつぶされます。 KnownColor の名前とそのコンポーネント値も表示されます。

次の使用例は、特定の既知の色を表示し、ToString を使用して色の名前とその 4 つのコンポーネント値を表示します。

C#
public void ToArgbToStringExample2(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;
    }
}

注釈

定義済みの色は既知の色とも呼ばれ、KnownColor 列挙体の要素によって表されます。 ToString メソッドが、FromArgb メソッドを使用して作成された Color 構造体に適用されると、ToString は、ARGB の値が定義済みの色の ARGB 値と一致する場合でも、ARGB コンポーネント名とその値で構成される文字列を返します。

適用対象

製品 バージョン
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1