Private Overloads Sub OnMouseLeftButtonDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs) ' Respond to the left mouse button down event by initiating the hit test. HitTestResult result = VisualTreeHelper.HitTest(m圜anvas, pt) Perform the hit test against a given portion of the visual object tree. Point pt = e.GetPosition((UIElement)sender) Retrieve the coordinate of the mouse position. Private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e) Respond to the left mouse button down event by initiating the hit test. The following code shows how to set up mouse event handlers for a UIElement object that is used to capture events used for hit testing. For example, if you have nested visual objects defined at (100, 100) in the parent's coordinate space, then hit testing a child visual at (0, 0) is equivalent to hit testing at (100, 100) in the parent's coordinate space. The coordinate value you pass as the point parameter for the HitTest method has to be relative to the coordinate space of the visual object you are hit testing against. In order to identify all visual objects, even those that may be partly or wholly obscured, use a hit test result callback. See the sample code in the section "Using a Hit Test Results Callback" for more information.ĭefault hit testing always returns the top-most object in the z-order. This means you can hit test for visual objects that are beneath other objects, even if they are wholly obscured. If you want to enumerate all visual objects under a specific point or geometry, return Continue from the HitTestResultCallback. If you are only interested in hit testing the visual object whose z-order value is top-most, you can set the visual hit test enumeration to return Stop from the HitTestResultCallback to stop the hit test traversal after the first item. In the following illustration, the circle object is on top of both the square and triangle objects. You can hit test against the entire visual tree, or any portion of it. However, the visual object that you pass as the parameter to the HitTest method determines which portion of the visual tree that will be hit test. The Windows Presentation Foundation (WPF) visual layer supports hit testing against all objects under a point or geometry, not just the top-most object. The following illustration shows the relationship between a non-rectangular object's region and its bounding rectangle. You can also choose to override the default implementation of hit testing to perform your own custom hit test calculations. For example, you could use hit testing to determine whether a mouse click within the bounding rectangle of an object falls within the geometry of a circle. The purpose of the HitTest methods in the VisualTreeHelper class is to determine whether a geometry or point coordinate value is within the rendered content of a given object, such as a control or graphic element. Ignoring UIElement hit testing policy: This applies when you need to ignore the UIElement hit testing policy, which takes into consideration such factors as whether an element is disabled or invisible.įor a complete code sample illustrating hit testing at the visual layer, see Hit Test Using DrawingVisuals Sample and Hit Test with Win32 Interoperation Sample. You can get results for all visuals intersecting a geometry or point, not just the first one. Hit testing against multiple objects: This applies when you need to hit test against multiple objects, such as overlapping objects. Hit testing using a geometry: This applies if you need to hit test using a geometry object rather than the coordinate value of a point. Hit testing against non- UIElement objects: This applies if you are hit testing non- UIElement objects, such as DrawingVisual or graphics objects. However, there are several scenarios in which you may need to implement hit testing at the visual layer. In many cases, the InputHitTest method provides the desired functionality for implementing hit testing of elements. The UIElement class provides the InputHitTest method, which allows you to hit test against an element using a given coordinate value. Hit testing support allows you to determine whether a geometry or point value falls within the rendered content of a Visual, allowing you to implement user interface behavior such as a selection rectangle to select multiple objects. This topic provides an overview of hit testing functionality provided by the visual layer.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |