程序异常退出后,托盘残留图标清理方法(C#)
生活随笔
收集整理的這篇文章主要介紹了
程序异常退出后,托盘残留图标清理方法(C#)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
程序猿對于宕機再熟悉不過了,如果一個程序異常退出,那我們只能在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();托盤又恢復往日簡潔 ^_^
轉載于:https://www.cnblogs.com/boxy/p/11402966.html
總結
以上是生活随笔為你收集整理的程序异常退出后,托盘残留图标清理方法(C#)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 国科大prml--SVM
- 下一篇: C#穿透session隔离———Wind