项目中需要重启一个托盘程序。但是重启之后,任务栏的托盘程序图标增加了一个,原来的图标并没有消失。
原来任务栏的托盘程序图标,当鼠标在上面移动过之后才会刷新。所以一个解决办法就是模拟鼠标事件,模拟鼠标在上面的所有图标都移动过。但是这种办法对于图标在隐藏任务栏中则不起作用。
#include <windows.h>
#include <iostream>
int main() {
HWND shell_tray_wnd_handle = FindWindow(L"Shell_TrayWnd", NULL);
HWND tray_notify_wnd_handle = FindWindowEx(shell_tray_wnd_handle, 0, L"TrayNotifyWnd", NULL);
HWND sys_paper_handle = FindWindowEx(tray_notify_wnd_handle, 0, L"SysPager", NULL);
HWND tool_bar_handle = NULL;
if ( sys_paper_handle != NULL ) {
tool_bar_handle = FindWindowEx(sys_paper_handle, 0, L"ToolbarWindow32", NULL);
} else {
tool_bar_handle = FindWindowEx(tray_notify_wnd_handle, 0, L"ToolbarWindow32", NULL);
}
if ( tool_bar_handle == NULL ) {
return 0;
}
RECT rect;
GetWindowRect(tool_bar_handle, &rect);
int width = rect.right - rect.left;
int height = rect.bottom - rect.top;
for (int x = 1; x < width; x += 2) {
for (int y = 1; y < height; y += 2) {
SendMessage(tool_bar_handle, WM_MOUSEMOVE, 0, MAKELPARAM(x, y));
}
}
return 0;
}