Get the HitTesting result when lots of drawings are in one single visual

醉酒当歌 提交于 2019-12-25 16:26:25

问题


So I have drawn some lines on a Visual Layer and added it to Canvas. Now that I'm trying to HitTest them I'm getting the whole visual. How can I get only the line that I click on?

public class MyDrawing : DrawingVisual
{
    public MyDrawing ()
    {
        using (var dc = RenderOpen())
        {
            foreach (var block in blocks)
            {
                // Draw lines here
            }
        }
    }
}

    private void OnMouseRightButtonUp(object sender, MouseButtonEventArgs e)
    {
        var x = MousePos.RightDown.X;
        var y = MousePos.RightDown.Y;

        var hitRect = new Rect(x - 2, y - 2, 4, 4);
        VisualTreeHelper.HitTest(MyCanvas, null, MyCallback, 
            new GeometryHitTestParameters(new RectangleGeometry(hitRect)));

        VisualTreeHelper.HitTest(zones, null, MyCallback,
new GeometryHitTestParameters(new RectangleGeometry(hitRect)));
    }

    private static HitTestResultBehavior MyCallback(HitTestResult result)
    {
        // I want to get the line I have hit here
        return HitTestResultBehavior.Stop;
    }

来源:https://stackoverflow.com/questions/25754861/get-the-hittesting-result-when-lots-of-drawings-are-in-one-single-visual

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!