Visual Basic Helper Routines
ClipCursor: Restrict the Mouse to a Form's Client Area
Posted:   Saturday February 12, 2000
Updated:   Monday December 26, 2011
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows NT4
OS restrictions:   None
Author:   VBnet - Randy Birch

This is a straightforward method to prevent the mouse cursor from leaving the current form. When activated, the mouse can not be moved outside the form (see caveats below), nor can the titlebar or other non-client area controls thereon be accessed.

The caveat to this call is that under 32-bit operating systems the user remains free to activate other widows outside the application via the keyboard. Should another app gain focus, upon regaining focus the form that had previously set the ClipCursor will no longer restrict the mouse to its client area. As a workaround to this condition, again for justified uses only, you may want to add the subclassing code from WM_ACTIVATEAPP: Detect Application Activation State in order to detect the form's activation when moving from another application in order to reset the restriction on regaining focus.

While it is strongly recommended that this practice be avoided for general use, specific situations it may warrant its limited use. Just remember that it is considered very bad programming practice to restrict or otherwise play with the rodent cursor. Not to mention that its bound to tick off the user.

 BAS Module Code

 Form Code
The demo shown in the illustration used two forms: the bottom one (Form1) holds only a simple Form2.Show statement in its Command1_Click event. The code for Form1 is not provided here.

The front form, Form2, requires only a command button (Command1) to close the form, as the code restricting the cursor is called in Form2's Activate event.

Option Explicit
' Copyright 1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.

Private Type RECT
   left   As Long
   top    As Long
   right  As Long
   bottom As Long
End Type

Private Type POINT
   x   As Long
   y   As Long
End Type

Private Declare Function ClipCursor Lib "user32" _
  (lpRect As Any) As Long

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 POINT) As Long
Private Declare Function OffsetRect Lib "user32" _
  (lpRect As RECT, _
   ByVal x As Long, _
   ByVal y As Long) As Long

Private Sub Command1_Click()

   Unload Me
End Sub

Private Sub Form_Unload(Cancel As Integer)

  'Release the cursor restriction
   ClipCursor ByVal 0&
End Sub

Private Sub Form_Activate()

  'justOnce is used to prevent re-calling the API
  'if another form is given focus. Removing the If..
  'condition will reset the cursor restriction ONLY
  'if the window that gained focus (via the
  'keyboard/task list etc) is another form within the
  'same project. If the clipped form has focus and
  'another window (outside the project) is activated, 
  'on regaining focus the clipped form will not
  'reset the clipping region because the form's
  'Activate event is only called when activating forms
  'within the application.  
   Static justOnce As Boolean
   Dim rc As RECT
   Dim topcorner As POINT
   If Not justOnce Then
      justOnce = True
      GetClientRect Me.hwnd, rc
      topcorner.x = rc.left
      topcorner.y =
      If ClientToScreen(Me.hwnd, topcorner) <> 0 Then
         If OffsetRect(rc, topcorner.x, topcorner.y) <> 0 Then
            Call ClipCursor(rc)
            Caption = "Cursor is Trapped!"
         End If
      End If
   End If
End Sub
Simple enough, eh?


PayPal Link
Make payments with PayPal - it's fast, free and secure!


Copyright 1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy


Hit Counter