2012-10-03 2 views
2

현재 pictureBox와 그리드를 사용하고 있습니다. pictureBox 위에 그리드를 성공적으로 설정할 수있었습니다. 이제 각 개별 셀을 선택하는 방법을 코딩하는 데 도움/지침을 찾고 있습니다. 개별 셀을 선택하고 파란색으로 표시하려면 어떻게합니까? 이것은 Game of Life와 관련되어 있습니다. 그리드에 대한PictureBox 그리드 및 클릭했을 때 개별 셀 선택

코드 :

private void pictureBox1_Paint(object sender, PaintEventArgs e) 
     { 
      Graphics g = e.Graphics; 
      int numOfCells = 200; 
      int cellSize = 10; 
      Pen p = new Pen(Color.Black); 

      for (int i = 0; i < numOfCells; i++) 
      { 
       // Vertical 
       g.DrawLine(p, i * cellSize, 0, i * cellSize, numOfCells * cellSize); 
       // Horizontal 
       g.DrawLine(p, 0, i * cellSize, numOfCells * cellSize, i * cellSize); 
      } 
     } 

블루 세포에 클릭 한 것들이다 :

enter image description here

답변

0

대신 모든 것을 처리 세포와 함께 작동하도록 몇 가지 구조를 추가 할 것 페인트 방법. 여기에 함께 던진 빠르고 더러운 앱이 있습니다. 두 개의 간단한 클래스를 사용합니다 : Cell과 2D 셀의 배열을 포함하는 Grid.

여기에서 가장 중요한 것은 마우스 위치를 세계 좌표로 매핑하는 Form 클래스의 Grid.SetDimentions 메서드 및 Form1_MouseDown 이벤트입니다.

셀 (The Cell) 클래스 :

using System.Drawing; 

public sealed class Cell 
{ 
    public bool Hover { get; set; } 
    public enum CellState { Inactive, Intermediate, Active } 
    public CellState State { get; private set; } 
    public int Column { get; private set; } 
    public int Row { get; private set; } 
    public Rectangle Bounds { get; private set; } 

    public Cell (int column, int row, CellState state = CellState.Inactive) 
    { 
     this.Column = column; 
     this.Row = row; 
    } 

    public void SetDimensions (Rectangle bounds) 
    { 
     this.Bounds = bounds; 
    } 

    public void RotateState() 
    { 
     switch (this.State) 
     { 
      case CellState.Active: { this.State = CellState.Inactive; break; } 
      case CellState.Inactive: { this.State = CellState.Intermediate; break; } 
      case CellState.Intermediate: { this.State = CellState.Active; break; } 
     } 
    } 
} 

그리드 클래스 :

using System.Drawing; 

public class Grid 
{ 
    public Cell [,] Cells { get; private set; } 
    public Size Size { get; private set; } 
    public Rectangle Bounds { get; private set; } 

    public Grid (Size gridSize) 
    { 
     this.Size = gridSize; 
     this.Cells = new Cell [this.Size.Width, this.Size.Height]; 

     for (int r=0; r < this.Size.Height; r++) 
     { 
      for (int c=0; c < this.Size.Width; c++) 
      { 
       this.Cells [c, r] = new Cell(c, r); 
      } 
     } 
    } 

    public void SetDimensions (Rectangle bounds) 
    { 
     Size size; 

     this.Bounds = bounds; 
     size = new Size((int) (((float) bounds.Width)/((float) this.Size.Width)), (int) (((float) bounds.Height)/((float) this.Size.Height))); 

     for (int r=0; r < this.Size.Height; r++) 
     { 
      for (int c=0; c < this.Size.Width; c++) 
      { 
       this.Cells [c, r].SetDimensions(new Rectangle(bounds.Left + (size.Width * c), bounds.Top + (size.Height * r), size.Width, size.Height)); 
      } 
     } 
    } 
} 

폼 :

using System; 
using System.Drawing; 
using System.Windows.Forms; 

public partial class Form1: Form 
{ 
    private Grid World { get; set; } 

    public Form1() 
    { 
     this.InitializeComponent(); 

     this.World = new Grid(new Size(50, 50)); 
     this.World.SetDimensions(new Rectangle(0, 0, this.pictureBox1.ClientSize.Width, this.pictureBox1.ClientSize.Height)); 
    } 

    private void Form1_Shown (object sender, EventArgs e) 
    { 
     this.pictureBox1.Invalidate(); 
    } 

    private void Form1_Resize (object sender, EventArgs e) 
    { 
     this.World.SetDimensions(new Rectangle(0, 0, this.pictureBox1.ClientSize.Width, this.pictureBox1.ClientSize.Height)); 

     this.pictureBox1.Invalidate(); 
    } 

    private void pictureBox1_MouseMove (object sender, MouseEventArgs e) 
    { 
     for (int r=0; r < this.World.Size.Height; r++) 
     { 
      for (int c=0; c < this.World.Size.Width; c++) 
      { 
       this.World.Cells [c, r].Hover = this.World.Cells [c, r].Bounds.Contains(e.X, e.Y); 
      } 
     } 

     this.pictureBox1.Invalidate(); 
    } 

    private void pictureBox1_MouseClick (object sender, MouseEventArgs e) 
    { 
     for (int r=0; r < this.World.Size.Height; r++) 
     { 
      for (int c=0; c < this.World.Size.Width; c++) 
      { 
       if (this.World.Cells [c, r].Bounds.Contains(e.X, e.Y)) 
       { 
        this.World.Cells [c, r].RotateState(); 
       } 
      } 
     } 
    } 

    private void pictureBox1_Paint (object sender, PaintEventArgs e) 
    { 
     for (int r=0; r < this.World.Size.Height; r++) 
     { 
      for (int c=0; c < this.World.Size.Width; c++) 
      { 
       if (this.World.Cells [c, r].State == Cell.CellState.Active) 
       { 
        e.Graphics.FillRectangle(System.Drawing.Brushes.Blue, this.World.Cells [c, r].Bounds); 
       } 
       else if (this.World.Cells [c, r].State == Cell.CellState.Inactive) 
       { 
        e.Graphics.FillRectangle(System.Drawing.Brushes.White, this.World.Cells [c, r].Bounds); 
       } 
       else if (this.World.Cells [c, r].State == Cell.CellState.Intermediate) 
       { 
        e.Graphics.FillRectangle(System.Drawing.Brushes.Gray, this.World.Cells [c, r].Bounds); 
       } 

       if (this.World.Cells [c, r].Hover) 
       { 
        e.Graphics.DrawRectangle(System.Drawing.Pens.Red, this.World.Cells [c, r].Bounds); 
       } 
       else 
       { 
        e.Graphics.DrawRectangle(System.Drawing.Pens.Black, this.World.Cells [c, r].Bounds); 
       } 
      } 
     } 
    } 
}