Option Strict On
Option Explicit On
Imports Microsoft.DirectX
Imports Microsoft.DirectX.DirectInput
Public Class clsDIMouse
Private frmTarget As Form
Private devMouse As Device
Private lMouseX As Long
Private lMouseY As Long
Public Sub New(ByVal frm As Form)
frmTarget = frm
devMouse = New Device(SystemGuid.Mouse)
devMouse.SetDataFormat(DeviceDataFormat.Mouse)
devMouse.SetCooperativeLevel(frm, CooperativeLevelFlags.Foreground Or CooperativeLevelFlags.Exclusive)
devMouse.Acquire()
End Sub
Private Function State() As MouseState
Try
Return devMouse.CurrentMouseState
Catch
Try
devMouse.Acquire()
Catch ex As InputException
If TypeOf ex Is OtherApplicationHasPriorityException Or TypeOf ex Is NotAcquiredException Then 'check what type of input exception occured
Return Nothing
End If
End Try
End Try
End Function
Public ReadOnly Property MouseX() As Long
Get
lMouseX = State.X
Return lMouseX
End Get
End Property
Public ReadOnly Property MouseY() As Long
Get
lMouseY += State.Y
Return lMouseY
End Get
End Property
Public ReadOnly Property LeftClick() As Boolean
Get
Dim temp As Byte() = State.GetMouseButtons()
If temp(0) <> 0 Then
Return True
Else
Return False
End If
End Get
End Property
Public ReadOnly Property RightClick() As Boolean
Get
Dim temp As Byte() = State.GetMouseButtons()
If temp(1) <> 0 Then
Return True
Else
Return False
End If
End Get
End Property
Public Function free() As Boolean
Try
devMouse.Unacquire()
devMouse.Dispose()
devMouse = Nothing
Return True
Catch
Return False
End Try
End Function
End Class
Public Function GoToMouse(ByVal sprPosX As Double, ByVal sprPosY As Double, ByVal sprPosSpeed As Double, ByVal mouseX As Double, ByVal mouseY As Double, ByRef MoveX As Double, ByRef MoveY As Double) As Boolean
Try
Dim xDiff As Double = CDbl(mouseX - sprPosX)
Dim ydiff As Double = CDbl(mouseY - sprPosY)
Dim Length As Double = xDiff * xDiff + ydiff * ydiff
Length = Sqrt(Length)
MoveX = (sprPosX - mouseX) / Length * sprPosSpeed
MoveY = (sprPosY - mouseY) / Length * sprPosSpeed
Catch
End Try
End Function