且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检查是否两个控件在Windows窗体重叠

更新时间:2023-02-05 18:21:13

一个更好的方法是使用Rectangle.Bounds.IntersectsWith方法,由它来检查你,可以生产更清洁码。我个人不知道任何性能问题或利益,一种方式或其他,但我冒昧猜测,只是循环您的控制和检查他们,这将是比建设速度列出两者循环。

A much better approach is to use the Rectangle.Bounds.IntersectsWith method, which does the check for you and can produce cleaner code. I'm personally unaware of any performance issues or benefits, one way or the other, although I would venture a guess that simply looping over your controls and checking them with this would be faster than building lists and loops both.

Picturebox pic = new Picturebox();
foreach(Control picturebox in Form1){
   if (pic.Bounds.IntersectsWith(picturebox.Bounds))
   {
       //We have a problem, Houston, because we just collided!
   }
}



我希望这可以帮助,即使你问这个问题前一段时间。

I hope this helps, even though you asked this question some time ago.