我有一个包含Canvas的WPF窗口,该Canvas用代码中的旋转矩形填充。每个矩形都有一个MouseDown事件,它们的位置将根据用户提供的坐标进行分布。通常两个或多个会重叠,部分遮挡其下方的矩形。
我需要为按下鼠标下方的每个矩形触发MouseDown事件,即使该矩形被另一个矩形遮挡了,但我只获得最顶部矩形的MouseDown事件。
我尝试为单击的矩形设置e.Handled,并在没有运气的情况下通过Canvas路由事件,甚至尝试根据对象的坐标在鼠标下方定位对象,但是矩形使计算变得困难。
public MainWindow()
{
InitializeComponent();
Rectangle r1 = new Rectangle() {Width = 80, Height = 120, Fill = Brushes.Blue };
r1.MouseDown += r_MouseDown;
RotateTransform rt1 = new RotateTransform(60);
r1.RenderTransform = rt1;
Canvas.SetLeft(r1, 150);
Canvas.SetTop(r1, 50);
canvas1.Children.Add(r1);
Rectangle r2 = new Rectangle() { Width = 150, Height = 50, Fill = Brushes.Green };
r2.MouseDown += r_MouseDown;
RotateTransform rt2 = new RotateTransform(15);
r2.RenderTransform = rt2;
Canvas.SetLeft(r2, 100);
Canvas.SetTop(r2, 100);
canvas1.Children.Add(r2);
}
private void r_MouseDown(object sender, MouseButtonEventArgs e)
{
Console.WriteLine("Rectangle Clicked");
}
}
答案 0 :(得分:0)
还有一个与此类似的问题,但是没有公认的答案,对于解决该问题的最终解决方案应该是什么还不清楚。让我们看看是否可以更清楚一点。
首先,下面概述的解决方案将使用VisualTreeHelper.HitTest
方法来识别鼠标是否单击了矩形。即使由于Canvas.SetTop
和各种.RenderTransform
等操作而移动了矩形,VisualTreeHelper仍使我们能够找到它们。
第二,我们将在您的canvas元素而不是单个矩形上捕获click事件。这样一来,我们就可以在画布一级处理内容,并立即检查所有矩形。
public MainWindow()
{
InitializeComponent();
//Additional rectangle for testing.
Rectangle r3 = new Rectangle() { Width = 175, Height = 80, Fill = Brushes.Goldenrod };
Canvas.SetLeft(r3, 80);
Canvas.SetTop(r3, 80);
canvas1.Children.Add(r3);
Rectangle r1 = new Rectangle() { Width = 80, Height = 120, Fill = Brushes.Blue };
RotateTransform rt1 = new RotateTransform(60);
r1.RenderTransform = rt1;
Canvas.SetLeft(r1, 100);
Canvas.SetTop(r1, 100);
canvas1.Children.Add(r1);
Rectangle r2 = new Rectangle() { Width = 150, Height = 50, Fill = Brushes.Green };
RotateTransform rt2 = new RotateTransform(15);
r2.LayoutTransform = rt2;
Canvas.SetLeft(r2, 100);
Canvas.SetTop(r2, 100);
canvas1.Children.Add(r2);
//Mouse 'click' event.
canvas1.PreviewMouseDown += canvasMouseDown;
}
//list to store the hit test results
private List<HitTestResult> hitResultsList = new List<HitTestResult>();
使用的HitTest方法更为复杂,因为该方法的最简单版本仅返回“最顶层”项目。最顶部,它们是指第一个绘制的项目,因此从视觉上看,它实际上是矩形堆栈底部的那个项目。为了获得所有矩形,我们需要使用下面显示的HitTest方法的复杂版本。
private void canvasMouseDown(object sender, MouseButtonEventArgs e)
{
if (canvas1.Children.Count > 0)
{
// Retrieve the coordinates of the mouse position.
Point pt = e.GetPosition((UIElement)sender);
// Clear the contents of the list used for hit test results.
hitResultsList.Clear();
// Set up a callback to receive the hit test result enumeration.
VisualTreeHelper.HitTest(canvas1,
new HitTestFilterCallback(MyHitTestFilter),
new HitTestResultCallback(MyHitTestResult),
new PointHitTestParameters(pt));
// Perform actions on the hit test results list.
if (hitResultsList.Count > 0)
{
string msg = null;
foreach (HitTestResult htr in hitResultsList)
{
Rectangle r = (Rectangle)htr.VisualHit;
msg += r.Fill.ToString() + "\n";
}
//Message displaying the fill colors of all the rectangles
//under the mouse when it was clicked.
MessageBox.Show(msg);
}
}
}
// Filter the hit test values for each object in the enumeration.
private HitTestFilterBehavior MyHitTestFilter(DependencyObject o)
{
// Test for the object value you want to filter.
if (o.GetType() == typeof(Label))
{
// Visual object and descendants are NOT part of hit test results enumeration.
return HitTestFilterBehavior.ContinueSkipSelfAndChildren;
}
else
{
// Visual object is part of hit test results enumeration.
return HitTestFilterBehavior.Continue;
}
}
// Add the hit test result to the list of results.
private HitTestResultBehavior MyHitTestResult(HitTestResult result)
{
//Filter out the canvas object.
if (!result.VisualHit.ToString().Contains("Canvas"))
{
hitResultsList.Add(result);
}
// Set the behavior to return visuals at all z-order levels.
return HitTestResultBehavior.Continue;
}
上面的测试示例仅显示一个消息框,显示单击鼠标指针时所有矩形的填充色;验证VisualTreeHelper实际上确实检索了堆栈中的所有矩形。