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

时间:2022-11-25 14:46:08

I am creating a Candy Crush styled game and I am stuck at a point where I have made click events for each picturebox array (which loads images from resource files) and I cannot figure out what code to use to click on the boxes. I cannot use e.X or e.Y because it is not MouseEventArgs, and can't change it to that either because then the method won't link back to the picturebox array. I just want to store the picture I clicked into a variable such as:d = picturebox[e.X,e.Y].Image;.

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

Here is my code:

这是我的代码:

 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 个解决方案

#1


1  

If you handle the MouseClick event instead, you can use MouseEventArgs:

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

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

and then:

接着:

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

#1


1  

If you handle the MouseClick event instead, you can use MouseEventArgs:

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

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

and then:

接着:

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