我创造了这样的图片框网格:添加CellPaint事件图片框与网格在上面画了
private void PictureBoxPaint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
int numOfCellsWidth = 50;
int numOfCellsHeight = 600;
int cellSize = 20;
Pen p = new Pen(Color.Black);
for (int y = 0; y < numOfCellsHeight; ++y)
{
g.DrawLine(p, 0, y * cellSize, numOfCellsHeight * cellSize, y * cellSize);
}
for (int x = 0; x < numOfCellsWidth; ++x)
{
g.DrawLine(p, x * cellSize, 0, x * cellSize, numOfCellsHeight * cellSize);
}
}
我TableLayoutPanel中的工作前,它有一个CellPaint事件,我可以将其绑定到数组列表,以便在列表更改时单元格的颜色会发生变化。这是我有:
private void tableLayoutPanelMainGrid_CellPaint(object sender, TableLayoutCellPaintEventArgs e)
{
if (mainVisualization.mainGrid != null)
if (mainVisualization.mainGrid.cellList != null)
using (var b = new SolidBrush(mainVisualization.mainGrid.cellList[e.Column, e.Row].color))
e.Graphics.FillRectangle(b, e.CellBounds);
}
我如何结合这两个?
你可以在你的PictureBoxPaint()方法提高自己的CellPaint事件。在创建自己的控件类时,趋于最佳工作,只需将PictureBox设置为基类并重写OnPaint()。还要考虑到会有很多事件处理程序调用,也许你不应该放弃选择绘制位图的选项。 –
如何将位图绑定到列表?每当列表发生变化时,我都必须在picturebox中重新绘制位图,在我的情况下,它可能会有些滞后(我正在制作一种Tetris游戏) –