【问题标题】:Taskbar notification glow任务栏通知发光
【发布时间】:2013-09-24 10:48:55
【问题描述】:

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

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

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

谢谢!

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

【问题讨论】:

标签: c# winforms


【解决方案1】:

希望对你有帮助

[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

【讨论】:

    【解决方案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();。A sample 以防万一。

    希望对你有所帮助。

    【讨论】:

    • 出于某种原因,尽管我正在聚焦窗口,但它仍然在闪烁。我是否必须设置 Focused proeprty 或其他东西?..
    【解决方案3】:

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

    只要打电话

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-31
      • 2015-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-11
      • 1970-01-01
      相关资源
      最近更新 更多