任务栏通知亮起

本文关键字:通知 任务栏 | 更新日期: 2023-09-27 18:32:45

我有一个TCP聊天应用程序。当新消息到达时,我想使任务栏发光,直到用户再次打开表单(以防它不是焦点/激活的)。

我的意思的一个例子:http://puu.sh/4z01n.png

我怎样才能让它像那样发光?

谢谢!

如果你仍然不明白我的意思..我提供的图像是当某些东西"发光"时出现在任务栏上的图标上。意思是有通知。这就是我想要实现的目标。

任务栏通知亮起

我希望这对你有帮助

[DllImport("User32.dll")]
[return:MarshalAs(UnmanagedType.Bool)]
static extern bool FlashWindowEx(ref FLASHINFO pwfi);

    [StructLayout(LayoutKind.Sequential)]
    public struct FLASHWINFO {
        public UInt32 cbSize;
        public IntPtr hwnd;
        public UInt32 dwFlags;
        public UInt32 uCount;
        public UInt32 dwTimeout;
    }

[Flags]
        public enum FlashMode {
            /// 
            /// Stop flashing. The system restores the window to its original state.
            /// 
            FLASHW_STOP = 0,
            /// 
            /// Flash the window caption.
            /// 
            FLASHW_CAPTION = 1,
            /// 
            /// Flash the taskbar button.
            /// 
            FLASHW_TRAY = 2,
            /// 
            /// Flash both the window caption and taskbar button.
            /// This is equivalent to setting the FLASHW_CAPTION | FLASHW_TRAY flags.
            /// 
            FLASHW_ALL = 3,
            /// 
            /// Flash continuously, until the FLASHW_STOP flag is set.
            /// 
            FLASHW_TIMER = 4,
            /// 
            /// Flash continuously until the window comes to the foreground.
            /// 
            FLASHW_TIMERNOFG = 12
        }
        public static bool FlashWindow(IntPtr hWnd, FlashMode fm) {
            FLASHWINFO fInfo = new FLASHWINFO();
            fInfo.cbSize = Convert.ToUInt32(Marshal.SizeOf(fInfo));
            fInfo.hwnd = hWnd;
            fInfo.dwFlags = (UInt32)fm;
            fInfo.uCount = UInt32.MaxValue;
            fInfo.dwTimeout = 0;
            return FlashWindowEx(ref fInfo);
        }

取自我在闪烁任务栏的维基

你需要

一些互操作性来实现这一点,首先在你的类中添加System.Runtime.InteropServices命名空间,在 you 类开始时定义这个函数,

    [DllImport("user32.dll")]
    static extern bool FlashWindow(IntPtr hwnd, bool FlashStatus);

这是一个API函数,它的描述说,FlashWindow函数闪烁指定的窗口一次。然后将Timer添加到您的类中(将其从工具箱中拖放,将其间隔设置为 500 毫秒)。然后假设Form1是您要闪烁的窗口,使用以下代码来实现此目的;

    private void Form1_Activated(object sender, EventArgs e)
    {
        timer1.Stop();//Stop the timer to stop flashing.
    }
    private void Form1_Deactivate(object sender, EventArgs e)
    {
        timer1.Start();//Start timer if window loses focus.
    }
    private void timer1_Tick(object sender, EventArgs e)
    {
        FlashWindow(this.Handle, true);//Flash the window untill it gets focused.
    }

好吧,当新消息到达时,请致电timer1.Start();。如果您需要,可以提供一个样品以防万一。

希望这对你有帮助。

您可以使用扩展方法,该方法将闪烁窗口直到它获得焦点,而无需使用计时器。

只需致电

    form.FlashNotification();

    public static class ExtensionMethods
        {
            [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
            [return: MarshalAs(UnmanagedType.Bool)]
            private static extern bool FlashWindowEx(ref FLASHWINFO pwfi);
            private const uint FLASHW_ALL = 3;
            private const uint FLASHW_TIMERNOFG = 12;
            [StructLayout(LayoutKind.Sequential)]
            private struct FLASHWINFO
            {
                public uint cbSize;
                public IntPtr hwnd;
                public uint dwFlags;
                public uint uCount;
                public uint dwTimeout;
            }
            public static bool FlashNotification(this Form form)
            {
                IntPtr hWnd = form.Handle;
                FLASHWINFO fInfo = new FLASHWINFO();
                fInfo.cbSize = Convert.ToUInt32(Marshal.SizeOf(fInfo));
                fInfo.hwnd = hWnd;
                fInfo.dwFlags = FLASHW_ALL | FLASHW_TIMERNOFG;
                fInfo.uCount = uint.MaxValue;
                fInfo.dwTimeout = 0;
                return FlashWindowEx(ref fInfo);
            }
        }
}