-
2
- #1
Let's say you have a form in restore mode. You don't want the user to move their mouse outside your form's boundary. You can use the following code to constrain the mouse movement to only the form: ( to test it, create two command buttons and paste the appropriate code from below)( Also, when you do constrain the mouse, you must turn off the constraint or it will stay in that "boundary" even after the form is closed. Notice the code for Unload)
Option Explicit
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function ClipCursor Lib "user32" _
(lpRect As Any) As Long
Private Declare Function GetWindowRect Lib "user32" _
(ByVal hwnd As Long, lpRect As RECT) As Long
Private rc As RECT
Private Sub Command1_Click()
'Constrain mouse inside form
Call GetWindowRect(Me.hwnd, rc)
Call ClipCursor(rc)
End Sub
Private Sub Command2_Click()
'Free mouse
Call ClipCursor(ByVal 0)
End Sub
Private Sub Form_Unload(Cancel As Integer)
Call ClipCursor(ByVal 0)
End Sub
Neil
Option Explicit
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function ClipCursor Lib "user32" _
(lpRect As Any) As Long
Private Declare Function GetWindowRect Lib "user32" _
(ByVal hwnd As Long, lpRect As RECT) As Long
Private rc As RECT
Private Sub Command1_Click()
'Constrain mouse inside form
Call GetWindowRect(Me.hwnd, rc)
Call ClipCursor(rc)
End Sub
Private Sub Command2_Click()
'Free mouse
Call ClipCursor(ByVal 0)
End Sub
Private Sub Form_Unload(Cancel As Integer)
Call ClipCursor(ByVal 0)
End Sub
Neil