PictureBox Grid 并在单击时选择单个单元格 [英] PictureBox Grid and selecting individual cells when clicked on

查看:31
本文介绍了PictureBox Grid 并在单击时选择单个单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用一个图片框和一个网格.我已经能够在图片框的顶部成功设置一个网格.现在我正在寻求帮助/指导来编写选择每个单元格的方法.如何选择单个单元格并以蓝色显示它们的颜色?这是对生命游戏的引用.

I am currently working with a pictureBox and a grid. I have been able to successfully set a grid on top of the pictureBox. Now I am seeking help/guidance in coding a way to select each individual cell. How can I select individual cells and display their color in blue? This is in reference to the Game of Life.

网格代码:

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

蓝色单元格是被点击的单元格:

Blue cells are the ones clicked on:

推荐答案

您需要添加一些结构来处理单元格,而不是处理paint方法中的所有内容.这是我拼凑的一个快速而肮脏的应用程序.它使用两个简单的类:Cell 和 Grid,它们包含一个二维的 Cell 数组.

You will want to add some structure to work with cells instead of handling everything in the paint method. Here is a quick and dirty app I threw together. It uses two simple classes: Cell and Grid which contains a 2D array of Cells.

这里要理解的主要内容是将鼠标位置映射到世界坐标的表单类中的 Grid.SetDimentions 方法和 Form1_MouseDown 事件.

The main thing to understand here is the Grid.SetDimentions method and Form1_MouseDown event in the form class which maps the mouse position to world coordinates.

细胞类:

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

这篇关于PictureBox Grid 并在单击时选择单个单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