C# 任务栏通知光晕

C# 任务栏通知光晕,c#,winforms,C#,Winforms,我有一个TCP聊天应用程序。当一条新消息到达时,我想让任务栏发光,直到用户再次打开表单(以防它没有焦点/激活) 我的意思的一个例子: 我怎样才能让它像那样发光 谢谢 如果您仍然不明白我的意思。我提供的图像是当某个东西“发光”时出现在任务栏图标上的图像。意思是有一个通知。这就是我想要实现的。我希望这会对你有所帮助 [DllImport("User32.dll")] [return:MarshalAs(UnmanagedType.Bool)] static extern bool FlashWin

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

我的意思的一个例子:

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

谢谢


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

我希望这会对你有所帮助

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

摘自我的wiki(位于

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

    [DllImport("user32.dll")]
    static extern bool FlashWindow(IntPtr hwnd, bool FlashStatus);
这是一个API函数,它的描述是,FlashWindow函数将指定的窗口闪烁一次。。然后向类中添加一个
计时器(从工具箱中拖放它,将其间隔设置为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);
            }
        }
}

你是说?可能是因为某种原因,它仍然闪烁着evne,尽管我正在聚焦窗口。我必须设置聚焦程序还是什么?。。