WPF Detect mouse move?
Well im trying to make my own basic Slider control just so i can learn abit how to interact with the mouse...
so far i have this:
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"/>
so this works while the mouse is on the rectangle.开发者_运维技巧. but if we look how the slider behaves its not the same you click and hold the mouse down and can drag it any where even outside of the window and it still updates its position..
So how is this done? my guess is its the Mouse Class but i don't understand how to use it.
Try using the Mouse.Capture method.
You pass it a reference to your control and:
When an element captures the mouse, it receives mouse input whether or not the cursor is within its borders.
If a CaptureMode is not specified, the default CaptureMode is Element.
Take note of the CaptureMode overload. This enumeration can help if your capturing with sub elements, etc.
精彩评论