Option Explicit
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If]#If[/URL] VBA7 Then
Private Declare PtrSafe Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As LongPtr, ByVal dwFlags As Long) As Boolean
Private Declare PtrSafe Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Declare PtrSafe Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As LongPtr, ByVal wFlag As Long) As LongPtr
Private Declare PtrSafe Function GetForegroundWindow Lib "user32" () As LongPtr
Private Declare PtrSafe Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As LongPtr, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare PtrSafe Function GetLastActivePopup Lib "user32" (ByVal hwndOwnder As LongPtr) As LongPtr
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
Private Declare PtrSafe Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As LongPtr, lpdwProcessId As Long) As Long
Private Declare PtrSafe Function SetTimer Lib "user32" (ByVal hwnd As LongPtr, ByVal nIDEvent As LongPtr, ByVal uElapse As Long, ByVal lpTimerFunc As LongPtr) As LongPtr
Private Declare PtrSafe Function KillTimer Lib "user32" (ByVal hwnd As LongPtr, ByVal nIDEvent As LongPtr) As Long
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If]#If[/URL] Win64 Then
Private Declare PtrSafe Function GetWindowLong Lib "user32" Alias "GetWindowLongPtrA" (ByVal hwnd As LongPtr, ByVal nIndex As Long) As LongPtr
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else]#Else[/URL]
Private Declare PtrSafe Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As LongPtr, ByVal nIndex As Long) As LongPtr
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End]#End[/URL] If
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else]#Else[/URL]
Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Boolean
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Declare Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Long, ByVal wFlag As Long) As Long
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetLastActivePopup Lib "user32" (ByVal hwndOwnder As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Private Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End]#End[/URL] If
Private Const SND_ASYNC = &H1
Private Const SND_FILENAME = &H20000
Private Const SND_LOOP = &H8
Private Const SND_PURGE = &H40
Private Const WS_SYSMENU = &H80000
Private Const GWL_STYLE = (-16)
Sub SoundMe()
Call StartTimer
Call PlaySound("c:\windows\media\Ring08.wav", 0, SND_ASYNC Or SND_FILENAME Or SND_LOOP)
End Sub
Sub StartTimer(Optional ByVal dummy As Boolean)
KillTimer Application.hwnd, 0
SetTimer Application.hwnd, 0, 100, AddressOf TimerProc
End Sub
Sub TimerProc(Optional ByVal dummy As Boolean)
[B][COLOR=#008000] 'Stop sound and timer if ESC key is pressed or if an unhandled error occurs.[/COLOR][/B]
If GetAsyncKeyState(vbKeyEscape) Or IsBreakMode Then
PlaySound vbNullString, 0, SND_PURGE
KillTimer Application.hwnd, 0
End If
End Sub
Function IsBreakMode() As Boolean
Dim hCurPid As Long, hForegroundPid As Long
Dim sbuffer As String, lRet As Long
sbuffer = Space(256)
lRet = GetClassName(GetForegroundWindow, sbuffer, 256)
Call GetWindowThreadProcessId(Application.hwnd, hCurPid)
Call GetWindowThreadProcessId(GetForegroundWindow, hForegroundPid)
IsBreakMode = _
(FindWindow("wndclass_desked_gsk", vbNullString) = _
GetNextWindow(GetLastActivePopup(FindWindow("wndclass_desked_gsk", vbNullString)), 4)) Or _
(GetWindowLong(GetForegroundWindow, GWL_STYLE) And WS_SYSMENU) = 0 And hForegroundPid = hCurPid And _
Left(sbuffer, lRet) <> "Net UI Tool Window"
End Function
Sub Auto_Close()
PlaySound vbNullString, 0, SND_PURGE
KillTimer Application.hwnd, 0
End Sub