Edited: I forgot to mention that in my script I already use OnMouseMove for other purpose, so I prefer a solution which works inside OnMouseMove.
Gui Test: +HwndIdTest -dpiScale
Gui Test: Show, x50 y50 w800 h500
MouseGetPos mx, my, IdWindow
WinGetTitle, WinT, ahk_id %IdWindow%
ToolTip The cursor is in window %WinT%, 150, 3
If (IdWindow != IdTest) ; Doesn't work,
MsgBox Out! Now in %WinT%! ; of course.