C# - 调用绘制事件处理程序



我想在单击按钮时调用我的绘制事件。我有这个:

private void btnDrawMap_Click(object sender, EventArgs e)
        {
            rows = Convert.ToInt32(this.numRows);
            cols = Convert.ToInt32(this.numCols);

            defineCellWallsList();
            defineCellPositionsList();
            pictureBox1_Paint_1;
        }

我认为你不能这样称呼它。pictureBox1_Paint_1所做的只是:

private void pictureBox1_Paint_1(object sender, PaintEventArgs e)
        {
            myGameForm.drawMap(e, mapCellWallList, mapCellPositionList);
        }

因此,如果我能打电话给myGameForm.drawMap,那就好了。这是:

public void drawMap(PaintEventArgs e, List<List<int>> walls, List<List<int>> positions)
        {
            // Create a local version of the graphics object for the PictureBox.
            Graphics g = e.Graphics;
            // Loop through mapCellArray.
            for (int i = 0; i < walls.Count; i++)
            {
                int[] mapData = getMapData(i, walls, positions);
                int column = mapData[0];
                int row = mapData[1];
                int right = mapData[2];
                int bottom = mapData[3];
                g.DrawLine(setPen(right), column + squareSize, row, column + squareSize, row + squareSize);
                g.DrawLine(setPen(bottom), column + squareSize, row + squareSize, column, row + squareSize);
            }
            drawMapEdges(e, walls, positions);
        }

它需要一个paintEventArgbtnDrawMap_Click需要一个eventArg,所以我将btnDrawMap_Click eventArg参数更改为paintEventArg,但是它有一个错误,指出btnDrawMap_Click没有重载采用eventHandler的方法。

任何帮助表示赞赏,谢谢。

> 只需调用 Invalidate() ,这会强制PictureBox重新绘制自身,从而调用您的Paint事件:

pictureBox1.Invalidate();

最新更新