避免c#中出现多个托盘图标
本文关键字:托盘图标 避免 | 更新日期: 2023-09-27 18:06:12
我正在c#.net中开发一个应用程序,为此,我正在编写代码以在系统托盘中显示图标,每当有新消息到达时,气球工具提示将显示在那里,其中有点击事件将打开新消息到达,一切工作正常,但问题是我正在获得多个数字在系统托盘中生成的图标,这应该只有一个,我怎么能防止它?我在网上找到了如何处理它们,但找不到防止一个以上的方法。或者有更好的方式来显示新收到的消息通知…如果你知道解决方案请帮助我…
有更好的自定义解决方案可用,参见这里和这里的一些示例。
但是,系统托盘不会自动刷新。如果你显示/隐藏多个系统托盘图标,它会把托盘弄得一团糟。通常,当鼠标悬停时,所有已处理的图标都会消失。但是,有一种方法可以以编程方式刷新系统托盘。参考这里。
注意: SendMessage函数,将指定的消息发送到一个或多个窗口。SendMessage函数调用指定窗口的窗口过程,直到窗口过程处理完消息才返回。
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll")]
public static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, uint msg, int wParam, int lParam);
public void RefreshTrayArea()
{
IntPtr systemTrayContainerHandle = FindWindow("Shell_TrayWnd", null);
IntPtr systemTrayHandle = FindWindowEx(systemTrayContainerHandle, IntPtr.Zero, "TrayNotifyWnd", null);
IntPtr sysPagerHandle = FindWindowEx(systemTrayHandle, IntPtr.Zero, "SysPager", null);
IntPtr notificationAreaHandle = FindWindowEx(sysPagerHandle, IntPtr.Zero, "ToolbarWindow32", "Notification Area");
if (notificationAreaHandle == IntPtr.Zero)
{
notificationAreaHandle = FindWindowEx(sysPagerHandle, IntPtr.Zero, "ToolbarWindow32", "User Promoted Notification Area");
IntPtr notifyIconOverflowWindowHandle = FindWindow("NotifyIconOverflowWindow", null);
IntPtr overflowNotificationAreaHandle = FindWindowEx(notifyIconOverflowWindowHandle, IntPtr.Zero, "ToolbarWindow32", "Overflow Notification Area");
RefreshTrayArea(overflowNotificationAreaHandle);
}
RefreshTrayArea(notificationAreaHandle);
}
private static void RefreshTrayArea(IntPtr windowHandle)
{
const uint wmMousemove = 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, wmMousemove, 0, (y << 16) + x);
}