MSDN > 論壇首頁 > Windows Presentation Foundation (WPF) > Hit-Testing with a scaled path doesn't work
發問發問
 

已答覆Hit-Testing with a scaled path doesn't work

  • 2009年7月2日 下午 12:45Brain K. 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    Hello!

    I've got a Path as a String (s.th. like "F1 M 440.823,310.821C 430.458,312.303 422.313,320.451 420.833,330.82") and want to use it for hit testing. To check for other elements nearby I want to scale the path (from it's center) so that it's 20% (or the like) larger. But when it's scaled hit testing doesn't work - without scaling it works pretty well, but I need to know the elements close to the geometry...

    Here's some code:

            
     {
                Geometry geo = Geometry.Parse("F1 M 419.561,159.471C 421.041,169.841 429.185,177.988 439.551,179.469");
    
                Path hitTestPath = new Path();
                hitTestPath.Data = geo;
                hitTestPath.Fill = Brushes.CadetBlue;
                hitTestPath.Stretch = Stretch.Fill;
    
                Canvas.SetLeft(hitTestPath, 418.894);
                Canvas.SetTop(hitTestPath, 158.804);
    
                // DrawingBase is a Canvas
    DrawingBase.Children.Add(hitTestPath);
     ScaleTransform scTrans = new ScaleTransform(); scTrans.ScaleX = 2; scTrans.ScaleY = 2; scTrans.CenterX = 10; scTrans.CenterY = 10; hitTestPath.RenderTransform = scTrans; // Set up a callback to receive the hit test result enumeration. VisualTreeHelper.HitTest(DrawingBase, null, new HitTestResultCallback(myHitTestCallback), new GeometryHitTestParameters(hitTestPath.Data)); } public HitTestResultBehavior myHitTestCallback(HitTestResult result) { if (result.VisualHit.GetType() == typeof(Path)) { Path path = (Path)result.VisualHit; MessageBox.Show("Pathname: " + path.Name); } return HitTestResultBehavior.Continue; }

    Any ideas? Thx!!!
    • 已編輯Brain K. 2009年7月4日 下午 05:06
    •  

解答

  • 2009年7月7日 下午 09:43Brendan Clark - MSFT 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆
    This is a known issue - the hit testing code uses an absolute amount of tolerance relative to the size of the geometry itself, not the rendered size including the application of any scale transforms.  That means that at the geometry's natural size hit-testing works fine, but the small amount of leeway will be greatly amplified when it is scaled up which gives the impression of hit-testing using far larger bounds than expected.  This won't be fixed for the next release.  The best work-around is to generate your geometry at a size closer to the largest size you need it to be accurately hit-tested, and to scale down from there instead of up, since shrinking the tolerance is less noticeable than increasing it.

所有回覆

  • 2009年7月4日 下午 05:08Brain K. 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    No ideas? :(

    I have to find a solution - I'll tell you my solution as soon as I found one... ;)
  • 2009年7月7日 下午 09:43Brendan Clark - MSFT 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆
    This is a known issue - the hit testing code uses an absolute amount of tolerance relative to the size of the geometry itself, not the rendered size including the application of any scale transforms.  That means that at the geometry's natural size hit-testing works fine, but the small amount of leeway will be greatly amplified when it is scaled up which gives the impression of hit-testing using far larger bounds than expected.  This won't be fixed for the next release.  The best work-around is to generate your geometry at a size closer to the largest size you need it to be accurately hit-tested, and to scale down from there instead of up, since shrinking the tolerance is less noticeable than increasing it.