简体   繁体   English

C#如何每5秒更改矩形的颜色

[英]c# how to change the color of rectangle every 5 seconds

i have a program that has bouncing squares within a form, i also have to make it so that every 5 seconds the drawn squares change color, i have a timer to update a listbox on the coordinates of each box and a second timer with the interval of 5000 to change the color of the square. 我有一个程序,该程序在窗体内有跳动的正方形,我也必须这样做,以使绘制的正方形每5秒钟更改一次颜色,我有一个计时器来更新每个框的坐标上的列表框,并有一个间隔为第二个计时器5000以更改正方形的颜色。 now i am trying to figure out how exactly do i change the color of the squares with the timer, i have a method called colorchange with a property called rectcolor 现在我想弄清楚如何使用计时器准确更改正方形的颜色,我有一个名为colorchange的方法,其属性为rectcolor

 private Color Rectcolor { get; set; }

 }
    public Color Colorchange()
    {
        Rectcolor =  Color.FromArgb(randcolor.Next(256), randcolor.Next(256), randcolor.Next(256));
        return Rectcolor;
    }

in Form1 i have the timer2 tick event with a foreach going through the boxes Form1我有timer2滴答事件,其中有一个foreach正在通过框

 public partial class Form1 : Form
{
    Random Randcolor = Box.randcolor;
    Random Rand = Box.rand;
    List<Box> Boxes = new List<Box>();

    public Form1()
    {
        InitializeComponent();
        this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
        this.SetStyle(ControlStyles.UserPaint, true);

    }
    private void timer2_Tick(object sender, EventArgs e)
    {
        foreach (Box tmp in Boxes)
        {
            tmp.Colorchange();
        }
    }

when i run the program nothing happens with the moving rectangles, how do i make colorchange actually change the color of the rectangle? 当我运行程序时,移动的矩形什么也没有发生,如何使colorchange实际改变矩形的颜色?

Full Box Class Code 整箱类代码

class Box
{
    protected Rectangle myRectangle;
    protected int xStep;
    protected int yStep;
    protected Form pForm;
    protected Label myLocation;
    protected Color myColor;
    public static Random rand = new Random();
    public static Random randcolor = new Random();

    public Box(int x, int y, int s, Form pF)
    {
        myColor = new Color();
        myColor = Colorchange();
        myLocation = new Label();
        myLocation.BackColor = Color.Transparent;
        myLocation.Left = x;
        myLocation.Top = y;
        myLocation.ForeColor = Colorchange();
        pF.Controls.Add(myLocation);
        pForm = pF;
        myRectangle = new Rectangle(x, y, s, s);
        myRectangle.X = x;
        myRectangle.Y = y;
        xStep = rand.Next(1, 15);
        yStep = rand.Next(1, 15);
    }

    public int X { get { return myRectangle.X; } }
    public int Y { get { return myRectangle.Y; } }
    private int Width { get { return myRectangle.Width; } }
    private int Height { get { return myRectangle.Height; } }
    private Color Rectcolor { get; set; }
    public void Move()
    {
        myRectangle.X += xStep;
        myRectangle.Y += yStep;
        if (myRectangle.X <= 0)
            xStep *= -1;
        if (myRectangle.X + myRectangle.Width >= pForm.ClientSize.Width)
            xStep *= -1;
        if (myRectangle.Y <= 0)
            yStep *= -1;
        if (myRectangle.Y + myRectangle.Height >= pForm.ClientSize.Height)
            yStep *= -1;
        //if (myRectangle.X >500)
        //    xStep *= -1;

    }
    public Color Colorchange()
    {
        Rectcolor =  Color.FromArgb(randcolor.Next(256), randcolor.Next(256), randcolor.Next(256));
        return Rectcolor;
    }


    public static bool Bounce(Box One, Box Two)
    {
        if (One.X + One.Width < Two.X)
            return false;
        if (Two.X + Two.Width < One.X)
            return false;
        if (One.Y + One.Height < Two.Y)
            return false;
        if (Two.Y + Two.Height < One.Y)
            return false;
        return true;
    }
    //public void collision()
    //{

    //    xStep *= -1;
    //    yStep *= -1;
    //}
    public void Draw(Graphics g)
    {
        g.DrawRectangle(new Pen(Color.Blue), myRectangle);

        myLocation.Top = myRectangle.Y -15;
        myLocation.Left = myRectangle.X+(myRectangle.Width/4);
        myLocation.Text = myRectangle.X.ToString() + ", " + myRectangle.Y.ToString();

    }

i start the timer on the buttone click, i will give you form1 我点击按钮启动计时器,我会给你form1

  public partial class Form1 : Form
{
    Random Randcolor = Box.randcolor;
    Random Rand = Box.rand;
    List<Box> Boxes = new List<Box>();

    public Form1()
    {
        InitializeComponent();
        this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
        this.SetStyle(ControlStyles.UserPaint, true);

    }

    private void button1_Click(object sender, EventArgs e)
    {
        // Box tmp = new Box(100, 100, 40);
        // Boxes.Add(tmp);
        for (int x = 1; x < 10; x++)
        {
            Boxes.Add(
                new Box(
                    Rand.Next(100 + x * 10, this.ClientSize.Width - 100),
                Rand.Next(100 + x * 10, this.ClientSize.Height - 100),
                Rand.Next(100 + x * 15), this));

            //Boxes.Add(new Box(100 + x * 10, 100 + x * 10, x * 15, this));

        }
        timer1.Enabled = true;
        timer2.Enabled = true;

    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        foreach (Box tmp in Boxes)
        {
            tmp.Draw(e.Graphics);

        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        listBox1.Items.Clear();
        foreach (Box tmp in Boxes)
        {
            tmp.Move();
            listBox1.Items.Add(tmp.X + ", " + tmp.Y);
        }
        this.Invalidate(false);


    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void timer2_Tick(object sender, EventArgs e)
    {
        foreach (Box tmp in Boxes)
        {
            tmp.Colorchange();
        }
    }

}

} }

Your ColorChange() method never really 'applies' the new color to the Rectangle object within your Box class. 您的ColorChange()方法从不真正将新颜色“应用”到Box类中的Rectangle对象。

Within your Box class you need a new Graphics object. Box类中,您需要一个新的Graphics对象。 You could set it as field or pass it as parameter for the constructor, as you are using it within the Draw() method anyways. 您可以将其设置为字段,也可以将其作为构造函数的参数传递,因为无论如何都在Draw()方法中使用它。

        private Graphics g;

        ...

        public Box(int x, int y, int s, Form pF)
        {
            g = pF.CreateGraphics();

            ...
        }

You will also have to modify your ColorChange() method, so that the Rectangle gets filled to the desired color: 您还必须修改ColorChange()方法,以使Rectangle填充为所需的颜色:

public Color Colorchange()
{
    this.Rectcolor = Color.FromArgb(randcolor.Next(256), randcolor.Next(256), randcolor.Next(256));

    // You need to fill the rectangle with the desired color again
    this.g.FillRectangle(new SolidBrush(this.Rectcolor), this.myRectangle);
    return Rectcolor;
}
g.DrawRectangle(new Pen(Color.Blue), myRectangle);

this line draws a blue rectangle. 这条线绘制了一个蓝色矩形。 Whatever color you've set to "myColor", it draws blue. 无论您将颜色设置为“ myColor”,它都会绘制蓝色。 Changing this to 更改为

g.DrawRectangle(new Pen(myColor), myRectangle);

should fix your issue. 应该解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM