我想在某些形状上使用 WPF HitTest 功能而不在屏幕上显示它们。这可能吗?这段代码不像我预期的那样工作。我该如何进行这项工作?
[Test, Ignore, RequiresSTA]
public void VerifyFillContains()
{
// make a simple triangle:
var seg1 = new System.Windows.Media.LineSegment(new Point(0.2, 0.1), false);
var seg2 = new System.Windows.Media.LineSegment(new Point(0.3, 0.8), false);
var figure = new PathFigure(new Point(0.7, 0.5), new List<PathSegment>{seg1, seg2}, true);
var sg = new PathGeometry();
sg.Figures.Add(figure);
if (sg.CanFreeze)
sg.Freeze();
// these don't work:
Assert.IsFalse(sg.FillContains(new Point()));
Assert.IsFalse(sg.FillContains(new Point(1.0, 1.0)));
Assert.IsTrue(sg.FillContains(new Point(0.5, 0.5)));
var path = new Path { Data = sg, Fill = Brushes.Black };
path.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
path.Arrange(new Rect(path.DesiredSize));
// and these don't work:
var result = VisualTreeHelper.HitTest(path, new Point(0.0, 0.0));
Assert.IsFalse(result != null && Equals(result.VisualHit, path));
result = VisualTreeHelper.HitTest(path, new Point(1.0, 1.0));
Assert.IsFalse(result != null && Equals(result.VisualHit, path));
result = VisualTreeHelper.HitTest(path, new Point(0.5, 0.5));
Assert.IsTrue(result != null && Equals(result.VisualHit, path));
}