'在窗体头部说明API函数:
Private Declare Function SetCapture Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function ReleaseCapture Lib "user32" () As Long
'下面是控件Object1的MouseMove事件中代码:
Private Sub Object1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim MouseOver As Boolean
'判断当前鼠标位置是否在Object1上
MouseOver = (0 <= X) And (X <= Object1.Width) And (0 <= Y) And (Y <= Object1.Height)
If MouseOver Then
' MouseOver Event
' 假如鼠标在Object1上, 则利用SetCapture将每一个鼠标事件都传递给Object1
' 并显示小精灵
小精灵.Visible = True
SetCapture Object1.hWnd
Else
' MouseLeave Event
' 假如鼠标不在Object1上, 则利用ReleaseCapture释放鼠标捕捉
' 并关闭显示小精灵
小精灵.Visible = False
ReleaseCapture
End If
End Sub
Private Declare Function SetCapture Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function ReleaseCapture Lib "user32" () As Long
'通过Form的MouseMove事件,处理无句柄控件(Label、Image等)的鼠标离开事件
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim MouseOver As Boolean
'判断当前鼠标位置是否在Label1上
MouseOver = (Label1.Left <= X) And (X <= Label1.Left + Label1.Width) And (Label1.Top <= Y) And (Y <= Label1.Top + Label1.Height)
If MouseOver Then '鼠标位置是在Label1上
' MouseOver Event
Label1.Visible = True '显示Label1
If Button = 0 Then '如果鼠标键没有按下则利用SetCapture将每一个鼠标事件都传递给Form1
SetCapture Form1.hWnd
End If
Else
' MouseLeave Event
Label1.Visible = False '关闭显示Label1
ReleaseCapture ' 假如鼠标不在Label1上, 则利用ReleaseCapture释放鼠标捕捉
End If
End Sub
'当Label1响应过鼠标事件后,再次判别鼠标位置
Private Sub Label1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim MouseOver As Boolean
MouseOver = (Label1.Left <= X) And (X <= Label1.Left + Label1.Width) And (Label1.Top <= Y) And (Y <= Label1.Top + Label1.Height)
If MouseOver Then '鼠标位置是在Label1上
' MouseOver Event
Label1.Visible = True '显示Label1
SetCapture Form1.hWnd '利用SetCapture将每一个鼠标事件都传递给Form1
Else
' MouseLeave Event
Label1.Visible = False '关闭显示Label1
ReleaseCapture ' 假如鼠标不在Label1上, 则利用ReleaseCapture释放鼠标捕捉
End If
End Sub
'程序退出时释放鼠标捕捉
Private Sub Form_Unload(Cancel As Integer)
ReleaseCapture
End Sub
附:判断鼠标是否在窗体上的方法:
Private Declare Function SetCapture Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
With Me
If X > 0 And X < .Width And Y > 0 And Y < .Height Then
SetCapture Me.hWnd
Label1 = "进入"
Else
ReleaseCapture
Label1 = "离开"
End If
End With
End Sub
文本转载自某人空间,若侵犯了权益,请联系我,积极删除。