Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我能';我不明白为什么我的图像不会重新绘制_C#_Windows_Winforms_Paint_Picturebox - Fatal编程技术网

C# 我能';我不明白为什么我的图像不会重新绘制

C# 我能';我不明白为什么我的图像不会重新绘制,c#,windows,winforms,paint,picturebox,C#,Windows,Winforms,Paint,Picturebox,我的图像确实是移动的,但无论我做什么,我都无法得到它,所以图像重新绘制!gamemanager绘制猫,form1将绘制参数和对象传递给gamemanager 表格1: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using Syst

我的图像确实是移动的,但无论我做什么,我都无法得到它,所以图像重新绘制!gamemanager绘制猫,form1将绘制参数和对象传递给gamemanager

表格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();
        }
    }
}
类别:


不管我怎么做,它都不会重新油漆

再次检查计时器是否已启用,PictureBox.Paint事件是否已触发(不要害怕使用调试器)。如果没有帮助,试着不要使用
PictureBox.Paint
事件和
Refresh
方法,而是直接在计时器中调用
myGM.Paint
。勾选处理程序

这在
PictureBoxes
和其他
控件中一直对我有效

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


虽然我记不起我必须添加哪个
time.Tick+=neweventhandler(time\u Tick)
到GameManager构造函数,显然这就是计时器如何连接到time_tick方法。

LOL,抱歉。我今天一整天都在为学校做这件事。我几乎可以自己做其他的事情。我就是想不出这个奇怪的举动。对不起,如果我要求太多的话。如果你愿意,我可以离开……我只是在开玩笑:)那么你真的把这个修好了吗?……是的,结果证明我的计时器时间与时间滴答法没有联系,所以没有任何东西会移动或绘制。我如何在计时器中调用myGM.paint。勾选myGM.paint是否需要图形g?当您的PictureBox第一次出现在窗体上时,您可以从事件中抓取图形对象,然后根据您的喜好使用此对象,什么?我该怎么做?到目前为止,我只知道我需要把myGM.paint()放在;在Timer.tick中,我很抱歉我可能看起来很愚蠢,我只是不明白。你不能像在课堂参考中那样坚持
e.Graphics
。该图形由框架管理,并自动处理。仅在该Paint()事件的生存期内使用它。可以把它传给其他潜艇,但你不应该保留对它的引用或自己处理它。。。
PictureBox.Refresh();
PictureBox.Update();
PictureBox.Update();
PictureBox.Refresh();