How to Limit Cursor Movement to Form Boundaries
Posted on January 5, 2009
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | 'System & API - How to Limit Cursor Movement to Form Boundaries Option Explicit Public Type RECT left As Integer top As Integer right As Integer bottom As Integer End Type Public Type POINT x As Long y As Long End Type Declare Sub ClipCursor Lib "user32" (lpRect As Any) Declare Sub GetClientRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) Declare Sub ClientToScreen Lib "user32" (ByVal hWnd As Long, lpPoint As POINT) Declare Sub OffsetRect Lib "user32" (lpRect As RECT, ByVal x As Long, ByVal y As Long) 'Add two Command Buttons to your form and set its caption as "Limit Cursor Movement", "Release Limit" Private Sub Command1_Click() Dim client As RECT Dim upperleft As POINT GetClientRect Me.hWnd, client upperleft.x = client.left upperleft.y = client.top ClientToScreen Me.hWnd, upperleft OffsetRect client, upperleft.x, upperleft.y ClipCursor client End Sub Private Sub Command2_Click() ClipCursor ByVal 0& End Sub Private Sub Form_Unload(Cancel As Integer) 'If you won't put the code line below in the form's unload event, the cursor will 'be still limited to the last coordinates after the program unloaded. ClipCursor ByVal 0& End Sub |