操作說明:使用幾何當做參數進行點擊測試
這個範例示範如何使用 作為點擊測試參數,在視覺物件 Geometry 上執行點擊測試。
範例
下列範例示範如何使用 方法來設定點擊測試 GeometryHitTestParametersHitTest 。 Point傳遞至 OnMouseDown
方法的值是用來建立 Geometry 物件,以展開點擊測試的範圍。
// Respond to the mouse button down event by setting up a hit test results callback.
private void OnMouseDown(object sender, MouseButtonEventArgs e)
{
// Retrieve the coordinate of the mouse position.
Point pt = e.GetPosition((UIElement)sender);
// Expand the hit test area by creating a geometry centered on the hit test point.
EllipseGeometry expandedHitTestArea = new EllipseGeometry(pt, 10.0, 10.0);
// Clear the contents of the list used for hit test results.
hitResultsList.Clear();
// Set up a callback to receive the hit test result enumeration.
VisualTreeHelper.HitTest(myControl, null,
new HitTestResultCallback(MyHitTestResultCallback),
new GeometryHitTestParameters(expandedHitTestArea));
// Perform actions on the hit test results list.
if (hitResultsList.Count > 0)
{
ProcessHitTestResultsList();
}
}
' Respond to the mouse button down event by setting up a hit test results callback.
Private Overloads Sub OnMouseDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
' Retrieve the coordinate of the mouse position.
Dim pt As Point = e.GetPosition(CType(sender, UIElement))
' Expand the hit test area by creating a geometry centered on the hit test point.
Dim expandedHitTestArea As New EllipseGeometry(pt, 10.0, 10.0)
' Clear the contents of the list used for hit test results.
hitResultsList.Clear()
' Set up a callback to receive the hit test result enumeration.
VisualTreeHelper.HitTest(myControl, Nothing, New HitTestResultCallback(AddressOf MyHitTestResultCallback), New GeometryHitTestParameters(expandedHitTestArea))
' Perform actions on the hit test results list.
If hitResultsList.Count > 0 Then
ProcessHitTestResultsList()
End If
End Sub
的 IntersectionDetailGeometryHitTestResult 屬性提供使用 作為點擊測試參數之點擊測試結果 Geometry 的相關資訊。 下圖顯示點擊測試幾何 (藍色圓形) 和目標視覺物件呈現內容 (紅色矩形) 之間的關係。
下列範例示範當 做點擊測試參數使用 時 Geometry ,如何實作點擊測試回呼。 參數 result
會轉換成 GeometryHitTestResult ,以擷取 屬性的值 IntersectionDetail 。 屬性值可讓您判中斷點擊測試參數是否 Geometry 完全或部分包含在點擊測試目標的轉譯內容中。 在此情況下,範例程式碼僅將點擊測試結果加入至完全包含在目標範圍內之視覺效果的清單中。
// Return the result of the hit test to the callback.
public HitTestResultBehavior MyHitTestResultCallback(HitTestResult result)
{
// Retrieve the results of the hit test.
IntersectionDetail intersectionDetail = ((GeometryHitTestResult)result).IntersectionDetail;
switch (intersectionDetail)
{
case IntersectionDetail.FullyContains:
// Add the hit test result to the list that will be processed after the enumeration.
hitResultsList.Add(result.VisualHit);
return HitTestResultBehavior.Continue;
case IntersectionDetail.Intersects:
// Set the behavior to return visuals at all z-order levels.
return HitTestResultBehavior.Continue;
case IntersectionDetail.FullyInside:
// Set the behavior to return visuals at all z-order levels.
return HitTestResultBehavior.Continue;
default:
return HitTestResultBehavior.Stop;
}
}
' Return the result of the hit test to the callback.
Public Function MyHitTestResultCallback(ByVal result As HitTestResult) As HitTestResultBehavior
' Retrieve the results of the hit test.
Dim intersectionDetail As IntersectionDetail = (CType(result, GeometryHitTestResult)).IntersectionDetail
Select Case intersectionDetail
Case IntersectionDetail.FullyContains
' Add the hit test result to the list that will be processed after the enumeration.
hitResultsList.Add(result.VisualHit)
Return HitTestResultBehavior.Continue
Case IntersectionDetail.Intersects
' Set the behavior to return visuals at all z-order levels.
Return HitTestResultBehavior.Continue
Case IntersectionDetail.FullyInside
' Set the behavior to return visuals at all z-order levels.
Return HitTestResultBehavior.Continue
Case Else
Return HitTestResultBehavior.Stop
End Select
End Function
注意
HitTestResult當交集詳細資料為 Empty 時,不應該呼叫回呼。
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應