C# 为画布上的重叠矩形触发MouseDown事件
我有一个WPF窗口,其中包含一个画布,在代码中填充了旋转的矩形。每个矩形都有一个MouseDown事件,它们的位置将根据用户提供的坐标分布。通常两个或多个会重叠,部分阻碍其下方的矩形 我需要在按下鼠标时为鼠标下的每个矩形触发MouseDown事件,即使该矩形被另一个矩形阻挡,但我只为最上面的矩形获取MouseDown事件 我曾尝试为单击的矩形设置e.Handled,并在画布上路由事件,但运气不佳,甚至尝试根据鼠标下方的对象坐标定位对象,但矩形的旋转使得这一点难以计算C# 为画布上的重叠矩形触发MouseDown事件,c#,wpf,canvas,rectangles,C#,Wpf,Canvas,Rectangles,我有一个WPF窗口,其中包含一个画布,在代码中填充了旋转的矩形。每个矩形都有一个MouseDown事件,它们的位置将根据用户提供的坐标分布。通常两个或多个会重叠,部分阻碍其下方的矩形 我需要在按下鼠标时为鼠标下的每个矩形触发MouseDown事件,即使该矩形被另一个矩形阻挡,但我只为最上面的矩形获取MouseDown事件 我曾尝试为单击的矩形设置e.Handled,并在画布上路由事件,但运气不佳,甚至尝试根据鼠标下方的对象坐标定位对象,但矩形的旋转使得这一点难以计算 public Mai
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");
}
}
还有一个问题与此类似,但没有公认的答案,也不清楚解决这个问题的最终解决方案是什么。让我们看看能不能更清楚一点 首先,下面概述的解决方案将使用
VisualTreeHelper.HitTest
方法来识别鼠标是否单击了矩形。VisualTreeHelper允许我们查找矩形,即使它们由于Canvas.SetTop
和各种.RenderTransform
操作而移动
其次,我们将在画布元素而不是单个矩形上捕获单击事件。这允许我们在画布级别处理事情,并一次检查所有矩形
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>();
上面的测试示例只显示了一个消息框,当鼠标指针被单击时,它显示了鼠标指针下所有矩形的填充颜色;验证VisualTreeHelper是否确实检索了堆栈中的所有矩形
与您的要求非常相似。此解决方案非常有效。我曾希望有一个简单一点的东西存在,但这确实起到了作用,我可以从中得到我需要的信息。谢谢@Stewbob。是的,当我看到你的问题时,我想,“哦,我知道怎么做”,然后花了半天的时间意识到这并不容易。多重形状,尤其是对象上的变换,需要使用VisualTreeHelper,这使得它有点麻烦。
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;
}