How to make a program check a picture box intersects with another control?

柔情痞子 提交于 2019-12-13 05:30:37

问题


I was wondering how I could check if a picturebox intersects with another thing on the form. I know for a rectangle its:

if (rectangle.IntersectsWith(otherRectangle))

but (i know, not possible) i want to do like the above:

if (pictureBox1.IntersectsWith(pictureBox2))

Anyone know a good way to check? Also, im making a game in which you can move the picture box 1 with arrows keys, and jump with space. Thanks!


回答1:


If two controls are children of the same form or container, then you can check whether the controls overlap by getting their Bounds and calling IntersectsWith:

if (pictureBox1.Bounds.IntersectsWith(pictureBox2.Bounds))

DisplayRectangle is the wrong property to access; for a PictureBox, it returns (0, 0, Width, Height), so IntersectsWith will always return true if Width and Height are nonzero.




回答2:


If two picture boxes are within the same parent then:

pictureBox1.DisplayRectangle.IntersectsWith(pictureBox2.DisplayRectangle)


来源:https://stackoverflow.com/questions/20746282/how-to-make-a-program-check-a-picture-box-intersects-with-another-control

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!