程序猿对于宕机再熟悉不过了,如果一个程序异常退出,那我们只能在Windows日志中找到它的踪迹;如果它是一个托盘程序,异常退出后,它会停留在托盘中久久不能离去,直到我们的鼠标从它身上划过;如果一个程序被另一个守护程序不断的杀掉、重启,那么它的结果会如下图所示,
请注意!!这不是扫雷!!没错,这正是残留的图标。解决方法其实很简单,在程序每次开启前,向托盘窗口发送鼠标经过消息WM_MOUSEMOVE,使其刷新。
首先要明确一点,托盘包括显示区域和溢出区域,所以需要同时刷新,防止遗漏。可以用Spy++鼠标右键选择“突出显示”,窗口轮廓就会闪烁,SysPager和NotifyIconOverflowWindow。
代码如下:
using System;
using System.Runtime.InteropServices;public class TaskBarUtil
{struct RECT{public int left, top, right, bottom;}public static void RefreshNotification(){var NotifyAreaHandle = GetNotifyAreaHandle();if (NotifyAreaHandle != IntPtr.Zero)RefreshWindow(NotifyAreaHandle);var NotifyOverHandle = GetNotifyOverHandle();if (NotifyOverHandle != IntPtr.Zero)RefreshWindow(NotifyOverHandle);}private static void RefreshWindow(IntPtr windowHandle){const uint WM_MOUSEMOVE = 0x0200;RECT rect;GetClientRect(windowHandle, out rect);for (var x = 0; x < rect.right; x += 5)for (var y = 0; y < rect.bottom; y += 5)SendMessage(windowHandle, WM_MOUSEMOVE, 0, (y << 16) + x);}private static IntPtr GetNotifyAreaHandle(){var TrayWndHandle = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "Shell_TrayWnd", string.Empty);var TrayNotifyWndHandle = FindWindowEx(TrayWndHandle, IntPtr.Zero, "TrayNotifyWnd", string.Empty);var SysPagerHandle = FindWindowEx(TrayNotifyWndHandle, IntPtr.Zero, "SysPager", string.Empty);var NotifyAreaHandle = FindWindowEx(SysPagerHandle, IntPtr.Zero, "ToolbarWindow32", string.Empty);return NotifyAreaHandle;}private static IntPtr GetNotifyOverHandle(){var OverHandle = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "NotifyIconOverflowWindow", string.Empty);var NotifyOverHandle = FindWindowEx(OverHandle, IntPtr.Zero, "ToolbarWindow32", string.Empty);return NotifyOverHandle;}[DllImport("user32.dll", SetLastError = true)]static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);[DllImport("user32.dll")]static extern bool GetClientRect(IntPtr handle, out RECT rect);[DllImport("user32.dll", CharSet = CharSet.Auto)]static extern IntPtr SendMessage(IntPtr handle, UInt32 message, Int32 wParam, Int32 lParam);
}
TaskBarUtil.RefreshNotification();
托盘又恢复往日简洁 ^_^