2016-11-11 1 views
0

그 위에 페인트 :그리드와의 PictureBox에 CellPaint 이벤트를 추가 내가이 같이있는 PictureBox에 격자 생성

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

비트 맵을 목록에 어떻게 바인딩 할 수 있습니까? 목록이 변경 될 때마다 픽처 박스의 비트 맵을 다시 그려야 할 것입니다. 제 경우에는 아마 느려질 것입니다 (테트리스 게임을 만들고 있습니다). –

답변

1

당신의 picturebox가 거대하지 않으면 나는 그것이 뒤떨어 질 것이라고 생각하지 않습니다. PBox는 이중 버퍼이며 잘 작동합니다.

이를 고려하여 CellPaint 이벤트 보이는 동안 정말 세포 시간이라고, 그래서 당신의 TLP의 전체 표면이 당신 에게 그것을 무효화 각 시간을 다시 그려야되는 작은. 그래서 : PBox의 상황과 큰 차이는 없습니다.

다음은 자신의 PaintCellPaint의 예입니다. 간단한 2 차원 배열과 두 개의 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에 대해서 가지고 있던 코드를 직접 복제 것을 볼 수 있습니다. 확실하지 만약 당신이 정말로 해야 그렇게, 그러나 당신이 당신이 당신의 자신의 cellList 데이터 구조를 사용하는 것이 좋습니다 물론

이 ..

이가있는 CellPaint 이벤트 ..을 시뮬레이션 할 수있는 방법을 예입니다 가장 간단한 방법은 cellPaint 루프 이후에 드로잉하는 것입니다.

셀 크기를 재 계산하여

(그리고 PictureBoxInvalidating) 멋지게 내용을 resize됩니다

enter image description here

+0

굉장합니다! 그것은 매력처럼 작동합니다! 이것은 매우 훌륭하고 상세한 답변입니다. –

관련 문제