问题
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