如何检测两个面板的重叠区域。 [英] How to detect overlapped area of two panels.

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

问题描述

我有两个不同宽度的可调整大小的面板。重新调整大小,如果他们重叠,那么我想只获得重叠区域。我试着检测重叠面板,但不知道如何获得重叠区域(宽度)。

 Rectangle sr =  new  Rectangle(panel1.Left,panel1.Top,panel1.Width,panel1.Height); 
foreach (控制ctrl .Controls)
{
if (ctrl Panel&& ; ctrl!= panel1)
{
Rectangle r = new 矩形(ctrl.Left,ctrl.Top,ctrl.Width,ctrl.Height );
if (sr.IntersectsWith(r))
{
// 这里我只想要ctrl的重叠区域(宽度)。
}
}
}



如有任何想法请分享。

解决方案

致电 Rectangle.Intersect方法(矩形,矩形) [ ^ ]获取交叉矩形然后使用其宽度属性。


  if (sr .IntersectsWith(r))
{
Rectangle rectangle3 = Rectangle.Intersect(sr,r);
// rectangle3表示两个其他Rectangle结构的交集
}


您可以简化代码:

  //  需要LInq  
使用 System.Linq

private void getIntersection(Panel targetPanel,Control containerToSearch)
{
Rectangle targetBounds = targetPanel.Bounds;

Rectangle testIntersect;

foreach (Panel thePanel in containerToSearch.Controls.OfType< Panel>( ))
{
if (thePanel == targetPanel) continue ;

testIntersect = Rectangle.Intersect(targetBounds,thePanel.Bounds);

if (testIntersect!= Rectangle.Empty)
{
// 仅供测试
Console.WriteLine( 面板名称:{0} \r\\\
intersection:{1}
,thePanel.Name,testIntersect.ToString());

// 无论你需要做什么...

// 当您找​​到第一个交叉面板时停止
断裂;
}
}
} < / panel >


I have two re-sizable panels having different width. on re-sizing if they get overlapped then i want to get only overlapped area. I tried following to detect overlapped panels, but not idea about how to get overlapped area(width).

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))
		{
			//here i want only the overlapped area(width) of ctrl.
		}
	}
}


if have any idea please share.

解决方案

Call the Rectangle.Intersect Method (Rectangle, Rectangle)[^] to get the intersection rectangle and then use its Width property.


if (sr.IntersectsWith(r))
{
   Rectangle rectangle3 = Rectangle.Intersect(sr, r);
   //rectangle3 represents the intersection of two other Rectangle structures
}


You can simplify your code:

// requires LInq
using System.Linq

private void getIntersection(Panel targetPanel, Control containerToSearch)
{
    Rectangle targetBounds = targetPanel.Bounds;

    Rectangle testIntersect;

    foreach (Panel thePanel in containerToSearch.Controls.OfType<Panel>())
    {
        if (thePanel == targetPanel) continue;

        testIntersect = Rectangle.Intersect(targetBounds, thePanel.Bounds);
        
        if (testIntersect != Rectangle.Empty)
        {
            // for testing only
            Console.WriteLine("panel name: {0} \r\nintersection: {1}", thePanel.Name, testIntersect.ToString());

            // whatever else you need to do ...

            // stop when you find the first intersecting Panel
            break;
        }
    }
}</panel>


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

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