试图找到鼠标的位置,以便我可以获得图片框数组的坐标
Trying to find location of mouse so I can get the coordinates of the picturebox array
我正在创建一个 Candy Crush 风格的游戏,我被困在为每个图片框数组(从资源文件加载图像)创建点击事件的时候,我无法弄清楚要使用什么代码来点击那些盒子。我不能使用 e.X 或 e.Y,因为它不是 MouseEventArgs
,也不能将其更改为那个,因为这样方法就不会 link 返回到图片框数组.我只想把我点击的图片存入一个变量如: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();
}
如果您改为处理 MouseClick
事件,则可以使用 MouseEventArgs
:
picturebox[i, j].MouseClick += picturebox_MouseClick;
然后:
void picturebox_MouseClick(object sender, MouseEventArgs e)
{
// Get the location with e.Location
}
我正在创建一个 Candy Crush 风格的游戏,我被困在为每个图片框数组(从资源文件加载图像)创建点击事件的时候,我无法弄清楚要使用什么代码来点击那些盒子。我不能使用 e.X 或 e.Y,因为它不是 MouseEventArgs
,也不能将其更改为那个,因为这样方法就不会 link 返回到图片框数组.我只想把我点击的图片存入一个变量如: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();
}
如果您改为处理 MouseClick
事件,则可以使用 MouseEventArgs
:
picturebox[i, j].MouseClick += picturebox_MouseClick;
然后:
void picturebox_MouseClick(object sender, MouseEventArgs e)
{
// Get the location with e.Location
}