3

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

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

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

谢谢!

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

4

3 回答 3

6

我希望这能帮到您

[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的 wiki

于 2013-09-24T11:03:51.767 回答
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();当有新消息到达时打电话。一个样本以备不时之需。

希望这对您有所帮助。

于 2013-09-24T11:04:19.963 回答
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);
            }
        }
}
于 2018-11-20T04:04:45.197 回答