Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: VB4/32,VB5,VB6
Expertise: Intermediate
Jun 24, 2000



Building the Right Environment to Support AI, Machine Learning and Deep Learning

ClipMouseToWindow - Confine the cursor to a window's client area

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
        ' unclip the mouse if no argument has been passed
        ClipCursor ByVal 0&
    End If
End Sub
Marco Bellinaso
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date