VB高手 鼠标移入移出窗体

来源:百度知道 编辑:UC知道 时间:2024/06/01 07:09:05
当鼠标移入,移出窗体的时候将窗体背景色变蓝,复原.
我用窗体自带的Mousemove事件处理,但在鼠标快速移出的时候,成功率就低了,就是说我快速移出窗体后,本该背景色复原的结果可能还是蓝色.
帮忙解决这个问题.
----------------------------------
我觉得会这个样子会很占CPU,我看很多网页上都有这种功能的,难道他们都是用的这种方法?

'在窗体上添加一个计时器,设置Interval属性为50(越小越精确,但要大于0),添加如下代码:

Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
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 Type POINTAPI
x As Long
y As Long
End Type

Private Sub Timer1_Timer()
Dim R As RECT, P As POINTAPI, L As Long
L = GetWindowRect(Me.hwnd, R)
L = GetCursorPos(P)
If P.x < R.Left Or P.x > R.Right Or P.y < R.Top Or P.y > R.Bottom Then
Form1.BackColor = vbRed
Else
Form1.BackColor = vbBlue
End If
End Sub

1楼的,计时器的最小精度好像是20吧