Color.ToString メソッドとは? わかりやすく解説

Color.ToString メソッド

この Color 構造体ユーザー判読できる文字列変換します

名前空間: System.Drawing
アセンブリ: System.Drawing (system.drawing.dll 内)
構文構文

Public Overrides Function
 ToString As String
public String ToString ()

戻り値
Color が FromName メソッドまたは FromKnownColor メソッドによって定義済みの色から作成されている場合はこの Color の名前を示す文字列。それ以外場合は ARGB コンポーネント名とその値で構成される文字列

解説解説

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

使用例使用例

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

この例では、一部既知の色を表示しToString使用して既知の色の名前とそれぞれの 4 つコンポーネントの値を示します

Public Sub ToArgbToStringExample2(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
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;
    }
}
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.
   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 ToArgbToStringExample2(PaintEventArgs
 e)
{
    Graphics g = e.get_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 = (KnownColor)0; 
            enumValue.CompareTo(KnownColor.YellowGreen) <= 0; enumValue++) {

        someColor = Color.FromKnownColor(enumValue);
        if (someColor.get_G() != 0 && someColor.get_R()
 == 0 && 
            !(someColor.get_IsSystemColor())) {
            colorMatches.set_Item(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(
            (KnownColor)colorMatches.get_Item(i));
        myBrush1.set_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.get_Black(), 
         x + 55, y);
        someColor = Color.FromArgb(someColor.ToArgb());
        g.DrawString(someColor.ToString(), myFont, Brushes.get_Black(),
         x + 55, y + 15);
        y += 40;
    }
} //ToArgbToStringExample2
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

Color.ToString メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Color.ToString メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS