首页 > 代码库 > WPF 任务栏图标闪烁提醒

WPF 任务栏图标闪烁提醒

  1 using System;  2 using System.Collections.Generic;  3 using System.Linq;  4 using System.Runtime.InteropServices;  5 using System.Text;  6 using System.Windows;  7 using System.Windows.Interop;  8   9  10 public static class FlashWindow 11 { 12     [DllImport("user32.dll")] 13     [return: MarshalAs(UnmanagedType.Bool)] 14     private static extern bool FlashWindowEx(ref FLASHWINFO pwfi); 15  16     [StructLayout(LayoutKind.Sequential)] 17     private struct FLASHWINFO 18     { 19         /// <summary> 20         /// The size of the structure in bytes. 21         /// </summary> 22         public uint cbSize; 23  24         /// <summary> 25         /// A Handle to the Window to be Flashed. The window can be either opened or minimized. 26         /// </summary> 27         public IntPtr hwnd; 28  29         /// <summary> 30         /// The Flash Status. 31         /// </summary> 32         public uint dwFlags; 33  34         /// <summary> 35         /// The number of times to Flash the window. 36         /// </summary> 37         public uint uCount; 38  39         /// <summary> 40         /// The rate at which the Window is to be flashed, in milliseconds. If Zero, the function uses the default cursor blink rate. 41         /// </summary> 42         public uint dwTimeout; 43     } 44  45     /// <summary> 46     /// Stop flashing. The system restores the window to its original stae. 47     /// </summary> 48     public const uint FLASHW_STOP = 0; 49  50     /// <summary> 51     /// Flash the window caption. 52     /// </summary> 53     public const uint FLASHW_CAPTION = 1; 54  55     /// <summary> 56     /// Flash the taskbar button. 57     /// </summary> 58     public const uint FLASHW_TRAY = 2; 59  60     /// <summary> 61     /// Flash both the window caption and taskbar button. 62     /// This is equivalent to setting the FLASHW_CAPTION | FLASHW_TRAY flags. 63     /// </summary> 64     public const uint FLASHW_ALL = 3; 65  66     /// <summary> 67     /// Flash continuously, until the FLASHW_STOP flag is set. 68     /// </summary> 69     public const uint FLASHW_TIMER = 4; 70  71     /// <summary> 72     /// Flash continuously until the window comes to the foreground. 73     /// </summary> 74     public const uint FLASHW_TIMERNOFG = 12; 75  76  77     /// <summary> 78     /// Flash the spacified Window (Form) until it recieves focus. 79     /// </summary> 80     /// <param name="form">The Form (Window) to Flash.</param> 81     /// <returns></returns> 82     public static bool Flash(Window w) 83     { 84         // Make sure we‘re running under Windows 2000 or later 85         if (Win2000OrLater) 86         { 87             FLASHWINFO fi = Create_FLASHWINFO(new WindowInteropHelper(w).Handle, FLASHW_ALL | FLASHW_TIMERNOFG, uint.MaxValue, 0); 88             return FlashWindowEx(ref fi); 89         } 90         return false; 91     } 92  93     private static FLASHWINFO Create_FLASHWINFO(IntPtr handle, uint flags, uint count, uint timeout) 94     { 95         FLASHWINFO fi = new FLASHWINFO(); 96         fi.cbSize = Convert.ToUInt32(Marshal.SizeOf(fi)); 97         fi.hwnd = handle; 98         fi.dwFlags = flags; 99         fi.uCount = count;100         fi.dwTimeout = timeout;101         return fi;102     }103 104     /// <summary>105     /// Flash the specified Window (form) for the specified number of times106     /// </summary>107     /// <param name="form">The Form (Window) to Flash.</param>108     /// <param name="count">The number of times to Flash.</param>109     /// <returns></returns>110     public static bool Flash(Window w, uint count)111     {112         if (Win2000OrLater)113         {114             FLASHWINFO fi = Create_FLASHWINFO(new WindowInteropHelper(w).Handle, FLASHW_ALL, count, 0);115             return FlashWindowEx(ref fi);116         }117         return false;118     }119 120     /// <summary>121     /// Start Flashing the specified Window (form)122     /// </summary>123     /// <param name="form">The Form (Window) to Flash.</param>124     /// <returns></returns>125     public static bool Start(Window w)126     {127         if (Win2000OrLater)128         {129             var fi = Create_FLASHWINFO(new WindowInteropHelper(w).Handle, FLASHW_ALL, uint.MaxValue, 0);130             return FlashWindowEx(ref fi);131         }132         return false;133     }134 135     /// <summary>136     /// Stop Flashing the specified Window (form)137     /// </summary>138     /// <param name="form"></param>139     /// <returns></returns>140     public static bool Stop(Window w)141     {142         if (Win2000OrLater)143         {144             var fi = Create_FLASHWINFO(new WindowInteropHelper(w).Handle, FLASHW_STOP, uint.MaxValue, 0);145             return FlashWindowEx(ref fi);146         }147         return false;148     }149 150     /// <summary>151     /// A boolean value indicating whether the application is running on Windows 2000 or later.152     /// </summary>153     private static bool Win2000OrLater154     {155         get { return System.Environment.OSVersion.Version.Major >= 5; }156     }157 }
FlashWindow


启动:FlashWindow.Start(Window w);

停止:FlashWindow.Stop(Window w);

.NET技术交流群 199281001 .欢迎加入。