系統托盤區殘留無效圖標通常因進程退出未正確調用 Shell_NotifyIcon(NIM_DELETE)。模擬鼠標遍歷托盤區觸發 WM_MOUSEMOVE,Explorer 會自動清理無效圖標。
import winex; import winex.mouse;
// 遞歸移動鼠標遍歷窗口客戶區
var mouseMoveWnd = function(hwnd) {
if (!hwnd) return;
var x, y, cx, cy = win.getPos(hwnd);
for (x = 1; x <= cx; x += 2) {
for (y = 1; y <= cy; y += 2) {
winex.mouse.move(hwnd, x, y);
}
}
}
// 主刷新函數
refreshTrayIcon = function() {
// 1. 主托盤區 ToolbarWindow32
var hwnd = winex.findExists(, , "Shell_TrayWnd", "TrayNotifyWnd");
var toolbar = winex.findEx(winex.findEx(hwnd, , "SysPager"), , "ToolbarWindow32");
mouseMoveWnd(toolbar);
// 2. 溢出區 ToolbarWindow32 (Win7+)
var _, hwndOverflow = winex.findExists(, , "NotifyIconOverflowWindow", "ToolbarWindow32");
mouseMoveWnd(hwndOverflow);
}
// 執行清理
refreshTrayIcon();
hwnd = winex.findExists(, "通知區域", "Shell_TrayWnd", "ToolbarWindow\d+");
var re, rcWindow = GetClientRect(hwnd, ::RECT());
for (x = 1; x <= rcWindow.right; x++) {
for (y = 1; y <= rcWindow.bottom; y++) {
PostMessage(hwnd, 0x200 /*WM_MOUSEMOVE*/, 0, y * 65536 + x);
}
}
無需模擬真實鼠標移動,速度更快,但需確保目標窗口類名匹配(正則 ToolbarWindow\d+)。