试图找到鼠标的位置,这样我就可以得到picturebox数组的坐标

时间:2016-01-15 18:59:53

标签: c# arrays mouseevent picturebox eventargs

我正在创建一个Candy Crush风格的游戏,我陷入了一个点,我为每个图片盒阵列(从资源文件加载图像)制作点击事件,我无法弄清楚用什么代码来点击框。我不能使用e.X或e.Y,因为它不是MouseEventArgs,也不能将其更改为,因为那时该方法不会链接回picturebox数组。我只想将我点击的图片存储到变量中,例如:d = picturebox[e.X,e.Y].Image;

这是我的代码:

 public partial class Form1 : Form
{

    Random R = new Random();
    int Random;
    PictureBox[,] picturebox = new PictureBox[8,8];


public Form1()
{
    InitializeComponent();

    for (int i = 0; i < 8; i++)
    {
        for (int j = 0; j < 8; j++)
        {
            Random = R.Next(0, 6);
            if (Random == 0)
            {
                picturebox[i, j] = new PictureBox();
                picturebox[i, j].Image = Resource.bluetile;
                //picturebox[i, j].Visible = false;
                picturebox[i, j].Location = new Point(i + (i * 38), j + (j * 38));
                picturebox[i, j].Size = new Size(40, 40);
                picturebox[i, j].BackColor = Color.Transparent;

                this.Controls.Add(picturebox[i, j]);
            }
            if (Random == 1)
            {
                picturebox[i, j] = new PictureBox();
                picturebox[i, j].Image = Resource.greentile;
                //picturebox[i, j].Visible = false;
                picturebox[i, j].Location = new Point(i + (i * 38), j + (j * 38));

                picturebox[i, j].Size = new Size(40, 40);
                picturebox[i, j].BackColor = Color.Transparent;

                this.Controls.Add(picturebox[i, j]);
            }
            if (Random == 2)
            {
                picturebox[i, j] = new PictureBox();
                picturebox[i, j].Image = Resource.orangetile;
                //picturebox[i, j].Visible = false;
                picturebox[i, j].Location = new Point(i + (i * 38), j + (j * 38));
                picturebox[i, j].Size = new Size(40, 40);
                picturebox[i, j].BackColor = Color.Transparent;

                this.Controls.Add(picturebox[i, j]);
            }
            if (Random == 3)
            {
                picturebox[i, j] = new PictureBox();
                picturebox[i, j].Image = Resource.purpletile;
                //picturebox[i, j].Visible = false;
                picturebox[i, j].Location = new Point(i + (i * 38), j + (j * 38));
                picturebox[i, j].Size = new Size(40, 40);
                picturebox[i, j].BackColor = Color.Transparent;

                this.Controls.Add(picturebox[i, j]);
            }
            if (Random == 4)
            {
                picturebox[i, j] = new PictureBox();
                picturebox[i, j].Image = Resource.redtile;
                //picturebox[i, j].Visible = false;
                picturebox[i, j].Location = new Point(i + (i * 38), j + (j * 38));
                picturebox[i, j].Size = new Size(40, 40);
                picturebox[i, j].BackColor = Color.Transparent;

                this.Controls.Add(picturebox[i, j]);
            }
            if (Random == 5)
            {
                picturebox[i, j] = new PictureBox();
                picturebox[i, j].Image = Resource.yellowtile;
                // picturebox[i, j].Visible = false;
                picturebox[i, j].Location = new Point(i + (i * 38), j + (j * 38));
                picturebox[i, j].Size = new Size(40, 40);
                picturebox[i, j].BackColor = Color.Transparent;
                picturebox[i, j].Tag = "0";

                this.Controls.Add(picturebox[i, j]);
            }

            picturebox[i, j].Click += picturebox_Click;
        }
    }

    this.Refresh();
}


void picturebox_Click(object sender, EventArgs e)
{
    PictureBox temp = sender as PictureBox;

    Image tem;
    Image d;

    // d = picturebox[e.X,e.Y].Image;
    this.Refresh();
}

1 个答案:

答案 0 :(得分:1)

如果您处理MouseClick事件,则可以使用MouseEventArgs

picturebox[i, j].MouseClick += picturebox_MouseClick;

然后:

void picturebox_MouseClick(object sender, MouseEventArgs e)
{
    // Get the location with e.Location
}