鼠标拖拽过程

单击鼠标时    

 '得到需要拖拽的对象 ,修改此处   

Dim item As Rectangle = sender
        '获取鼠标当前位置
        mouseVerticalPosition = e.GetPosition(Nothing).Y
        mouseHorizontalPosition = e.GetPosition(Nothing).X
        '启用鼠标捕获
        isMouseCaptured = True
        '将鼠标捕获设置为 UIElement
        item.CaptureMouse()

 

移动鼠标时

 

松开鼠标时

 

 

Private Sub RectAClick(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
        '若已经处理过, 则不处理
        e.Handled = True
        Me.statusTextBlock.Text = "x:y=" & e.GetPosition(Me.ParentCanvas).ToString & ".RectA is Click,not BubbleUp"
    End Sub

    Dim isMouseCaptured As Boolean
    Dim mouseVerticalPosition As Double
    Dim mouseHorizontalPosition As Double

    Private Sub Rect_Handle_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
        Dim item As Rectangle = sender
        '获取鼠标当前位置
        mouseVerticalPosition = e.GetPosition(Nothing).Y
        mouseHorizontalPosition = e.GetPosition(Nothing).X
        '启用鼠标捕获
        isMouseCaptured = True
        '将鼠标捕获设置为 UIElement
        item.CaptureMouse()
    End Sub

    Private Sub Rect_Handle_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseEventArgs)
        Dim item As Rectangle = sender
        If (isMouseCaptured) Then
            ' 计算对象的当前位置
            Dim deltaV As Double = e.GetPosition(Nothing).Y - mouseVerticalPosition
            Dim deltaH As Double = e.GetPosition(Nothing).X - mouseHorizontalPosition
            Dim newTop As Double = deltaV + item.GetValue(Canvas.TopProperty)
            Dim newLeft As Double = deltaH + item.GetValue(Canvas.LeftProperty)

            ' 设置对象新位置
            item.SetValue(Canvas.TopProperty, newTop)
            item.SetValue(Canvas.LeftProperty, newLeft)

            ' Update position global variables.
            mouseVerticalPosition = e.GetPosition(Nothing).Y
            mouseHorizontalPosition = e.GetPosition(Nothing).X
        End If

    End Sub

    Private Sub Rect_Handle_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
        Dim item As Rectangle = sender
        isMouseCaptured = False
        item.ReleaseMouseCapture()
        mouseVerticalPosition = -1
        mouseHorizontalPosition = -1
    End Sub

 

 

 

转载于:https://www.cnblogs.com/wmt1708/archive/2009/03/22/1418734.html

你可能感兴趣的:(鼠标拖拽过程)