WPF中的图标闪烁功能

发布于:2025-05-29 ⋅ 阅读:(23) ⋅ 点赞:(0)

代码示例

using System.Runtime.InteropServices;
using System.Windows.Interop;
using System.Windows;


namespace AutoFeed.ViewModel
{
    // 用于存储窗口闪烁信息的结构体
    [StructLayout(LayoutKind.Sequential)]
    public struct FLASHWINFO
    {
        public uint cbSize;         // 结构体大小
        public IntPtr hwnd;         // 窗口句柄
        public uint dwFlags;        // 闪烁标志
        public uint uCount;         // 闪烁次数
        public uint dwTimeout;      // 闪烁间隔时间
    }

    public class API
    {
        
         // 同时闪烁窗口标题和任务栏按钮
         public const uint FLASHW_ALL = 3;

         // 持续闪烁直到窗口激活
         public const uint FLASHW_TIMERNOFG = 12;

         // 调用Windows API实现窗口闪烁
         [DllImport("user32.dll")]
         [return: MarshalAs(2)]
         public static extern bool FlashWindowEx(ref FLASHWINFO pwfi);

         // 开始闪烁窗口
         public static void FlashWindow(Window window)
         {
             // 获取窗口句柄
             WindowInteropHelper h = new WindowInteropHelper(window);

             // 初始化闪烁信息
             FLASHWINFO info = new FLASHWINFO
             {
                 hwnd = h.Handle,
                 dwFlags = FLASHW_ALL | FLASHW_TIMERNOFG, // 同时闪烁标题和任务栏,直到窗口激活
                 uCount = uint.MaxValue,  // 无限次数
                 dwTimeout = 0            // 使用默认间隔
             };

             // 设置结构体大小
             info.cbSize = Convert.ToUInt32(Marshal.SizeOf(info));
             // 启动闪烁
             FlashWindowEx(ref info);
        }

         // 停止闪烁窗口
         public static void StopFlashingWindow(Window window)
         {
             WindowInteropHelper h = new WindowInteropHelper(window);

             FLASHWINFO info = new FLASHWINFO
             {
                 hwnd = h.Handle,
                 dwFlags = 0,             // 停止闪烁的标志
                 uCount = uint.MaxValue,
                 dwTimeout = 0
             };

             info.cbSize = Convert.ToUInt32(Marshal.SizeOf(info));
             // 停止闪烁
             FlashWindowEx(ref info);
         }
    }
    
}


网站公告

今日签到

点亮在社区的每一天
去签到