C#Winforms-当可移动绘制的图像位于标签上方时进行检查
我在表单上绘制了一个图像,可以通过箭头键移动。 它是在绘制方法中创建的,如下所示:C#Winforms-当可移动绘制的图像位于标签上方时进行检查,c#,winforms,C#,Winforms,我在表单上绘制了一个图像,可以通过箭头键移动。 它是在绘制方法中创建的,如下所示: int _x = 500; // initial x co-ordinate int _y = 600; // initial y co-ordinate private void testForm_Paint(object sender, PaintEventArgs e) { e.Graphics.DrawImage(new Bitmap("carImage.png"), _x, _y, 150,
int _x = 500; // initial x co-ordinate
int _y = 600; // initial y co-ordinate
private void testForm_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(new Bitmap("carImage.png"), _x, _y, 150, 100);
}
我在表单上也有一些标签,目的是当图像移动到标签上并按下空格时,标签上的文本会发生变化
我感到困惑的是,当按下空格键时,如何检查图像是否位于正确的位置,即图像是否位于标签顶部
我尝试了几种不同的方法,包括:
case Keys.Space:
foreach (Control control in this.Controls)
{
if (control is Label)
{
for (int i = _x; i < _x + 150; i++)
{
for (int j = _y; j > _y - 100; j--)
{
// If carImage within bounds of control
if (control.Bounds.Contains(i, j))
control.Text = "newText";
}
}
}
}
但我不确定图像应该是什么对象类型,以便将其用作方法参数
我尝试将图像设置为位图类型,但它没有.Bounds属性(只有GetBounds())。我想我可以使用carImage.Height等,但我仍然不确定如何使用这个来检查它们是否重叠
因此,有两个问题:
- 是否有其他方法检查图像坐标是否与标签边界重叠,如果没有重叠
- 将图像设置为什么类型,以便在control.Bounds.IntersectsWith()中调用它,以及如何准确地使该方法与图像边界一起工作(即括号中应该包含什么内容)
Bitmap _image = new Bitmap("carImage.png");
int _x = 500; // initial x co-ordinate
int _y = 600; // initial y co-ordinate
private void testForm_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(_image, _x, _y);
}
private void CheckIntersect()
{
Rectangle imageBounds = new Rectangle(_x, _y, _image.Width, _image.Height);
if (label1.Bounds.IntersectsWith(imageBounds))
control.Text = "newText";
}
试着这样做:
Bitmap _image = new Bitmap("carImage.png");
int _x = 500; // initial x co-ordinate
int _y = 600; // initial y co-ordinate
private void testForm_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(_image, _x, _y);
}
private void CheckIntersect()
{
Rectangle imageBounds = new Rectangle(_x, _y, _image.Width, _image.Height);
if (label1.Bounds.IntersectsWith(imageBounds))
control.Text = "newText";
}
矩形类具有IntersectWith函数。确定你的矩形并使用它。可能的解决方案:你应该能够确定它。你在哪里画这幅画?图像有多大<代码>新矩形(x,y,yourImage.Width,yourImage.Height)谢谢,看起来可能有用。但是,当我绘制图像时(在绘制方法中):e.Graphics.DrawImage(新位图(“picPlane.png”),x,y,150100);它没有被分配给变量。您知道如何将其分配给变量,或者在图像上使用IntersectsWith方法而不将其分配给变量吗?您需要存储该位置;这可以在WinForm级别完成。Rectangle类有一个IntersectWith函数。确定你的矩形并使用它。可能的解决方案:你应该能够确定它。你在哪里画这幅画?图像有多大<代码>新矩形(x,y,yourImage.Width,yourImage.Height)谢谢,看起来可能有用。但是,当我绘制图像时(在绘制方法中):e.Graphics.DrawImage(新位图(“picPlane.png”),x,y,150100);它没有被分配给变量。您知道如何将其分配给变量,或者在图像上使用IntersectsWith方法而不将其分配给变量吗?您需要存储该位置;这可以在WinForm级别上完成。