ClipCursor Function

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

Platforms

Description & Usage

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!).

Return Value

If an error occured, the function returns zero (call GetLastError to get the error code). If successful, the function returns a non-zero value.

Visual Basic-Specific Issues

None.

Parameters

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

Example

' This code is licensed according to the terms and conditions listed here.

' Confine the cursor temporarily to inside of Form1

' ** Place the following code where you want to confine the cursor. **
Dim r As RECT  ' confinement rectangle
Dim retval As Long  ' return value

retval = GetWindowRect(Form1.hWnd, r)  ' put window's coordinates into r
retval = ClipCursor(r)  ' confine the cursor

' ** Place the following code where you want to release the cursor. **
Dim r As RECT, retval As Long
Dim deskhWnd As Long  ' the handle of the desktop window

deskhWnd = GetDesktopWindow()  ' get handle of the desktop window
retval = GetWindowRect(deskhWnd, r)  ' put window's coordinates into r
retval = ClipCursor(r)  ' "confine" the cursor to the entire screen

See Also

GetClipCursor

Category

Cursor

Go back to the alphabetical Function listing.
Go back to the Reference section index.


Last-Modified: September 10, 1999
This page is copyright © 1999 Paul Kuliniewicz. Copyright Information Revised October 29, 2000
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/c/clipcursor.html