即使不是Rect ,我们也需要将 Rectangle 转换成Rect。 我们需要得到Rectangle所占的Rect结构范围,可以通过下面的代码得到一个Rectangle的大小范围,他为Rect结构。
public static Rect GetElementRect(FrameworkElement element)
{
GeneralTransform buttonTransform = element.TransformToVisual(null);
Point point = buttonTransform.TransformPoint(new Point());
return new Rect(point, new Size(element.ActualWidth, element.ActualHeight));
}
然后调用Rect.Intersect方法来对两个Rect对象进行相交检测。如果Rect.Intersect(Rect)之后,当前Rect有大小,说明已经相交。
Bob Bao [MSFT]
MSDN Community Support | Feedback to us
