C# - 调用paint事件处理程序

时间:2013-09-12 03:54:30

标签: c# event-handling

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

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的重载方法。

任何帮助表示感谢,谢谢。

1 个答案:

答案 0 :(得分:4)

只需致电Invalidate(),即强制PictureBox重绘自己,从而调用Paint事件:

pictureBox1.Invalidate();