Tác giả: tuyen_dt18
Mô tả:
Code: Select all
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function ScreenToClient Lib "user32" (ByVal hWnd As Long, lpPoint As POINTAPI) As Long
Private Declare Function GetWindowRect Lib "user32" Alias "GetWindowRect" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Type POINTAPI
x As Long
y As Long
End Type
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Function IsHover(ByVal hWnd As Long) As Boolean
Dim P As POINTAPI, RC As RECT
GetCursorPos P
GetWindowRect hWnd, RC
ScreenToClient hWnd, P
If (P.x >= 0) And (P.x < (RC.Right - RC.Left)) And (P.y >= 0) And (P.y <= (RC.Bottom - RC.Top)) Then
IsHover = True
Else
IsHover = False
End If
End Function
Sử dụng : Tạo 1 Project mới, thêm một Timer với Interval=100 vào, Paste đoạn code trên vào, sao đó thêm code dưới đây vào sự kiện Timer() của Timer1 sau , và chạy chương trình :
Private Sub Timer1_Timer()
If IsHover(Me.hWnd) Then
Me.Caption = "Hover Form"
Else
Me.Caption = "Leave Form"
End If
If IsHover(Command1.hWnd) Then
Command1.Caption = "Hover"
Else
Command1.Caption = "Leave"
End If
End Sub