'添加一个UserControl1,加入以下代码:
Option Explicit
'将鼠标捕获设置到指定的窗口。在鼠标按钮按下的时候,这个窗口会为当前应用程序或整个系统接收所有鼠标输入.
Private Declare Function SetCapture Lib “user32” (ByVal hwnd As Long) As Long
'为当前的应用程序释放鼠标捕获.
Private Declare Function ReleaseCapture Lib “user32” () As Long
Dim But As Integer '鼠标按下与弹起事件。
Public Event Click()
Private Sub UserControl_Initialize()
Call BosPic(0)
End Sub
Private Sub UserControl_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button <> 1 Then Exit Sub
But = Button '获得鼠标事件
Call BosPic(2)
End Sub
Private Sub UserControl_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If X >= 0 And X <= UserControl.Width And Y >= 0 And Y <= UserControl.Height Then
SetCapture UserControl.hwnd
If But <> 1 Then Call BosPic(1)
Else
ReleaseCapture '这里就是离开啦
But = 0
Call BosPic(0)
End If
End Sub
Private Sub UserControl_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If X >= 0 And X <= UserControl.Width And Y >= 0 And Y <= UserControl.Height And But = 1 Then
RaiseEvent Click '触发单击事件。
End If
But = 0 '将鼠标弹起【释放】。
Call BosPic(0)
End Sub
Private Sub BosPic(Index As Integer)
Select Case Index
Case 0
UserControl.BackColor = RGB(255, 0, 0)
Case 1
UserControl.BackColor = RGB(0, 255, 0)
Case 2
UserControl.BackColor = RGB(255, 0, 255)
Case 3
End Select
End Sub