无论如何在WPF中使用几何来测试uielement吗?我试过VisualTreeHelper
了,但它不起作用。UIElement
如果鼠标用一个点点击它,可能会引发鼠标按下事件。
但是,如果几何与 an 相交,我想引发一个事件UIElement
。我怎么能这样做?
public class MyUI : UIElement
{
protected override void OnReder(DrawingContext dc)
{
dc.DrawRectangle(..., new Rect(12,12,120,120));
...
}
}
MyUI ui = new MyUI();
Grid grid = new Grid();
grid.Children.Add(ui);
EllipseGeometry eg = new EllipseGeometry(new Rect(24,24,40,40));
VisualTreeHelper.HitTest(grid, null, HitTestResult, new GeometryHitTestParameters(eg));
if (results.Count > 0)
MessageBox.Show("Hit Count = "+results.ToString());
...
List<DependencyObject> results;
public HitTestResultBehavior HitTestResult(HitTestResult result)
{
results.Add(result.VisualHit);
return HitTestResultBehavior.Continue;
}