C#Picturebox到Picturebox的交互 [英] C# Picturebox to picturebox interaction

查看:70
本文介绍了C#Picturebox到Picturebox的交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我吗?我想制作的是这样,如果一个pictuebox碰到另一个picrebox,则将发生某些事情,例如我正在使用

Can anyone please help me? I''m trying to make is so that if a pictuebox hits another picrebox something will happen for example i''m using

if (e.KeyCode == Keys.Up)
{
    You.Location = new Point(You.Location.X, You.Location.Y - 5);
}



然后图片框会上升,当它碰到该区域中的另一个图片框时,它会执行某些操作,

我的主意是



and the picturebox will go up and when it hits another picurebox that is in the area it will do something,

my idea was

if(picturebox1.location == picruebox2.location)
{
}



它会做某事,但是它必须要击中一个确切的像素,但这很难,我想要,所以如果您在移动时击中picturebbox,它会做某事,有人可以帮忙吗?



and it will do something, but it will have to hit a exact pixel, but that is very hard, i want it so if you hit the picturebbox while moving it will do something, can anyone help?

推荐答案

您需要查看 Rectangle.Contains [ ^ ]方法.如果将点向上移动,请使用移动PictureBox的TLHC;如果向下,则使用BLHC. (显然,如果它向侧面移动,请在此处调整左"和右").

在这两种情况下,请使用固定控件中的矩形.

[edit]糟糕!给您错误的链接:应该是到Contains(Point)而不是Contains(Rectangle).已更正-OriginalGriff [/edit]


我听不懂"

好吧,我会再解释一下:
每个图片框都有一个矩形,该矩形由它的位置(左上角或TLHC)以及它的Width和Height属性组成.

每个矩形都有一个名为Contains的方法,该方法有一个重载Point的方法.

如果将Point作为移动PictureBox的TLHC调用Rectangle.Contains方法,则如果移动PictureBox的TLHC在固定矩形的矩形内,则布尔结果为true.

您可以通过控件的Location属性访问控件的TLHC,并通过Bounds属性访问矩形的控件.

所以:
You need to look at the Rectangle.Contains[^] method. If you are moving the point up, use the TLHC of your moving PictureBox, if down, then use the BLHC. (obviously, if it moves sideways adjust the Left and Right there).

In both cases, use the rectangle from the stationary control.

[edit]Oops! Gave you the wrong link: should have been to Contains(Point) instead of Contains(Rectangle). Corrected - OriginalGriff[/edit]


"I don''t understand"

OK, I''ll try to explain a little more:
Each Picture box has a Rectangle, which consists of it''s Location (the Top Left Hand Corner or TLHC) together with it''s Width and Height properties.

Each Rectangle has a method called Contains which has an overload taking a Point.

If you call the Rectangle.Contains Method with the Point as the TLHC of your moving PictureBox, the bool result will be true if the TLHC of your moving PictureBox is inside the rectangle of the stationary one.

You can access the TLHC of a control via it''s Location property, and the Rectangle via the Bounds property.

So:
movingPictureBox.Top -= 20;
if (stationaryPictureBox.Bounds.Contains(movingPictureBox.Location))
    {
    MessageBox.Show("Inside");
    }

是否更有意义?


这篇关于C#Picturebox到Picturebox的交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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