本文參考C++改寫 https://blog.csdn.net/dpsying/article/details/20139651 (該文章的坐標理解的有誤解,會導致功能無效)
SendMessage的移動鼠標里的坐標 是基於句柄內的 坐標,並不是屏幕坐標,任務欄寬度300 高度固定40,那么就應該從寬度0-300 坐標15之間 移動過去。
首先聲明需要用到的 winapi 函數

1 [DllImport("user32.dll", EntryPoint = "FindWindow")] 2 private static extern int FindWindow(string lpszClass, string lpszWindow); 3 [DllImport("user32.dll", EntryPoint = "FindWindowEx")] 4 private static extern int FindWindowEx(int hwndParent, int hwndChildAfter, string lpszClass, string lpszWindow); 5 6 [DllImport("user32.dll", EntryPoint = "GetWindowRect")] 7 private static extern int GetWindowRect(int hwnd, ref System.Drawing.Rectangle lpRect); 8 [DllImport("user32.dll", EntryPoint = "SendMessage")] 9 private static extern int SendMessage(int hwnd, int wMsg, int wParam, int lParam); 10 11 private static readonly int WM_MOUSEMOVE = 512;
封裝調用的流程方法

1 /// <summary> 2 /// 刷新任務欄圖標 3 /// </summary> 4 public static void RefreshTaskbarIcon() 5 { 6 //任務欄窗口 7 int one = FindWindow("Shell_TrayWnd", null); 8 //任務欄右邊托盤圖標+時間區 9 int two = FindWindowEx(one, 0, "TrayNotifyWnd", null); 10 //不同系統可能有可能沒有這層 11 int three = FindWindowEx(two, 0, "SysPager", null); 12 //托盤圖標窗口 13 int foor; 14 if (three > 0) 15 { 16 foor = FindWindowEx(three, 0, "ToolbarWindow32", null); 17 } 18 else 19 { 20 foor = FindWindowEx(two, 0, "ToolbarWindow32", null); 21 } 22 if (foor > 0) 23 { 24 System.Drawing.Rectangle r = new System.Drawing.Rectangle(); 25 GetWindowRect(foor, ref r); 26 //從任務欄左上角從左到右 MOUSEMOVE一遍,所有圖標狀態會被更新 27 for (int x = 0; x < (r.Right - r.Left) - r.X; x++) 28 { 29 SendMessage(foor, WM_MOUSEMOVE, 0, (1 << 16) | x); 30 } 31 } 32 }