防止c#

时间:2015-08-03 04:54:56

标签: c# trayicon

我正在c#.net中开发一个应用程序,为此我正在编写代码以在系统托盘中显示图标,每当有新消息到达时,气球工具提示将显示在那里,其中有点击事件将打开新的消息到了,一切正常,但问题是我在系统托盘中生成多个图标,只有一个,我怎么能阻止它?我在互联网上发现如何处置它们,但找不到方法防止多于一个。或者有更好的方法来显示新收到的消息的通知..请帮助我,如果你知道解决方案..

1 个答案:

答案 0 :(得分:1)

有一些更好的自定义解决方案,请参阅herehere以获取一些示例。

但是,系统托盘不会自动刷新。如果显示/隐藏多个系统托盘图标,则可能会使托盘变脏。鼠标悬停时,通常所有丢弃的图标都会消失。但是,有一种方法可以通过编程方式刷新系统托盘。参考here

注意: 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);
    }