簡體   English   中英

WPF檢測鼠標移動?

[英]WPF Detect mouse move?

好吧,我試圖做我自己的基本Slider控件,以便我可以學一點點如何與鼠標進行交互...

到目前為止,我有這個:

vb.net:

Private Sub Rectangle_MouseMove(ByVal sender as Object, ByVal e as System.Windows.Input.MouseEventArgs)

If (e.LeftButton = MouseButtonState.Pressed) Then
    Dim p As Point = Mouse.GetPosition(Me)
    Rectangle.SetValue(FrameworkElement.MarginProperty, New Thickness(p.X - Rectangle.Width / 2, 0, 0, 0))
End If
End Sub

Private Sub Rectangle_MouseDown(ByVal sender as Object, ByVal e as System.Windows.Input.MouseButtonEventArgs)

End Sub

Private Sub Rectangle_MouseUp(ByVal sender as Object, ByVal e as System.Windows.Input.MouseButtonEventArgs)

End Sub

xaml:

<Rectangle x:Name="Rectangle" Fill="White" Stroke="Black" HorizontalAlignment="Left" Margin="0" VerticalAlignment="Top" Width="100" Height="75" MouseMove="Rectangle_MouseMove" MouseDown="Rectangle_MouseDown" MouseUp="Rectangle_MouseUp"/>

因此,這在鼠標位於矩形上時有效。.但是,如果我們看一下滑塊的行為,則可以單擊並按住鼠標,並將其拖動到窗口外的任何位置,並且仍會更新其位置。

那怎么辦呢? 我的猜測是它的鼠標類,但我不知道如何使用它。

嘗試使用Mouse.Capture方法。

您將其傳遞給控件的引用,並:

當元素捕獲鼠標時,無論光標是否在其邊界內,它都會接收鼠標輸入。

如果未指定CaptureMode,則默認的CaptureMode為Element。

注意CaptureMode重載。 如果您使用子元素等進行捕獲,則此枚舉可能會有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM