Limit Mouse

April 26, 2008

Option Explicit

Private Type RECT
   left                 As Integer
   top                  As Integer
   right                As Integer
   bottom               As Integer
End Type

Private Type POINT
   x                    As Long
   y                    As Long
End Type

Private Declare Sub ClipCursor Lib “user32″ (lpRect As Any)
Private Declare Sub GetClientRect Lib “user32″ (ByVal hWnd As _
      Long, lpRect As RECT)
Private Declare Sub ClientToScreen Lib “user32″ (ByVal hWnd As _
      Long, lpPoint As POINT)
Private Declare Sub OffsetRect Lib “user32″ (lpRect As RECT, _
       ByVal x As Long, ByVal y As Long)
Public Sub LimitCursorMovement(ctl As Object)
  
   Dim client           As RECT
   Dim upperleft        As POINT
   Dim lHwnd As Long
   On Error Resume Next
   lHwnd = ctl.hWnd
   If lHwnd = 0 Then Exit Sub
   GetClientRect ctl.hWnd, client
   upperleft.x = client.left
   upperleft.y = client.top
   ClientToScreen ctl.hWnd, upperleft
   OffsetRect client, upperleft.x, upperleft.y
   ClipCursor client
End Sub

Public Sub ReleaseLimit()
   ‘Releases the cursor limits
   ‘Be sure to call on unloading the form
   ClipCursor ByVal 0&
End Sub

Private Sub cmdNormal_Click()
  ReleaseLimit
End Sub

Private Sub cmdSetLimit_Click()
  LimitCursorMovement Me
End Sub

Private Sub Form_Load()
  ReleaseLimit
 
End Sub

Private Sub Form_Unload(Cancel As Integer)
  ReleaseLimit
End Sub