# ClipCursor Function

`Declare Function ClipCursor Lib "user32.dll" (lpRect As RECT) As Long`

ClipCursor confines the mouse cursor to a rectangular area of the screen. If the user tries to move the cursor outside of this bounding region or a call to SetCursorPos tells it to go outside the box, the cursor will immediately returned to the area. There is no way to get it out. This bounding effect will last in whatever program you switch to, and will remain even if the program that confined the cursor closes! The only way to "release" the cursor is to "confine" it to the entire screen (see example). It isn't usually a good idea to confine the cursor, since the user expects to move the cursor anywhere (not to mention the disasterous effect if your program quit before releasing the cursor!). The return value is 0 if an error occured, or non-zero if it is successful.

lpRect
  The rectangle (in screen coordinates) defining the confinement rectangle.

Example:

    Dim r As RECT
    ' This code confines the cursor to the inside of Form1
    x = GetWindowRect(Form1.hWnd, r)  ' API puts window coords into RECT
    x = ClipCursor(r)  ' Confine the cursor
    ' This code releases the cursor
    deskhWnd = GetDesktopWindow()  ' API gets desktop's handle
    x = GetWindowRect(deskhWnd, r)  ' API puts window coords into RECT
    x = ClipCursor(r)  ' "Confine" the cursor to the entire screen.

Related Call: GetClipCursor
Category: Mouse
Back to the index.