如何获得画笔颜色?

How to get brushes color?

FormDesign 中有按钮。如果单击第一个按钮,

        brush = Brushes.Red;

每个按钮都有这样的代码。我想在那一刻获得画笔颜色。我怎样才能得到它;

Color c = (color of brush);

这边?

编辑:我想将颜色数据保存在列表中。

您可以使用 Control.BackColorControl.ForeColor 获得颜色。

Brush is a parent class for various type of brushes; only the SolidBrush 有一个颜色 属性。

因此您需要转换为 SolidBrush:

或者:

Brush b1 = Brushes.Red;
Color c1 = ((SolidBrush)b1).Color;

SolidBrush b2 = (SolidBrush)Brushes.Red;
Color c2 = b2.Color;

下面的代码说明了更改按钮颜色的按钮单击,按钮颜色存储在 String

 private void button1_Click(object sender, EventArgs e)
      {
         Brush brush = new SolidBrush(Color.Red);
         button1.BackColor = ((SolidBrush)brush).Color;

         string getColor;
         getColor = button1.BackColor.ToString();
         MessageBox.Show($"Color of Button1  " + getColor);

      }

private void button1_Click(object sender, EventArgs e)
      {
         Brush brush1 = Brushes.Red;
     button1.BackColor = ((SolidBrush)brush1).Color;

     string getColor1;
     getColor1 = button1.BackColor.ToString();
     MessageBox.Show($"Color of Button1  " + getColor1);

     //Similarly store other button colors in a string
     string getColor2 = "Orange"; string getColor3 = "Blue"; 

     //Store these string value in a list 
     List<string> colors = new List<string>();
     colors.Add(getColor1);
     colors.Add(getColor2);
     colors.Add(getColor3);
     foreach (string color in colors) { MessageBox.Show(color); }
      }