2016-11-11 103 views
0

我创造了这样的图片框网格:添加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); 
      } 
     } 

,这是它的外观:
enter image description here

我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); 
} 

我如何结合这两个?

+1

你可以在你的PictureBoxPaint()方法提高自己的CellPaint事件。在创建自己的控件类时,趋于最佳工作,只需将PictureBox设置为基类并重写OnPaint()。还要考虑到会有很多事件处理程序调用,也许你不应该放弃选择绘制位图的选项。 –

+0

如何将位图绑定到列表?每当列表发生变化时,我都必须在picturebox中重新绘制位图,在我的情况下,它可能会有些滞后(我正在制作一种Tetris游戏) –

回答

1

除非你的照片箱很大,我不认为这会是滞后的。 PBox是双缓冲的,应该可以正常工作。

试想一下:当你CellPaint事件看起来小果然是呼吁每个细胞每个时间,让你的TLP的整个表面被重绘每个一次无效它。所以:与PBox的情况没有太大区别..

以下是您自己的PaintCellPaint的示例。它使用一个简单的2d-Color数组和两个ints来存储当前单元格大小。 重新计算调整大小PictureBox董事会!

Color[,] cellList; 
int cellWidth = 23; 
int cellHeight = 23; 

private void pictureBox1_Paint(object sender, PaintEventArgs e) 
{ 
    if (cellList == null) InitCells(22, 22); 


    for (int c = 0; c < cellList.GetLength(0); c++) 
     for (int r = 0; r < cellList.GetLength(1); r++) 
     { 
      TableLayoutCellPaintEventArgs tep = new 
       TableLayoutCellPaintEventArgs(e.Graphics, 
        Rectangle.Round(e.Graphics.ClipBounds), 
        new Rectangle(c*cellWidth, r*cellHeight, cellWidth, cellHeight), 
        c, r); 
      pictureBox1_CellPaint(e, tep); 
     } 
    // insert the gridline drawing here: 
    for (int c = 0; c <= cellList.GetLength(1); c++) 
      e.Graphics.DrawLine(Pens.DarkSlateBlue, 0, c * cellHeight, 
           cellWidth * cellList.GetLength(0), c * cellHeight); 
    for (int c = 0; c <= cellList.GetLength(0); c++) 
      e.Graphics.DrawLine(Pens.DarkSlateBlue, c * cellWidth, 0, 
           c * cellWidth, cellHeight * cellList.GetLength(1)); 
} 

private void pictureBox1_CellPaint(object sender, TableLayoutCellPaintEventArgs e) 
{ 
    //if (mainVisualization.mainGrid != null) 
    // if (mainVisualization.mainGrid.cellList != null) 
      using (var b = new SolidBrush(cellList[e.Column, e.Row])) 
       e.Graphics.FillRectangle(b, e.CellBounds); 
} 

你可以看到,它是你曾经为TLP代码的直接克隆。不知道如果你真的应该做到这一点,但它是一个例子,你如何能够模拟CellPaint事件..

当然,你会希望使用自己cellList数据结构..

它是由给你决定如何整合网格线的绘图。最简单的方法是在cellPaint循环之后绘制它们。

通过重新计算的单元尺寸(和InvalidatingPictureBox),它会很好地resize内容:

enter image description here

+0

太棒了!它像一个魅力!这是一个非常好的和详细的答案。 –