如何检测多个控件的重叠状态 [英] how to detect the overlap status for multiple controls

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

问题描述

我有一个窗口窗体,它包含几个控件(类型是面板),
控件可能会彼此重叠,当我单击其中一个控件时,
我将检查在此单击的控件上有多少控件重叠.
我的问题是如何以这种方式检测?

I have a window form, and it includes several controls (type is panel),
the controls may overlap for each other, and when I click one of the controls,
I would check how many controls are overlapping on this clicked control.
My question is how to detect in this way ? Thanks of your help.

推荐答案

每个控件都具有TopLeftWidthHeight属性.当用户选择一个控件时,您将需要确定这些属性,然后循环浏览所有其他控件以检查是否存在相交.您可以在窗体的控件上使用foreach循环,如下所示:-
Each control has a Top, Left, Width and Height property. When a user selects a control, you will need to determine these properties, and then cycle through all the other controls to check if there is an intersection. You can use a foreach loop on the form''s controls, something like this:-
Rectangle sr = new Rectangle(panel1.Left, panel1.Top, panel1.Width, panel1.Height);
foreach (Control ctrl in this.Controls)
{
	if (ctrl is Panel && ctrl != panel1)
	{
		Rectangle r = new Rectangle(ctrl.Left, ctrl.Top, ctrl.Width, ctrl.Height);
		if (sr.IntersectsWith(r))
		{
			//do your stuff
		}
	}
}


我只是以panel1为例.您应该替换您选择的面板.


I have just used panel1 as an example. You should substitute for your selected panel.


嗨体育,

这很容易做到,但是通常控件的重叠是布局设计错误的结果.如果要避免重叠,则不应浪费时间进行检测.正确的设计将不允许此类事情.它应该基于分层面板,停靠和填充.顺便说一下,它也解决了可变形式的大小和屏幕分辨率的问题.

请查看我过去的回答:
当屏幕分辨率改变时,Zom Out发生故障 [ ^ ].

在此代码中,您可以找到基本的代码示例:
如何停靠按钮,以便它可以使用以下格式进行调整 [ GUI外观-C#.Net [
Hi Sports,

This is easy to do, but usually overlapping of the controls is a result of wrong layout design. If you want to avoid overlapping, you should not waste time of detecting it. Right design won''t allow such things. It should be based on hierarchical panels, docking and padding. By the way, it also solves the problem of variable form''s size and screen resolution.

Please see my past answer:
Zom Out malfunctions when Screen resolution changes[^].

In this one you can find a rudimentary code sample:
how to dock button so that it can adjust with the form[^].

See also this one:
GUI Apperance - C#.Net[^].

Good luck,
—SA


这篇关于如何检测多个控件的重叠状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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