Jason Lee Hayes
Active member
- Local time
- Today, 13:53
- Joined
- Jul 25, 2020
- Messages
- 224
I'm aware this cannot be done normally but using API is it possible to limit the mouse pointer to move only within the boundary of a form?
I have been using my best friend Google and found the below:-
Can this be adapted to be used as a module so i can call it from a form open?
I don't need to button caption; just a simple ClipCussor= True , ClipCursor False king of thing...
Dim client As RECT
Dim upperleft As POINT
Dim hWnd As Long
'Get Userform handle
hWnd = FindWindow(vbNullString, Me.Caption)
'Get information about our window
GetClientRect hWnd, client
upperleft.x = client.left
upperleft.y = client.top
'Make the bottom and right the same as the top/left
' client.bottom = client.top
' client.right = client.left
'Convert window coordinates to screen coordinates
ClientToScreen hWnd, upperleft
'offset our rectangle
OffsetRect client, upperleft.x, upperleft.y
'limit the cursor movement
ClipCursor client
End Sub
Private Sub CommandButton2_Click()
'Releases the cursor limits
ClipCursor ByVal 0&
End Sub
Private Sub UserForm_Activate()
CommandButton1.Caption = "Limit Cursor Movement"
CommandButton2.Caption = "Release Limit"
End Sub
Private Sub UserForm_Click()
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
'Releases the cursor limits
ClipCursor ByVal 0&
End Sub
I have been using my best friend Google and found the below:-
Can this be adapted to be used as a module so i can call it from a form open?
I don't need to button caption; just a simple ClipCussor= True , ClipCursor False king of thing...
Dim client As RECT
Dim upperleft As POINT
Dim hWnd As Long
'Get Userform handle
hWnd = FindWindow(vbNullString, Me.Caption)
'Get information about our window
GetClientRect hWnd, client
upperleft.x = client.left
upperleft.y = client.top
'Make the bottom and right the same as the top/left
' client.bottom = client.top
' client.right = client.left
'Convert window coordinates to screen coordinates
ClientToScreen hWnd, upperleft
'offset our rectangle
OffsetRect client, upperleft.x, upperleft.y
'limit the cursor movement
ClipCursor client
End Sub
Private Sub CommandButton2_Click()
'Releases the cursor limits
ClipCursor ByVal 0&
End Sub
Private Sub UserForm_Activate()
CommandButton1.Caption = "Limit Cursor Movement"
CommandButton2.Caption = "Release Limit"
End Sub
Private Sub UserForm_Click()
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
'Releases the cursor limits
ClipCursor ByVal 0&
End Sub