2016-11-11 97 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

太棒了!它像一個魅力!這是一個非常好的和詳細的答案。 –