共用方式為


UIElement.CapturePointer(Pointer) 方法

定義

設定 UIElement的指標擷取。 擷取之後,只有具有擷取的專案才會引發指標相關事件。

public:
 virtual bool CapturePointer(Pointer ^ value) = CapturePointer;
bool CapturePointer(Pointer const& value);
public bool CapturePointer(Pointer value);
function capturePointer(value)
Public Function CapturePointer (value As Pointer) As Boolean

參數

value
Pointer

指標物件參考。

傳回

Boolean

bool

如果物件具有指標擷取,則為true;否則為false

範例

此範例示範根據處理 PointerPressed呼叫 CapturePointer。 它也會顯示追蹤和計算指標參考的模式。

int _pointerCount;

public Scenario2()
{
    this.InitializeComponent();
    bEnteredExited.PointerEntered += bEnteredExited_PointerEntered;
    bEnteredExited.PointerExited += bEnteredExited_PointerExited;
    bEnteredExited.PointerPressed += bEnteredExited_PointerPressed;
    bEnteredExited.PointerReleased += bEnteredExited_PointerReleased;
    bEnteredExited.PointerMoved += bEnteredExited_PointerMoved;

    // To code for multiple Pointers (that is, fingers), 
    // we track how many entered/exited.
    _pointerCount = 0;
}

private void bEnteredExited_PointerMoved(object sender, 
    PointerRoutedEventArgs e)
{
    Scenario2UpdateVisuals(sender as Border, "Moved");
}

private void bEnteredExited_PointerReleased(object sender, 
    PointerRoutedEventArgs e)
{
    ((Border)sender).ReleasePointerCapture(e.Pointer);
    txtCaptureStatus.Text = string.Empty;
}

//Can only get capture on PointerPressed (i.e. touch down, mouse click, pen press)
private void bEnteredExited_PointerPressed(object sender, 
    PointerRoutedEventArgs e)
{
    if (tbPointerCapture.IsOn)
    {
        bool _hasCapture = ((Border)sender).CapturePointer(e.Pointer);
        txtCaptureStatus.Text = "Got Capture: " + _hasCapture;
    }
}

private void bEnteredExited_PointerExited(object sender, 
    PointerRoutedEventArgs e)
{
    _pointerCount--;
    Scenario2UpdateVisuals(sender as Border, "Exited");
}

private void bEnteredExited_PointerEntered(object sender, 
    PointerRoutedEventArgs e)
{
    _pointerCount++;
    Scenario2UpdateVisuals(sender as Border, "Entered");
}

private void Scenario2UpdateVisuals(Border border, 
    String eventDescription)
{
    switch (eventDescription.ToLower())
    {
        case "exited":
            if (_pointerCount <= 0)
            {
                border.Background = new SolidColorBrush(Colors.Red);
                bEnteredExitedTextBlock.Text = eventDescription;
            }
            break;
        case "moved":
            RotateTransform rt = 
                (RotateTransform)bEnteredExitedTimer.RenderTransform;
            rt.Angle += 2;
            if (rt.Angle > 360) rt.Angle -= 360;
            break;
        default:
            border.Background = new SolidColorBrush(Colors.Green);
            bEnteredExitedTextBlock.Text = eventDescription;
            break;
    }
}

private void Scenario2Reset(object sender, RoutedEventArgs e)
{
    Scenario2Reset();
}

private void Scenario2Reset()
{
    bEnteredExited.Background = new SolidColorBrush(Colors.Green);
    bEnteredExitedTextBlock.Text = string.Empty;
}
Private _pointerCount As Integer

Public Sub New()
    Me.InitializeComponent()
    AddHandler bEnteredExited.PointerEntered, AddressOf bEnteredExited_PointerEntered
    AddHandler bEnteredExited.PointerExited, AddressOf bEnteredExited_PointerExited
    AddHandler bEnteredExited.PointerPressed, AddressOf bEnteredExited_PointerPressed
    AddHandler bEnteredExited.PointerReleased, AddressOf bEnteredExited_PointerReleased
    AddHandler bEnteredExited.PointerMoved, AddressOf bEnteredExited_PointerMoved

    'To code for multiple Pointers (i.e. Fingers) we track how many entered/exited.
    _pointerCount = 0
End Sub

