【问题标题】:I can't figure out why my image will not repaint我无法弄清楚为什么我的图像不会重新绘制
【发布时间】:2013-10-08 00:01:57
【问题描述】:

我的图像确实在移动,但无论我做什么,我都无法得到它,所以图像会重新绘制! GameManger 绘制猫,form1 将绘制参数和对象传递给游戏管理器。

表格1:

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

namespace CatAndMouse
{
    public partial class Form1 : Form
    {
        GameManager myGM = new GameManager();
        int dir = 0;
        public Form1()
        {
            InitializeComponent();
            newGame();
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (this.myGM != null)
                this.myGM.paint(e.Graphics);
            //e.Graphics.DrawImage(imgMouse.Images[0], pointXMouse, pointYMouse);
            //e.Graphics.DrawImage(imgCat.Images[0], 50, 100);
            //e.Graphics.DrawImage(imgCheese.Images[0], 75, 100);
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Up)
            {
                dir = 0;
            }
            if (e.KeyCode == Keys.Right)
            {
                dir = 1;
            }
            if (e.KeyCode == Keys.Down)
            {
                dir = 2;
            }
            if (e.KeyCode == Keys.Left)
            {
                dir = 3;
            }
        }
        public void newGame()
        {
            timer1.Start();
            myGM.newGame(imgCat, imgMouse, imgCheese, this);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            pictureBox1.Refresh();
        }
        public int getDir()
        {
            return dir;
        }
    }
}

游戏管理器:

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

namespace CatAndMouse
{
    class GameManager
    {
        Form1 myForm;
        Cat ca1 = new Cat();
        Mouse m = new Mouse();
        Cheese ch = new Cheese();
        int amount = 5;
        int catdir = 0;
        Timer time = new Timer();
        public ImageList imgCat = new ImageList();
        public ImageList imgMouse = new ImageList();
        public ImageList imgCheese = new ImageList();

        public void newGame(ImageList cat, ImageList mouse, ImageList cheese, Form1 form)
        {
            imgCat = cat;
            imgMouse = mouse;
            imgCheese = cheese;
            myForm = form;
            time.Start();
        }

        public void move()
        {
            ca1.Move(amount, catdir);
            m.Move(amount, catdir);
        }

        public void paint(Graphics g)
        {
            g.DrawImage(imgCat.Images[0], ca1.getLocation());
        }

        private void time_Tick(object sender, EventArgs e)
        {
            move();
            getDir();
            myForm.Refresh();
        }
        public void getDir()
        {
            catdir = myForm.getDir();
        }
    }
}

猫:

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

namespace CatAndMouse
{
    class Cat: Moveable
    {
        Random myCLoc = new Random();
        private Moveable myCatMove;
        public Point p = new Point(100, 100);
        int dir = 0;

        public void Move(int n, int d)
        {
            dir = d;
            if (dir == 0)
            {
                p.Y = p.Y - n;
            }
            if (dir == 1)
            {
                p.X = p.X + n;
            }
            if (dir == 2)
            {
                p.Y = p.Y + n;
            }
            if (dir == 3)
            {
                p.X = p.X - n;
            }
        }
        public void changeDirection()
        {
        }

        public Point getLocation()
        {
            return p;
        }

        public void paint(PaintEventArgs e)
        {

        }
    }
}

无论我做什么,它都不会重画!

【问题讨论】:

  • 大声笑,对不起。今天为了上学,我整天都在做这件事。我几乎可以自己做其他所有事情。我就是想不通这个该死的举动油漆废话。
  • 对不起,如果我问的太多了。如果你愿意,我可以离开……
  • 我只是在开玩笑:)
  • 那么你真的解决了这个问题吗?...
  • 是的,原来我的定时器时间没有链接到time_tick方法,所以什么都不会移动或绘画。

标签: c# windows winforms paint picturebox


【解决方案1】:

仔细检查是否启用了计时器并触发了 PictureBox.Paint 事件(不要害怕使用调试器)。如果没有帮助尽量不要使用PictureBox.Paint事件和Refresh方法,而是直接在Timer.Tick处理程序中调用myGM.paint

【讨论】:

  • 如果 myGM.paint 需要图形 g,我如何在 TImer.Tick 中调用 myGM.paint?
  • 您可以在您的PictureBox第一次出现在窗体上时,从事件中抓取图形对象,然后随意使用该对象
  • 等等,什么?我该怎么做?到目前为止,我所了解的是我需要把 myGM.paint();在 Timer.Tick 中
  • 对不起,我可能看起来很愚蠢,我只是不明白。
  • 你不能像在类级别参考中那样坚持e.Graphics。该图形由框架管理,并自动处理。仅在该 Paint() 事件的生命周期内使用它。没关系。将它传递给其他潜艇,但你不应该保留对它的引用或自己处置它......
【解决方案2】:

这在PictureBoxes 和其他Controls 中一直对我有用

PictureBox.Refresh();
PictureBox.Update();

PictureBox.Update();
PictureBox.Refresh();

虽然我不记得是哪个

【讨论】:

    【解决方案3】:

    我必须将time.Tick +=new EventHandler(time_Tick); 添加到 GameManager 构造函数中,显然这就是定时器如何连接 time_tick 方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多