The new vbAccelerator Site - more VB and .NET Code and Controls

Restrict Mouse Movement to an Area of the Desktop


Steve McMahon(





Other Tips
All Tips
By Date
By Subject

API (33)
Manipulation (3)

Clipboard (3)
Box (5)

Desktop (3)
GDI (13)
Graphics (13)
Internet (2)
Comms (3)

Keyboard (2)
Mouse (1)
Shell (1)
Sprites (1)
Subclassing (3)
Box (2)

Windows (11)
Controls (10)


This tip shows you how to ensure a cursor remains within a certain rectangle on the screen. Note that if the user uses Alt-Tab to switch to another application, the clipping cursor is cleared.

Start a new project in VB. Add a new module, and add the following code:

' API Declarations:
Private Type POINTAPI
&nbsp &nbsp X As Long
&nbsp &nbsp Y As Long
End Type
Private Type RECT
&nbsp &nbsp eft As Long
&nbsp &nbsp op As Long
&nbsp &nbsp ight As Long
&nbsp &nbsp ottom As Long
End Type
Private Declare Sub ClipCursorRect Lib "user32" Alias "ClipCursor" (lpRect As RECT)
Private Declare Sub ClipCursorClear Lib "user32" Alias "ClipCursor" (ByVal lpRect As Long)
Private Declare Function ClientToScreen Lib "user32" (ByVal hWnd As Long, lpPoint As POINTAPI) As Long

Public Sub RestrictCursor( _
&nbsp &nbsp &nbsp &nbsp ByVal lLeft As Long, _
&nbsp &nbsp &nbsp &nbsp ByVal lTop As Long, _
&nbsp &nbsp &nbsp &nbsp ByVal lWidth As Long, _
&nbsp &nbsp &nbsp &nbsp ByVal lHeight As Long, _
&nbsp &nbsp &nbsp &nbsp Optional ByRef oPositionTo As Object = Nothing _
&nbsp &nbsp )
Dim tR As RECT
&nbsp &nbsp
&nbsp &nbsp
' Convert positions into a rectangle in pixels:
&nbsp &nbsp tR.Left = lLeft \ Screen.TwipsPerPixelX
&nbsp &nbsp tR.Top = lTop \ Screen.TwipsPerPixelY
&nbsp &nbsp tR.Right = (lLeft + lWidth) \ Screen.TwipsPerPixelX
&nbsp &nbsp tR.Bottom = (lLeft + lHeight) \ Screen.TwipsPerPixelY
&nbsp &nbsp
&nbsp &nbsp
' Validate optional parameter:
&nbsp &nbsp If oPositionTo Is Nothing Then Set oPositionTo = Screen
&nbsp &nbsp
' If positions refer to an form or control, then
&nbsp &nbsp ' convert the coordinates to the screen position:
&nbsp &nbsp If Not oPositionTo Is Screen Then
&nbsp &nbsp &nbsp &nbsp tP.X = tR.Left
&nbsp &nbsp &nbsp &nbsp tP.Y = tR.Top
&nbsp &nbsp &nbsp &nbsp ClientToScreen oPositionTo.hWnd, tP
&nbsp &nbsp &nbsp &nbsp tR.Left = tP.X
&nbsp &nbsp &nbsp &nbsp tR.Top = tP.Y
&nbsp &nbsp &nbsp &nbsp tP.X = tR.Right
&nbsp &nbsp &nbsp &nbsp tP.Y = tR.Bottom
&nbsp &nbsp &nbsp &nbsp ClientToScreen oPositionTo.hWnd, tP
&nbsp &nbsp &nbsp &nbsp tR.Right = tP.X
&nbsp &nbsp &nbsp &nbsp tR.Bottom = tP.Y
&nbsp &nbsp End If
&nbsp &nbsp
&nbsp &nbsp
' Set the cursor clipping rectangle:
&nbsp &nbsp ClipCursorRect tR
&nbsp &nbsp
End Sub
Public Sub ClearRestrictCursor()
&nbsp &nbsp ClipCursorClear 0
End Sub

Now you can create a test project. Add a Picture Box to the form in your project, and draw a Check Box inside the Picture Box (note: you must put the check box inside the picture otherwise you won't be able to get at it whilst the cursor is being restricted!).

Add the following code to the Check box click event:

Private Sub Check1_Click()
&nbsp &nbsp If (Check1.Value = Checked) Then
&nbsp &nbsp &nbsp &nbsp
' Restrict the cursor so it can't move
&nbsp &nbsp &nbsp &nbsp ' out of the picture:
&nbsp &nbsp &nbsp &nbsp RestrictCursor 0, 0, Picture1.Width, Picture1.Height, Picture1
&nbsp &nbsp Else
&nbsp &nbsp &nbsp &nbsp
' Stop restricting the cursor:
&nbsp &nbsp &nbsp &nbsp ClearRestrictCursor
&nbsp &nbsp End If
End Sub

Run the project. You will find that when you click the Check Box you can't move the mouse pointer outside the Picture Box.


Related Tips and Articles:



AboutContributeSend FeedbackPrivacy

Copyright 1998-1999, Steve McMahon ( All Rights Reserved.
Last updated: 01/08/98