如何检查是否两个控件在Windows窗体重叠 [英] How to check if two controls are overlapping in Windows Forms

查看:230
本文介绍了如何检查是否两个控件在Windows窗体重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个类,允许用户拖动一个表格面板。我如何才能确保
用户并不把两个小组对彼此的顶部?如果他们这样做,我想移动/或突出的控制之一,而他们都重叠。

I have created a class that allows the user to drag panels on a forms. How can I ensure that the user does not place two panels on top of each other? If they do, I would like to shift/or highlight one of the control while they are both overlapped.

我试着在onmousedown事件的事件设置此,但没有相当的工作。

I tried setting this in OnMouseDown event but that didn't quite work.

另外,板的形式的数目取决于图像的形式需要显示的数目。每个小组都有面板内一个PictureBox。

Also, the number of panels on the form vary depending on the number of pictures the form needs to show. Each panel has a picturebox inside the panel.

推荐答案

一个更好的方法是使用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.

这篇关于如何检查是否两个控件在Windows窗体重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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