PointerRoutedEventArgs 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
包含最後一個指標事件訊息所傳回的引數。
public ref class PointerRoutedEventArgs sealed : RoutedEventArgs
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class PointerRoutedEventArgs final : RoutedEventArgs
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public sealed class PointerRoutedEventArgs : RoutedEventArgs
Public NotInheritable Class PointerRoutedEventArgs
Inherits RoutedEventArgs
- 繼承
- 屬性
Windows 需求
裝置系列 |
Windows 10 (已於 10.0.10240.0 引進)
|
API contract |
Windows.Foundation.UniversalApiContract (已於 v1.0 引進)
|
範例
下列程式碼範例顯示 輸入範例中的案例 2。 此程式碼示範使用PointerPressed、PointerReleased、PointerEntered、PointerExited和PointerMoved事件直接操作的一些使用模式。
<StackPanel x:Name="Scenario2Output" ManipulationMode="All">
<StackPanel Orientation="Horizontal" Margin="0,10,0,0">
<Button x:Name="scenario2Reset" Content="Reset"
Margin="0,0,10,0" Click="Scenario2Reset"/>
</StackPanel>
<StackPanel Orientation="Horizontal" >
<ToggleSwitch x:Name="tbPointerCapture"
Header="Pointer Capture" FontSize="20"/>
<TextBlock x:Name="txtCaptureStatus"
Style="{StaticResource BasicTextStyle}"/>
</StackPanel>
<Border x:Name="bEnteredExited" Background="Red"
Height="300" Width="450" CornerRadius="20"
HorizontalAlignment="Left">
<Grid>
<TextBlock Style="{StaticResource BasicTextStyle}" Text=""
HorizontalAlignment="Center" VerticalAlignment="Center"
x:Name="bEnteredExitedTextBlock"/>
<Ellipse VerticalAlignment="Bottom" Stroke="Silver"
StrokeDashArray="2,2" StrokeThickness="2" Margin="2"
x:Name="bEnteredExitedTimer" Width="20" Height="20"
RenderTransformOrigin="0.5,0.5">
<Ellipse.RenderTransform >
<RotateTransform Angle="0" />
</Ellipse.RenderTransform>
</Ellipse>
</Grid>
</Border>
</StackPanel>
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
備註
在大部分情況下,建議您透過所選語言架構中指標事件處理常式的事件引數取得指標資訊, (使用 JavaScript、使用 C++、C# 或 Visual Basic 的 UWP 應用程式,或使用 DirectX 搭配 C++ 的 UWP 應用程式) 。
如果事件引數未以內部方式公開應用程式所需的指標詳細資料,您可以透過 PointerRoutedEventArgs 的 GetCurrentPoint 和 GetIntermediatePoints 方法來存取擴充指標資料。 使用這些方法來指定指標資料的內容。
靜態 PointerPoint 方法 GetCurrentPoint 和 GetIntermediatePoints一律使用應用程式的內容。 PointerRoutedEventArgs 事件資料類別會用於下列事件:
- PointerPressed
- PointerCanceled
- PointerCaptureLost
- PointerEntered
- PointerExited
- PointerMoved
- PointerReleased
- PointerWheelChanged
重要
滑鼠輸入會與第一次偵測到滑鼠輸入時指派的單一指標相關聯。 按一下滑鼠按鈕 (左鍵、滾輪或右鍵) 會透過 PointerPressed 事件建立指標與該按鈕的次要關聯。 只在放開相同的滑鼠按鈕時才會觸發 PointerReleased 事件 (這個事件完成前,沒有其他按鈕可以與該指標關聯)。 由於這個專屬關聯的關係,其他滑鼠按鈕的按一下都會經由 PointerMoved 事件進行路由。 您可以在處理此事件時測試滑鼠按鍵狀態,如下列範例所示。
private void Target_PointerMoved(object sender, PointerRoutedEventArgs e)
{
Windows.UI.Xaml.Input.Pointer ptr = e.Pointer;
// Multiple, simultaneous mouse button inputs are processed here.
// Mouse input is associated with a single pointer assigned when
// mouse input is first detected.
// Clicking additional mouse buttons (left, wheel, or right) during
// the interaction creates secondary associations between those buttons
// and the pointer through the pointer pressed event.
// The pointer released event is fired only when the last mouse button
// associated with the interaction (not necessarily the initial button)
// is released.
// Because of this exclusive association, other mouse button clicks are
// routed through the pointer move event.
if (ptr.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Mouse)
{
// To get mouse state, we need extended pointer details.
// We get the pointer info through the getCurrentPoint method
// of the event argument.
Windows.UI.Input.PointerPoint ptrPt = e.GetCurrentPoint(Target);
if (ptrPt.Properties.IsLeftButtonPressed)
{
eventLog.Text += "\nLeft button: " + ptrPt.PointerId;
}
if (ptrPt.Properties.IsMiddleButtonPressed)
{
eventLog.Text += "\nWheel button: " + ptrPt.PointerId;
}
if (ptrPt.Properties.IsRightButtonPressed)
{
eventLog.Text += "\nRight button: " + ptrPt.PointerId;
}
}
// Prevent most handlers along the event route from handling the same event again.
e.Handled = true;
// Display pointer details.
updateInfoPop(e);
}
private void Target_PointerMoved(object sender, PointerRoutedEventArgs e)
{
Windows.UI.Xaml.Input.Pointer ptr = e.Pointer;
// Multiple, simultaneous mouse button inputs are processed here.
// Mouse input is associated with a single pointer assigned when
// mouse input is first detected.
// Clicking additional mouse buttons (left, wheel, or right) during
// the interaction creates secondary associations between those buttons
// and the pointer through the pointer pressed event.
// The pointer released event is fired only when the last mouse button
// associated with the interaction (not necessarily the initial button)
// is released.
// Because of this exclusive association, other mouse button clicks are
// routed through the pointer move event.
if (ptr.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Mouse)
{
// To get mouse state, we need extended pointer details.
// We get the pointer info through the getCurrentPoint method
// of the event argument.
Windows.UI.Input.PointerPoint ptrPt = e.GetCurrentPoint(Target);
if (ptrPt.Properties.IsLeftButtonPressed)
{
eventLog.Text += "\nLeft button: " + ptrPt.PointerId;
}
if (ptrPt.Properties.IsMiddleButtonPressed)
{
eventLog.Text += "\nWheel button: " + ptrPt.PointerId;
}
if (ptrPt.Properties.IsRightButtonPressed)
{
eventLog.Text += "\nRight button: " + ptrPt.PointerId;
}
}
// Prevent most handlers along the event route from handling the same event again.
e.Handled = true;
// Display pointer details.
updateInfoPop(e);
}
- 傳送者 (的值,其位於委派簽章上,而不是這個事件資料類別) 。
- PointerRoutedEventArgs 的特定成員,例如 KeyModifiers 或 GetCurrentPoint。
- 指標裝置描述類別的值。 從 Pointer 屬性取得 指標 。
- 來自 PointerPoint系統輸入概念化的成員。 使用 GetCurrentPoint API 取得 PointerPoint 值,然後呼叫 PointerPoint API,例如 Position 和 PointerPointProperties。
特定事件通常具有各種指標裝置和指標點類別中的資訊,主要與該事件有關。 例如,當您處理PointerWheelChanged時,您可能會對PointerPointProperties中的MouseWheelDelta感興趣。
GetCurrentPoint 和 GetIntermediatePoints 方法所擷取的物件可透過 Properties 屬性存取擴充指標資訊,以取得 PointerPointProperties 物件。
在下列範例中,我們會透過 PointerPoint 和PointerPointProperties物件取得擴充指標屬性。 (請參閱 快速入門: 完整範例的指標。)
String queryPointer(PointerPoint ptrPt)
{
String details = "";
switch (ptrPt.PointerDevice.PointerDeviceType)
{
case Windows.Devices.Input.PointerDeviceType.Mouse:
details += "\nPointer type: mouse";
break;
case Windows.Devices.Input.PointerDeviceType.Pen:
details += "\nPointer type: pen";
if (ptrPt.IsInContact)
{
details += "\nPressure: " + ptrPt.Properties.Pressure;
details += "\nrotation: " + ptrPt.Properties.Orientation;
details += "\nTilt X: " + ptrPt.Properties.XTilt;
details += "\nTilt Y: " + ptrPt.Properties.YTilt;
details += "\nBarrel button pressed: " + ptrPt.Properties.IsBarrelButtonPressed;
}
break;
case Windows.Devices.Input.PointerDeviceType.Touch:
details += "\nPointer type: touch";
details += "\nrotation: " + ptrPt.Properties.Orientation;
details += "\nTilt X: " + ptrPt.Properties.XTilt;
details += "\nTilt Y: " + ptrPt.Properties.YTilt;
break;
default:
details += "\nPointer type: n/a";
break;
}
GeneralTransform gt = Target.TransformToVisual(page);
Point screenPoint;
screenPoint = gt.TransformPoint(new Point(ptrPt.Position.X, ptrPt.Position.Y));
details += "\nPointer Id: " + ptrPt.PointerId.ToString() +
"\nPointer location (parent): " + ptrPt.Position.X + ", " + ptrPt.Position.Y +
"\nPointer location (screen): " + screenPoint.X + ", " + screenPoint.Y;
return details;
}
一般而言,這個方法所傳回的物件是用來將指標資料摘要至 GestureRecognizer。 另一個案例是取得PointerWheelChanged事件的MouseWheelDelta;該值位於PointerPointProperties中。
版本歷程記錄
Windows 版本 | SDK 版本 | 新增值 |
---|---|---|
1709 | 16299 | IsGenerated |
屬性
Handled |
取得或設定值,這個值會將路由事件標示為已處理,並防止事件路由中的大部分處理常式再次處理相同的事件。 |
IsGenerated |
取得值,這個值表示指標事件是從使用者與物件直接互動所發生,還是由平臺根據應用程式的 UI 變更而產生。 |
KeyModifiers |
取得值,這個值表示起始指標事件時,哪些索引鍵修飾詞作用中。 |
OriginalSource |
取得引發事件之 物件的參考。 這通常是控制項的範本部分,而不是在應用程式 UI 中宣告的專案。 (繼承來源 RoutedEventArgs) |
Pointer |
取得指標標記的參考。 |
方法
GetCurrentPoint(UIElement) |
擷取 PointerPoint 物件,提供與事件相關聯之指標的基本資訊。 |
GetIntermediatePoints(UIElement) |
擷取 PointerPoint 物件的集合,這些物件代表從最後一個指標事件到 目前指標事件為止的指標歷程記錄。 集合中的每個PointerPoint都會提供與事件相關聯之指標的基本資訊。集合中的最後一個專案相當於GetCurrentPoint所傳回的PointerPoint物件。 |