Windows 窗体应用程序中的鼠标拖拽事件处理

鼠标按下事件处理

当鼠标按下时,记录鼠标的 X 坐标,并改变光标形状为手形。

Private Sub P_MouseDown(sender As Object, e As MouseEventArgs)
    mdx = e.X
    Cursor.Current = Cursors.Hand
End Sub

鼠标移动事件处理

当鼠标移动时,如果按下了鼠标左键,则计算击球板新的 X 坐标,并判断是否超出窗体边界。如果超出,则限制在边界内。

Private Sub P_MouseMove(sender As Object, e As MouseEventArgs)
    If e.Button = MouseButtons.Left Then
        Dim x As Integer = P.Left + (e.X - mdx)
        If x < 0 xss=removed> Me.ClientSize.Width - P.Width Then x = Me.ClientSize.Width - P.Width
        P.Left = x
    End If
End Sub
ppt 文件大小:2.53MB