Solved Limit Mouse Pointer Movement To Form Boundary Only (1 Viewer)

Jason Lee Hayes

Active member
Local time
Today, 14:59
Joined
Jul 25, 2020
Messages
175
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
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 08:59
Joined
Feb 28, 2001
Messages
27,131
I found this:


This looks like a "C"-callable function. Which means you might need to figure out in which library (.DLL or other loadable library) it is actually defined and then somehow declare an entry point to it that would be compatible with the VBA environment. In C variants, you can actually call such a thing because most C-based languages create machine code directly.

In VBA, you are running an emulated machine where the compiler created pseudo-code, not machine code, so you have to define how to emulate a given subroutine or function by defining the interface for VBA. If you can do that, you have a good shot at doing what you want.

The interface declaration will follow these syntax rules:


There is your starting point. I can't research it farther than that because I have to help my wife with something and you know those "honey-do" projects are very high priority.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 21:59
Joined
May 7, 2009
Messages
19,231
you do realize that without a button to turn if off, there is no way you can close the form
if the form is datasheet?

Code:
Option Compare Database
Option Explicit
' search and found the x86 version on the net
' acknowledgment to whom is due.
'
' arnelgp
' Modified for x64 Access
'
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
#If VBA7 Then
    Private Declare PtrSafe Sub ClipCursor Lib "user32" (lpRect As Any)
    Private Declare PtrSafe Function GetClientRect Lib "user32" (ByVal hwnd As LongPtr, lpRect As RECT) As Long
    Private Declare PtrSafe Function ClientToScreen Lib "user32" (ByVal hwnd As LongPtr, lpPoint As POINT) As Long
    Private Declare PtrSafe Function OffsetRect Lib "user32" (lpRect As RECT, ByVal x As Long, ByVal y As Long) As Long
    Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassname As String, ByVal lpWindowName As String) As LongPtr
#Else
    Private Declare Sub ClipCursor Lib "user32" (lpRect As Any)
    Private Declare Sub GetClientRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT)
    Private Declare Sub ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINT)
    Private Declare Sub OffsetRect Lib "user32" (lpRect As RECT, ByVal x As Long, ByVal Y As Long)
    'Added this declaration to get userform window handle
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
#End If


Public Sub MouseToForm(Optional bolClip As Boolean = False, Optional f As Access.Form)
'Limits the Cursor movement to within the form.
#If VBA7 Then
    Dim hwnd As LongPtr
#Else
    Dim hwnd As Long
#End If
    
    If (bolClip) Then
        If ((f) Is Nothing) Then Exit Sub
        'Get Userform handle
        'hwnd = FindWindow(vbNullString, f.Caption)
        hwnd = f.hwnd
        Call MouseToWinHandle(hwnd)
    
    Else
        'Releases the cursor limits
        ClipCursor ByVal 0&
    End If
    
End Sub

#If VBA7 Then
Public Function MouseToWinHandle(hwnd As LongPtr)
#Else
Public Function MouseToWinHandle(hwnd As Long)
#End If
    Dim Client As RECT
    Dim upperleft As POINT
    '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 Function
 

Jason Lee Hayes

Active member
Local time
Today, 14:59
Joined
Jul 25, 2020
Messages
175
Hi,

Sorry, yes - the form(s) i intend to use the function on will be a borderless popup form but all will have a button which will close the form on click.
What i meant was i don't need the caption toggle in the original posting therefore what you have provided is exactly what i need. Obviously i can create a new module and add the above;.

Ref: MouseToForm(Optional bolClip As Boolean = False, Optional f As Access.Form)

How would you call it though?

Would it be something like: eg

On Form Open Event

Call MouseToForm True (Enable Mouse Limit

Call MouseToForm () = False ' (Disable Mouse Limit)

Call MouseToForm () =True (frmMyFormName) '(Enable Mouse Limit on form where form name is frmMyFormName)

Thanks in Advance...
 

wvmitchell

New member
Local time
Today, 06:59
Joined
Sep 4, 2020
Messages
24
Have you considered using the Pop Up or Modal settings, might that work?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 21:59
Joined
May 7, 2009
Messages
19,231
all will have a button which will close the form on click
yes, you can do that.
on the Load event of your form, restrict the mouse movement within that form:

private sub form_load()
call MouseToForm(True, Me)
end sub

now on the Click event of the button that closes the form, you need to reinstate the mouse:

private sub button_Click()
call MouseToForm(False)
end sub
 

isladogs

MVP / VIP
Local time
Today, 14:59
Joined
Jan 14, 2017
Messages
18,209
Hi
I missed this thread yesterday but was contacted separately by the OP. Attached is the example app I sent Jason which includes code to restrict the cursor to the current form ... and also to release it again if required.
 

Attachments

  • Multi Monitors v3.zip
    114.5 KB · Views: 262

Users who are viewing this thread

Top Bottom