ClipMouse




Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Private Declare Function GetClientRect Lib "user32" (ByVal hWnd As Long, _
lpRect As RECT) As Long
Private Declare Function ClientToScreen Lib "user32" (ByVal hWnd As Long, _
lpPoint As Any) As Long
Private Declare Function ClipCursor Lib "user32" (lpRect As Any) As Long

' confine (clip) the mouse's cursor to the client

' area of a given window or control.

' if the argument is omitted, any current clipping is canceled


Sub ClipMouseToWindow(Optional ByVal hWnd As Long)
Dim rcTarg As RECT

If hWnd Then
' clip the mouse to the specified window

' get the window's client area

GetClientRect hWnd, rcTarg
' convert to screen coordinates. Two steps:

' first, the upper-left corner

ClientToScreen hWnd, rcTarg
' next, the bottom-right corner

ClientToScreen hWnd, rcTarg.Right
' finally, we can clip the cursor

ClipCursor rcTarg
Else
' unclip the mouse if no argument has been passed

ClipCursor ByVal 0&
End If
End Sub











( clipmouse.html )- by Paolo Puglisi - Modifica del 17/12/2023