任务栏通知发光

时间:2013-09-24 10:48:55

标签: c# winforms

我有一个TCP聊天应用程序。当新消息到来时,我想让任务栏发光,直到用户再次打开表单(如果没有焦点/激活)。

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

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

谢谢!

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

3 个答案:

答案 0 :(得分:5)

我希望这会对你有所帮助

[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);
        }

来自我的维基Flashing Taskbar

答案 1 :(得分:2)

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

    [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();。如果您需要,请sample以防万一。

希望这会对你有所帮助。

答案 2 :(得分:0)

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

只需致电

    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);
            }
        }
}