8,737
社区成员
发帖
与我相关
我的任务
分享
private bool CheckHover(FrameworkElement control1, FrameworkElement controlElem1, FrameworkElement control2, FrameworkElement controlElem2)
{
// 建立一个矩形
Rect rect1 = UserControlBounds(control1);
Rect rect2 = UserControlBounds(control2);
rect1.Intersect(rect2);
if (rect1 == Rect.Empty)
{
//鼠标不在矩形中
return false;
}
else
{
bool bCollision = false;
Point ptCheck = new Point();
// 设置pixel进行hittest
for (int x = Convert.ToInt32(rect1.X); x < Convert.ToInt32(rect1.X + rect1.Width); x++)
{
for (int y = Convert.ToInt32(rect1.Y); y < Convert.ToInt32(rect1.Y + rect1.Height); y++)
{
ptCheck.X = x;
ptCheck.Y = y;
List<UIElement> hits = System.Windows.Media.VisualTreeHelper.FindElementsInHostCoordinates(ptCheck, control1) as List<UIElement>;
if (hits.Contains(controlElem1))
{
// 模拟hittest
List<UIElement> hits2 = System.Windows.Media.VisualTreeHelper.FindElementsInHostCoordinates(ptCheck, control2) as List<UIElement>;
if (hits2.Contains(controlElem2))
{
bCollision = true;
break;
}
}
}
if (bCollision) break;
}
return bCollision;
}
}
void CompositionTarget_Rendering(object sender, EventArgs e)
{
Path shipShell = ship.FindName("ShipShell") as Path;
Path pathAsteroid1 = asteroidXaml1.FindName("pathAsteroid") as Path;
if (CheckHover(ship, shipShell, asteroidXaml1, pathAsteroid1))
{
txtStatus.Text = "鼠标Hover事件";
return;
}
txtStatus.Text = string.Empty;
}