Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Sadly, WinForms does not like to have too many controls, especially when you get into the hundreds. You can never have each control paint at the same time since each control will send its own paint message to windows.</p> <p>I think the best way to approach a game board like MineSweeper is to just use one control and draw the grid of buttons.</p> <p>Simple Mine class:</p> <pre><code>public class Mine { public Rectangle Bounds { get; set; } public bool IsBomb { get; set; } public bool IsRevealed { get; set; } } </code></pre> <p>Then create a new control where you inherit from the Panel control and set the DoubleBuffered property to prevent any flicker:</p> <pre><code>public class MineSweeperControl : Panel { private int columns = 16; private int rows = 12; private Mine[,] mines; public MineSweeperControl() { this.DoubleBuffered = true; this.ResizeRedraw = true; // initialize mine field: mines = new Mine[columns, rows]; for (int y = 0; y &lt; rows; ++y) { for (int x = 0; x &lt; columns; ++x) { mines[x, y] = new Mine(); } } } // adjust each column and row to fit entire client area: protected override void OnResize(EventArgs e) { int top = 0; for (int y = 0; y &lt; rows; ++y) { int left = 0; int height = (this.ClientSize.Height - top) / (rows - y); for (int x = 0; x &lt; columns; ++x) { int width = (this.ClientSize.Width - left) / (columns - x); mines[x, y].Bounds = new Rectangle(left, top, width, height); left += width; } top += height; } base.OnResize(e); } protected override void OnPaint(PaintEventArgs e) { e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; for (int y = 0; y &lt; rows; ++y) { for (int x = 0; x &lt; columns; ++x) { if (mines[x, y].IsRevealed) { e.Graphics.FillRectangle(Brushes.DarkGray, mines[x, y].Bounds); } else { ControlPaint.DrawButton(e.Graphics, mines[x, y].Bounds, ButtonState.Normal); } } } base.OnPaint(e); } // determine which button the user pressed: protected override void OnMouseDown(MouseEventArgs e) { for (int y = 0; y &lt; rows; ++y) { for (int x = 0; x &lt; columns; ++x) { if (mines[x, y].Bounds.Contains(e.Location)) { mines[x, y].IsRevealed = true; this.Invalidate(); MessageBox.Show( string.Format("You pressed on button ({0}, {1})", x.ToString(), y.ToString()) ); } } } base.OnMouseDown(e); } } </code></pre> <p><img src="https://i.stack.imgur.com/pz1dB.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload