VB判断鼠标是否在窗口或控件范围+获得鼠标的坐标

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

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

键盘上的舞指

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值