WPF - Hit Testing Rendered Pixel Characters

Is there a way to hit pixel characters in wpf? I want to get a hit for point "i", but not get hit when I press the space bar between point and line "i". I tried to do this using the DrawingContext.DrawText method, but when I hit the test with the resulting DrawingVisual, which contains the text using the VisualTreeHelper.HitTest method, it does not distinguish between the space and the black pixels of the character. It gives a hit for each point in the bounding box of the character. Is there any way to do this?

+3
source share
1 answer

I have found a solution. To do this, you need to go to a lower level in GlyphRunDrawing. Use here and replace GeometryDrawing with GlyphRunDrawing. Then run the GeometryGroup, which contains each symbol as Geometry, and do some test testing with the code:

GeometryGroup ggroup = (GeometryGroup)((GlyphRunDrawing)drawing).GlyphRun.BuildGeometry();
if(ggroup.FillContains(pt))
{
    Debug.WriteLine("Contains point " + pt.ToString());
}
+4
source

Source: https://habr.com/ru/post/1779659/


All Articles