谢谢各位的关心,问题终于解决了!
完整的用枚举来随机取填充颜色的程序如下:
enum ColorType//定义枚举
{
Yellow,Red,Blue,Green,Orange
}
public Color GetColor()//随机获得颜色并返回一个参数
{
ColorType c = new ColorType();
System.Type t = c.GetType();
System.Array cArray = Enum.GetValues(t);
Random r = new Random();
int i = r.Next() % cArray.Length + 1;
Color cc = Color.FromName(cArray.GetValue(i).ToString());
return cc;
}
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)//画图并显示出来
{
Pen pen = new Pen(Color.Red,1);
Point pt = new Point(0,0);
Size sz = new Size(200,200);
Rectangle rect = new Rectangle(pt,sz);
e.Graphics.DrawRectangle(pen,rect);
Color c = GetColor();
SolidBrush brush = new SolidBrush(c);
e.Graphics.FillRectangle(brush,rect);
}