MousePosCtr




Public Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Public Type POINTAPI
x As Long
y As Long
End Type
Public Declare Function GetClientRect Lib "user32" _
(ByVal hwnd As Long, lpRect As RECT) As Long
Public Declare Function GetWindowRect Lib "user32" _
(ByVal hwnd As Long, lpRect As RECT) As Long
Public Declare Function GetCursorPos Lib "user32" _
(lpPoint As POINTAPI) As Long

Public Function MouseOver(windowHandle As Long, _
testc As Control)

Dim windowPos As RECT
Dim clientRect As RECT
Dim mousePos As POINTAPI
Dim controlRect As RECT

r = GetWindowRect(windowHandle, windowPos)
r = GetClientRect(windowHandle, clientRect)

'Calculate border width - windowRect is

'complete size and client rect is internaL

'so subtract and divide

BorderWidth = (windowPos.Right - windowPos.Left _
- clientRect.Right) / 2

'topOffset is difference in window and

'Client rect sizes minus one border width


topOffset = windowPos.Bottom - windowPos.Top _
- clientRect.Bottom - BorderWidth

controlRect.Left = windowPos.Left + BorderWidth + _
(testc.LeftScreen.TwipsPerPixelX)

controlRect.Right = windowPos.Left + BorderWidth + _
(testc.Left + testc.Width) / Screen.TwipsPerPixelX)

controlRect.Top = windowPos.Top + topOffset + _
(testc.Top / Screen.TwipsPerPixelY)

controlRect.Bottom = windowPos.Top + topOffset + _
((testc.Top + testc.Height) / Screen.TwipsPerPixelY)

r = GetCursorPos(mousePos)

MouseOver = (mousePos.x >= controlRect.Left And _
mousePos.x < controlRect.Right And
mousePos.y >= controlRect.Top And _
mousePos.y < controlRect.Bottom)
End Function

Assumes:
The GetClientRect API does not return enough info
to find out the exact position of your controls on
the screen, but GetWindowRect returns data which
includes offsets created by titlebars and fOrm
borders. This code shows how to mani pulate these
two pieces of information to work out the correct
coordinates and create a MouseOver event.










( mouseposctr.html )- by Paolo Puglisi - Modifica del 17/12/2023