Option Explicit
'获得整个窗口的范围矩形,窗口的边框、标题栏、滚动条及菜单等都在这个矩形内
Private Declare Function GetWindowRect Lib “user32” (ByVal Hwnd As Long, lpRect As RECT) As Long
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
'获取鼠标指针的当前位置
Private Declare Function GetCursorPos Lib “user32” (lpPoint As POINTAPI) As Long
Private Type POINTAPI
X As Long
Y As Long
End Type
'返回包含了指定点的窗口的句柄。忽略屏蔽、隐藏以及透明窗口
Private Declare Function WindowFromPoint Lib “user32” (ByVal xPoint As Long, ByVal yPoint As Long) As Long
'判断鼠标是否在窗口(COX)范围内
Private Function IsMouseOver() As Boolean
Dim pt As POINTAPI
GetCursorPos pt
IsMouseOver = (WindowFromPoint(pt.X, pt.Y) = Hwnd)
End Function
'获得鼠标在桌面的坐标
Private Function CursorPosition() As POINTAPI
Dim pt As POINTAPI
GetCursorPos pt
CursorPosition.X = pt.X
CursorPosition.Y = pt.Y
End Function
'判断鼠标是否在控件(窗口)范围内
Private Function ControlRange(ByVal Hwnd As Long) As Boolean
Dim pt As RECT, Point As POINTAPI, CRge As Boolean
GetCursorPos Point
GetWindowRect Hwnd, pt
ControlRange = IIf(Point.X > pt.Left And Point.X < pt.Right And Point.Y > pt.Top And Point.Y < pt.Bottom, True, False)
End Function
Private Sub Text1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
If ControlRange(Text1.Hwnd) Then
Text1.BackColor = RGB(255, 0, 0)
Else
Text1.BackColor = RGB(255, 255, 255)
Timer1.Enabled = False
End If
End Sub