C# 如何在windows窗体中随机化颜色枚举?

C# 如何在windows窗体中随机化颜色枚举?,c#,forms,C#,Forms,} 我正在尝试随机化SolidBrush(randomColor),这样当我移动鼠标时,它的颜色会相应地改变和绘制。我尝试了上面的代码,它给了我“mscorlib.dll中发生了类型为'System.ArgumentException'的未处理异常”。你能查一下我的随机化代码吗?在我添加代码之前,它根据我预定的颜色按预期工作。如果您想要随机颜色,您必须创建随机数,使其成为颜色枚举允许的最大最小值 我认为最好不要使用颜色枚举,而是使用旧的RGB构造函数 using System; using Sy

}


我正在尝试随机化SolidBrush(randomColor),这样当我移动鼠标时,它的颜色会相应地改变和绘制。我尝试了上面的代码,它给了我“mscorlib.dll中发生了类型为'System.ArgumentException'的未处理异常”。你能查一下我的随机化代码吗?在我添加代码之前,它根据我预定的颜色按预期工作。

如果您想要随机颜色,您必须创建随机数,使其成为颜色枚举允许的最大最小值

我认为最好不要使用颜色枚举,而是使用旧的RGB构造函数

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace painter
{
public partial class Form1 : Form
{        
    Array values = Enum.GetValues(typeof(Color));
    Random random = new Random();

    public bool shouldpaint = false;
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        shouldpaint = true;
    }

    private void Form1_MouseUp(object sender, MouseEventArgs e)
    {
        shouldpaint = false;
    }

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        if (shouldpaint == true)
        {
            Graphics a = CreateGraphics();

            Color randomColor = (Color)values.GetValue(random.Next(values.Length));

            a.FillEllipse(new SolidBrush(randomColor), e.X, e.Y, 5, 5);
        }
    }
}
在c#中:


编辑:正如@Enigmativity所说,256是唯一的上限,因此这将为您提供从0到255的随机数。如果您想要随机颜色,您必须创建随机数,并将其转换为颜色枚举允许的最大值和最小值

我认为最好不要使用颜色枚举,而是使用旧的RGB构造函数

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace painter
{
public partial class Form1 : Form
{        
    Array values = Enum.GetValues(typeof(Color));
    Random random = new Random();

    public bool shouldpaint = false;
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        shouldpaint = true;
    }

    private void Form1_MouseUp(object sender, MouseEventArgs e)
    {
        shouldpaint = false;
    }

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        if (shouldpaint == true)
        {
            Graphics a = CreateGraphics();

            Color randomColor = (Color)values.GetValue(random.Next(values.Length));

            a.FillEllipse(new SolidBrush(randomColor), e.X, e.Y, 5, 5);
        }
    }
}
在c#中:


编辑:正如@Enigmativity所说,256是唯一的上限,因此这将为您提供从0到255的随机数。如果您想要随机颜色,您必须创建随机数,并将其转换为颜色枚举允许的最大值和最小值

我认为最好不要使用颜色枚举,而是使用旧的RGB构造函数

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace painter
{
public partial class Form1 : Form
{        
    Array values = Enum.GetValues(typeof(Color));
    Random random = new Random();

    public bool shouldpaint = false;
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        shouldpaint = true;
    }

    private void Form1_MouseUp(object sender, MouseEventArgs e)
    {
        shouldpaint = false;
    }

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        if (shouldpaint == true)
        {
            Graphics a = CreateGraphics();

            Color randomColor = (Color)values.GetValue(random.Next(values.Length));

            a.FillEllipse(new SolidBrush(randomColor), e.X, e.Y, 5, 5);
        }
    }
}
在c#中:


编辑:正如@Enigmativity所说,256是唯一的上限,因此这将为您提供从0到255的随机数。如果您想要随机颜色,您必须创建随机数,并将其转换为颜色枚举允许的最大值和最小值

我认为最好不要使用颜色枚举,而是使用旧的RGB构造函数

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace painter
{
public partial class Form1 : Form
{        
    Array values = Enum.GetValues(typeof(Color));
    Random random = new Random();

    public bool shouldpaint = false;
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        shouldpaint = true;
    }

    private void Form1_MouseUp(object sender, MouseEventArgs e)
    {
        shouldpaint = false;
    }

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        if (shouldpaint == true)
        {
            Graphics a = CreateGraphics();

            Color randomColor = (Color)values.GetValue(random.Next(values.Length));

            a.FillEllipse(new SolidBrush(randomColor), e.X, e.Y, 5, 5);
        }
    }
}
在c#中:


编辑:正如@Enigmativity所说的,256是唯一的上限,因此这将给出0到255之间的随机数快速提醒一下,
Random.Next(int,int)
的第二个参数是一个独占的最大值,表示
或和数。Next(0255)
将只生成从
0
254
的值。BTW Random类为真正的随机数提供一个伪随机数,考虑使用Stask.Survith.CytoLogic中的RngCuthToReVeServices提供程序,检查链接——你在C++中完美地给出的例子。谢谢。@derape我承认我错了。但我认为这是一个简单的礼貌问题。反正没问题:)这个问题有C#标志,而不是VB;-)快速提醒一下,
Random.Next(int,int)
的第二个参数是一个独占的最大值,表示
或和数。Next(0255)
将只生成从
0
254
的值。BTW Random类为真正的随机数提供一个伪随机数,考虑使用Stask.Survith.CytoLogic中的RngCuthToReVeServices提供程序,检查链接——你在C++中完美地给出的例子。谢谢。@derape我承认我错了。但我认为这是一个简单的礼貌问题。反正没问题:)这个问题有C#标志,而不是VB;-)快速提醒一下,
Random.Next(int,int)
的第二个参数是一个独占的最大值,表示
或和数。Next(0255)
将只生成从
0
254
的值。BTW Random类为真正的随机数提供一个伪随机数,考虑使用Stask.Survith.CytoLogic中的RngCuthToReVeServices提供程序,检查链接——你在C++中完美地给出的例子。谢谢。@derape我承认我错了。但我认为这是一个简单的礼貌问题。反正没问题:)这个问题有C#标志,而不是VB;-)快速提醒一下,
Random.Next(int,int)
的第二个参数是一个独占的最大值,表示
或和数。Next(0255)
将只生成从
0
254
的值。BTW Random类为真正的随机数提供一个伪随机数,考虑使用Stask.Survith.CytoLogic中的RngCuthToReVeServices提供程序,检查链接——你在C++中完美地给出的例子。谢谢。@derape我承认我错了。但我认为这是一个简单的礼貌问题。反正没问题:)