''' <summary>
''' Invoked when this page is about to be displayed in a Frame.
''' </summary>
''' <param name="e">Event data that describes how this page was reached.  The Parameter
''' property is typically used to configure the page.</param>
Protected Overrides Sub OnNavigatedTo(e As NavigationEventArgs)
End Sub

Private Sub bEnteredExited_PointerMoved(sender As Object, e As PointerRoutedEventArgs)
    Scenario2UpdateVisuals(TryCast(sender, Border), "Moved")
End Sub

Private Sub bEnteredExited_PointerReleased(sender As Object, e As PointerRoutedEventArgs)
    DirectCast(sender, Border).ReleasePointerCapture(e.Pointer)
    txtCaptureStatus.Text = String.Empty
End Sub

'Can only get capture on PointerPressed (i.e. touch down, mouse click, pen press)
Private Sub bEnteredExited_PointerPressed(sender As Object, e As PointerRoutedEventArgs)
    If tbPointerCapture.IsOn Then
        Dim _hasCapture As Boolean = DirectCast(sender, Border).CapturePointer(e.Pointer)
        txtCaptureStatus.Text = "Got Capture: " & _hasCapture
    End If
End Sub

Private Sub bEnteredExited_PointerExited(sender As Object, e As PointerRoutedEventArgs)
    _pointerCount -= 1
    Scenario2UpdateVisuals(TryCast(sender, Border), "Exited")
End Sub

Private Sub bEnteredExited_PointerEntered(sender As Object, e As PointerRoutedEventArgs)
    _pointerCount += 1
    Scenario2UpdateVisuals(TryCast(sender, Border), "Entered")

End Sub

Private Sub Scenario2UpdateVisuals(border As Border, eventDescription As String)
    Select Case eventDescription.ToLower()
        Case "exited"
            If _pointerCount <= 0 Then
                border.Background = New SolidColorBrush(Colors.Red)
                bEnteredExitedTextBlock.Text = eventDescription
            End If
            Exit Select
        Case "moved"

            Dim rt As RotateTransform = DirectCast(bEnteredExitedTimer.RenderTransform, RotateTransform)
            rt.Angle += 2
            If rt.Angle > 360 Then
                rt.Angle -= 360
            End If
            Exit Select
        Case Else
            border.Background = New SolidColorBrush(Colors.Green)
            bEnteredExitedTextBlock.Text = eventDescription
            Exit Select

    End Select
End Sub

Private Sub Scenario2ResetMethod(sender As Object, e As RoutedEventArgs)
    Reset()
End Sub

Private Sub Reset()
    bEnteredExited.Background = New SolidColorBrush(Colors.Green)
    bEnteredExitedTextBlock.Text = String.Empty
End Sub

備註

如果指標處於按下狀態,則您只能成功擷取指標, (Pointer.IsInContact 應該是 true) 。 實際構成的內容會根據按下的指標裝置類型而有所不同, (按下滑鼠按鍵、觸控點向下、連絡人中的手寫筆) 。 如果您嘗試擷取未按下的指標,或先前按下指標但放開的位置,CapturePointer 會傳回 false。 現有擷取不會受到傳回 false的 CapturePointer 呼叫影響。

您通常會擷取 PointerPressed 事件處理常式內的指標。 您從PointerPressed處理常式的PointerRoutedEventArgs事件資料取得的Pointer實例是您從處理常式程式碼內呼叫 CapturePointer 時,應該針對 value 參數傳遞的值

您通常會擷取指標,因為您想要目前的指標動作在應用程式中起始行為。 在此情況下,您通常不希望其他元素處理來自該指標動作的任何其他事件,直到您的行為完成或釋放指標擷取來取消為止。 如果擷取指標,則只有已擷取的專案會取得指標的輸入事件,而其他元素也不會引發事件,即使指標移至其界限也一樣。 例如,請考慮具有兩個相鄰元素的 UI。 一般而言,如果您將指標從某個元素移到另一個專案,您必須先從第一個專案取得 PointerMoved 事件,然後再從第二個專案取得。 但是,如果第一個專案已擷取指標,則即使擷取的指標離開其界限,第一個專案仍會繼續接收 PointerMoved 事件。 此外,當擷取的指標進入指標時,第二個專案不會引發擷取指標的 PointerEntered 事件。

指標擷取狀態,並產生與指標擷取相關的事件,完全不是應用程式程式碼。 如果使用者放開指標,則會產生 PointerReleased 事件,且指標擷取與該指標相關聯的指標會遺失。 這也會在原始擷取專案上引發 PointerCaptureLost

在大部分情況下,當使用者完成輸入動作以釋放先前指標擷取的輸入動作時,會自動釋放指標擷取, (移動觸控點、釋放滑鼠左鍵、將手寫筆從範圍) 。 另一個可能釋放擷取的條件是也會引發 PointerCanceled 事件的任何動作。 您的應用程式通常依賴與使用者輸入動作相關聯的擷取發行行為,而不需要特別使用 ReleasePointerCaptureReleasePointerCaptures取消指標擷取。 如需詳細資訊,請參閱互動

如果已擷取指標,CapturePointer 方法會傳回 false

UIElement可以一次擷取多個指標點。 使用 value 參數來指出您想要擷取的 指標 實例。

代表手勢的輸入事件 (,例如 TappedDoubleTapped) 通常會在放開指標之後引發,因此您不應該嘗試在事件處理常式中擷取手勢事件的指標。 不允許筆勢事件的事件資料中的 指標 參考起始指標擷取。

提示

請勿嘗試在指標相關輸入事件處理常式的範圍內使用 CapturePointer。 除非您有一個您確定的 指標 與允許在該時間有指標擷取的指標相關聯,否則您的 CapturePointer 呼叫不會有任何作用。 沒有實際的方法可以產生新的 指標 ,並使用新的指標呼叫 CapturePointer。 您應該只使用系統透過指標相關輸入事件的事件資料,提供給系統的 指標 參考。

適用於

另請參閱