Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Constraining mouse movement to within a form's border 2

Status
Not open for further replies.

fneily

Instructor
Apr 5, 2002
2,538
US
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
 
Awesomeeee!!!!
Cool
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top