Nota
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare ad accedere o modificare le directory.
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare a modificare le directory.
In questo esempio viene illustrato come eseguire un hit test su un oggetto visivo composto da uno o più oggetti Geometry.
Esempio
Nell'esempio seguente viene illustrato come recuperare il DrawingGroup da un oggetto visivo che usa il metodo GetDrawing. Viene quindi eseguito un test di colpimento sul contenuto reso di ciascun grafico nel DrawingGroup per determinare quale geometria è stata colpita.
Nota
Nella maggior parte dei casi, si userebbe il metodo HitTest per determinare se un punto interseca uno qualsiasi del contenuto sottoposto a rendering di un oggetto visivo.
// Determine if a geometry within the visual was hit.
static public void HitTestGeometryInVisual(Visual visual, Point pt)
{
// Retrieve the group of drawings for the visual.
DrawingGroup drawingGroup = VisualTreeHelper.GetDrawing(visual);
EnumDrawingGroup(drawingGroup, pt);
}
// Enumerate the drawings in the DrawingGroup.
static public void EnumDrawingGroup(DrawingGroup drawingGroup, Point pt)
{
DrawingCollection drawingCollection = drawingGroup.Children;
// Enumerate the drawings in the DrawingCollection.
foreach (Drawing drawing in drawingCollection)
{
// If the drawing is a DrawingGroup, call the function recursively.
if (drawing.GetType() == typeof(DrawingGroup))
{
EnumDrawingGroup((DrawingGroup)drawing, pt);
}
else if (drawing.GetType() == typeof(GeometryDrawing))
{
// Determine whether the hit test point falls within the geometry.
if (((GeometryDrawing)drawing).Geometry.FillContains(pt))
{
// Perform action based on hit test on geometry.
}
}
}
}
' Determine if a geometry within the visual was hit.
Public Shared Sub HitTestGeometryInVisual(ByVal visual As Visual, ByVal pt As Point)
' Retrieve the group of drawings for the visual.
Dim drawingGroup As DrawingGroup = VisualTreeHelper.GetDrawing(visual)
EnumDrawingGroup(drawingGroup, pt)
End Sub
' Enumerate the drawings in the DrawingGroup.
Public Shared Sub EnumDrawingGroup(ByVal drawingGroup As DrawingGroup, ByVal pt As Point)
Dim drawingCollection As DrawingCollection = drawingGroup.Children
' Enumerate the drawings in the DrawingCollection.
For Each drawing As Drawing In drawingCollection
' If the drawing is a DrawingGroup, call the function recursively.
If drawing.GetType() Is GetType(DrawingGroup) Then
EnumDrawingGroup(CType(drawing, DrawingGroup), pt)
ElseIf drawing.GetType() Is GetType(GeometryDrawing) Then
' Determine whether the hit test point falls within the geometry.
If (CType(drawing, GeometryDrawing)).Geometry.FillContains(pt) Then
' Perform action based on hit test on geometry.
End If
End If
Next drawing
End Sub
Il metodo FillContains è un metodo sovraccaricato che consente di eseguire l'hit test utilizzando uno specifico Point o Geometry. Se viene tracciata una geometria, il tratto può estendersi all'esterno dei limiti di riempimento. In tal caso, è possibile chiamare StrokeContains oltre a FillContains.
È anche possibile fornire un ToleranceType che viene usato ai fini dell'appiattimento di Bézier.
Nota
Questo esempio non tiene conto di eventuali trasformazioni o ritagli che vengano applicati alla geometria. Inoltre, questo esempio non funzionerà con un controllo con stile, poiché non dispone di elementi grafici direttamente associati.
Vedere anche
.NET Desktop feedback