我正在尝试在画布上实现WPF Path对象的橡皮筋选择。不幸的是,我将VisualTreeHelper.HitTest与矩形几何一起使用无法正常工作。
我希望仅当我的橡皮筋矩形的一部分与直线的线路径相交时才会受到打击。但是使用rect时,即使我不在直线或边界框附近,只要我的rect在行的左侧或上方,我都会受到打击。
是否有某种方法可以解决此问题,或者很明显我做错了什么?] >>
我编写了一个简单的应用程序来演示该问题。这是一行和一个标签。如果我对VisualTreeHelper.HitTest的调用(使用橡皮筋)检测到其在形状上,则将底部的标签设置为Visible。否则,标签会折叠。
我在这条线上,正如我所期望的那样,它检测到一个命中。很好。
这里我在线下,没有命中。这也不错
但是只要我在行的左边或上方,无论走多远,我都会受到打击
这里是测试应用程序窗口:
<Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:po="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options" Title="MainWindow" Height="500" Width="525"> <Window.Resources> <LineGeometry x:Key="LineGeo" StartPoint="50, 100" EndPoint="200, 75"/> </Window.Resources> <Canvas x:Name="MyCanvas" Background="Yellow" MouseLeftButtonDown="MyCanvas_OnMouseLeftButtonDown" MouseMove="MyCanvas_OnMouseMove" MouseLeftButtonUp="MyCanvas_OnMouseLeftButtonUp" > <!-- The line I hit-test --> <Path x:Name="MyLine" Data="{StaticResource LineGeo}" Stroke="Black" StrokeThickness="5" Tag="1234" /> <!-- This label's is hidden by default and only shows up when code-behind sets it to Visible --> <Label x:Name="MyLabel" Canvas.Left="100" Canvas.Top="200" Content="HIT DETECTED!!!" FontSize="25" FontWeight="Bold" Visibility="{x:Static Visibility.Collapsed}"/> </Canvas> </Window>
这是带有HitTest代码的鼠标代码背后的处理程序
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
namespace WpfApp1
{
public partial class MainWindow : Window
{
public MainWindow() => InitializeComponent();
private Point _startPosition;
Path _path;
private RectangleGeometry _rectGeo;
private static readonly SolidColorBrush _brush = new SolidColorBrush(Colors.BlueViolet) { Opacity=0.3 };
private void MyCanvas_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
MyCanvas.CaptureMouse();
_startPosition = e.GetPosition(MyCanvas);
// Create the visible selection rect and add it to the canvas
_rectGeo = new RectangleGeometry();
_rectGeo.Rect = new Rect(_startPosition, _startPosition);
_path = new Path()
{
Data = _rectGeo,
Fill =_brush,
StrokeThickness = 0,
IsHitTestVisible = false
};
MyCanvas.Children.Add(_path);
}
private void MyCanvas_OnMouseMove(object sender, MouseEventArgs e)
{
// Sanity check
if (e.MouseDevice.LeftButton != MouseButtonState.Pressed ||
null == _path ||
!MyCanvas.IsMouseCaptured)
{
return;
}
e.Handled = true;
// Get the second position for the rect geometry
var curPos = e.GetPosition(MyCanvas);
var rect = new Rect(_startPosition, curPos);
_rectGeo.Rect = rect;
_path.Data = _rectGeo;
// This is set up like a loop because my real production code is looking
// for many shapes.
var paths = new List<Path>();
var htp = new GeometryHitTestParameters(_rectGeo);
var resultCallback = new HitTestResultCallback(r => HitTestResultBehavior.Continue);
var filterCallback = new HitTestFilterCallback(
el =>
{
// Filter accepts any object of type Path. There should be just one
if (el is Path s && s.Tag != null)
paths.Add(s);
return HitTestFilterBehavior.Continue;
});
VisualTreeHelper.HitTest(MyCanvas, filterCallback, resultCallback, htp);
// Set the label visibility based on whether or not we hit the line
var line = paths.FirstOrDefault();
MyLabel.Visibility = ReferenceEquals(line, MyLine) ? Visibility.Visible : Visibility.Collapsed;
}
private void MyCanvas_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (null == _path)
return;
e.Handled = true;
MyLabel.Visibility = Visibility.Collapsed;
MyCanvas.Children.Remove(_path);
_path = null;
if (MyCanvas.IsMouseCaptured)
MyCanvas.ReleaseMouseCapture();
}
}
}
我正在尝试在画布上实现WPF Path对象的橡皮筋选择。不幸的是,我将VisualTreeHelper.HitTest与矩形几何一起使用无法正常工作。我...
您的代码中的问题是您没有正确使用命中测试回调。过滤器回调用于从匹配测试中排除对象。这是结果回调,可为您提供有关实际测试被击中的信息。