Option Explicit
Private Declare GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare GetParent Lib "user32" (ByVal hwnd As Long) As Long
Private Type POINTAPI
x As Long
y As Long
End Type
Private Sub Form_Load()
Me.Left = -Me.Width + 15
End Sub
Private Sub Timer1_Timer()
Dim i As Long
Dim iPos As POINTAPI
Dim pace As Long
GetCursorPos iPos '获得当前鼠标位置
i = WindowFromPoint(iPos.x, iPos.y) '获得当前鼠标位置的句柄
If i = Me.hwnd Or GetParent(i) = hwnd Or GetParent(GetParent(i)) = hwnd Then
While Me.Left <= 0
Me.Left = Me.Left + 1
DoEvents
Wend
Else
While Me.Left >= -Me.Width + 15
Me.Left = Me.Left - 1
DoEvents
Wend
End If
End Sub