EDIT: Having some icons disappear.
EDIT: Not anymore I think it was Winspector &/or Window Spy.
Modification to allow abandoned tray icons tray icons to be cleaned-up.
Plus a new way to get the right
ToolbarWindow32. Works on XP, not tested elsewhere.
Code:
; #NoTrayIcon ; uncomment these three lines to test standalone
; TrayIcon_CleanUp()
; Return
; Based on TrayIcon by Sean @ http://www.autohotkey.com/forum/topic17314.html
TrayIcon_CleanUp()
{ ; Remove abandoned icons in notification tray, such as left behine by "Process Close"
DetectHiddenWindows, On
ControlGet, hWndTray, hWnd,, Infobereich, ahk_class Shell_TrayWnd
WinGet, pidTaskbar, PID, ahk_class Shell_TrayWnd
hProc := DllCall("OpenProcess", "Uint", 0x38, "int", 0, "Uint", pidTaskbar)
pRB := DllCall("VirtualAllocEx", "Uint", hProc, "Uint", 0, "Uint", 20, "Uint", 0x1000, "Uint", 0x4)
VarSetCapacity(btn, 20)
VarSetCapacity(nfo, 24)
SendMessage, 0x418, 0, 0,, ahk_id %hWndTray% ; TB_BUTTONCOUNT
ToolTip, %ErrorLevel%
Loop, %ErrorLevel%
{
Restart:
SendMessage, 0x417, A_Index - 1, pRB,, ahk_id %hWndTray% ; TB_GETBUTTON
DllCall("ReadProcessMemory", "Uint", hProc, "Uint", pRB, "Uint", &btn, "Uint", 20, "Uint", 0)
dwData := NumGet(btn,12)
DllCall("ReadProcessMemory", "Uint", hProc, "Uint", dwData, "Uint", &nfo, "Uint", 24, "Uint", 0)
hWnd := NumGet(nfo, 0)
uID := NumGet(nfo, 4)
if !WinExist("ahk_id " . hWnd)
{
TrayIcon_Remove(hWnd,uID)
Goto Restart
}
}
DllCall("VirtualFreeEx", "Uint", hProc, "Uint", pRB, "Uint", 0, "Uint", 0x8000)
DllCall("CloseHandle", "Uint", hProc)
Return sTrayIcon_CleanUp
}
TrayIcon_Remove(hWnd, uID, nMsg = 0, hIcon = 0, nRemove = 2)
{
DetectHiddenWindows, On
NumPut(VarSetCapacity(ni,444,0), ni)
NumPut(hWnd , ni, 4)
NumPut(uID , ni, 8)
NumPut(1|2|4, ni,12)
NumPut(nMsg , ni,16)
NumPut(hIcon, ni,20)
Return DllCall("shell32\Shell_NotifyIconA", "Uint", nRemove, "Uint", &ni)
}
As noted above the Notification Tray functions have changed in Win7 & prob won't work with it.
Very nice. Just one little bug: when there is an icon to delete, the next one will not be tested because when for example icon 6 will be deleted, the former number 7 will become number 6 and will be oberjumped. I made a little correction.