C# 调用SendMessage刷新任务栏图标(强制结束时图标未消失)
本文参考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;
winapi
封装调用的流程方法


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 }
方法
赞 (0)
