C#PictureBox和Timer不能一起工作

时间:2014-05-14 09:17:45

标签: c# timer drawing picturebox

所以我试图在C#中使用PictureBox进行简单的游戏绘画和更新功能的计时器,但我注意到当我开始在我的图片框上绘画时,我的计时器停止工作..我不知道为什么..

以下是图片框代码:

private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        Console.WriteLine("PicBox running!");
        if (this.MyGame.InGame)
        {
            this.pictureBox1.Refresh();
            e.Graphics.Clear(Color.White);               
            MyGame.CurrentMap.Draw(e.Graphics);
            //e.Graphics.Dispose(); <- if I uncomment this, then the whole programm just freezes
        }
    }

这是计时器代码:

private void timer1_Tick(object sender, EventArgs e)
    {
        Console.WriteLine("Timer running!");
        if (this.MyGame.InGame)
        {
            MyGame.CurrentMap.Update();
            MyGame.UpdateTime();                
        }
    }

这是MyGame.CurrentMap.Draw(e.Graphics)调用的方法; :

public void Draw(Graphics g)
    {
        foreach(Planet item in Entities){
            item.Draw(g);
        }            
    }

任何帮助都会受到高度赞赏。我来自javascript,所以我真的不知道我在这里做了什么非常错误。

1 个答案:

答案 0 :(得分:0)

当发生计时器滴答时,您想重新绘制PictureBox。为此,您应该使它成为PictureBox接收Paint事件。这就是Refresh()所做的,所以调用this.pictureBox1.Refresh();在timer1_Tick结束时。

Paint事件包含对Refresh的调用没有意义,因为这反过来会生成一个Paint事件。

相关问题