在C#/ WPF中发生特定事件时强制窗口闪烁

我正在使用C#/ WPF来创建一个应用程序。 在该应用程序中,我想在发生特定事件时使窗口闪烁,以便该应用程序的用户知道发生了什么。 我怎样才能在我的C#WPF应用程序中得到它。

就像在Yahoo Messenger中一样,如果你收到消息,那么消息窗口会闪烁以获得你的关注,我想在我的应用程序中使用该效果。

以与IM通知类似的方式闪烁窗口和任务栏可以使用以下代码在WPF中完成。 它使用PlatformInvoke使用WPF Application.Current.MainWindow的Win32句柄调用WinAPI函数FlashWindowEx

 public class FlashWindowHelper { private IntPtr mainWindowHWnd; private Application theApp; public FlashWindowHelper(Application app) { this.theApp = app; } public void FlashApplicationWindow() { InitializeHandle(); Flash(this.mainWindowHWnd, 5); } public void StopFlashing() { InitializeHandle(); if (Win2000OrLater) { FLASHWINFO fi = CreateFlashInfoStruct(this.mainWindowHWnd, FLASHW_STOP, uint.MaxValue, 0); FlashWindowEx(ref fi); } } private void InitializeHandle() { if (this.mainWindowHWnd == IntPtr.Zero) { // Delayed creation of Main Window IntPtr as Application.Current passed in to ctor does not have the MainWindow set at that time var mainWindow = this.theApp.MainWindow; this.mainWindowHWnd = new System.Windows.Interop.WindowInteropHelper(mainWindow).Handle; } } [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool FlashWindowEx(ref FLASHWINFO pwfi); [StructLayout(LayoutKind.Sequential)] private struct FLASHWINFO { ///  /// The size of the structure in bytes. ///  public uint cbSize; ///  /// A Handle to the Window to be Flashed. The window can be either opened or minimized. ///  public IntPtr hwnd; ///  /// The Flash Status. ///  public uint dwFlags; ///  /// The number of times to Flash the window. ///  public uint uCount; ///  /// The rate at which the Window is to be flashed, in milliseconds. If Zero, the function uses the default cursor blink rate. ///  public uint dwTimeout; } ///  /// Stop flashing. The system restores the window to its original stae. ///  public const uint FLASHW_STOP = 0; ///  /// Flash the window caption. ///  public const uint FLASHW_CAPTION = 1; ///  /// Flash the taskbar button. ///  public const uint FLASHW_TRAY = 2; ///  /// Flash both the window caption and taskbar button. /// This is equivalent to setting the FLASHW_CAPTION | FLASHW_TRAY flags. ///  public const uint FLASHW_ALL = 3; ///  /// Flash continuously, until the FLASHW_STOP flag is set. ///  public const uint FLASHW_TIMER = 4; ///  /// Flash continuously until the window comes to the foreground. ///  public const uint FLASHW_TIMERNOFG = 12; ///  /// Flash the spacified Window (Form) until it recieves focus. ///  ///  ///  public static bool Flash(IntPtr hwnd) { // Make sure we're running under Windows 2000 or later if (Win2000OrLater) { FLASHWINFO fi = CreateFlashInfoStruct(hwnd, FLASHW_ALL | FLASHW_TIMERNOFG, uint.MaxValue, 0); return FlashWindowEx(ref fi); } return false; } private static FLASHWINFO CreateFlashInfoStruct(IntPtr handle, uint flags, uint count, uint timeout) { FLASHWINFO fi = new FLASHWINFO(); fi.cbSize = Convert.ToUInt32(Marshal.SizeOf(fi)); fi.hwnd = handle; fi.dwFlags = flags; fi.uCount = count; fi.dwTimeout = timeout; return fi; } ///  /// Flash the specified Window (form) for the specified number of times ///  /// The handle of the Window to Flash. /// The number of times to Flash. ///  public static bool Flash(IntPtr hwnd, uint count) { if (Win2000OrLater) { FLASHWINFO fi = CreateFlashInfoStruct(hwnd, FLASHW_ALL | FLASHW_TIMERNOFG, count, 0); return FlashWindowEx(ref fi); } return false; } ///  /// A boolean value indicating whether the application is running on Windows 2000 or later. ///  private static bool Win2000OrLater { get { return Environment.OSVersion.Version.Major >= 5; } } } 

用法

 var helper = new FlashWindowHelper(Application.Current); // Flashes the window and taskbar 5 times and stays solid // colored until user focuses the main window helper.FlashApplicationWindow(); // Cancels the flash at any time helper.StopFlashing(); 

您可以使用TaskBarItem类使应用程序的任务栏图标闪烁。 以下是一些可以帮助您实现目标的资源:
http://social.msdn.microsoft.com/Forums/en/wpf/thread/369bc5ee-a9a7-493f-978f-27a8ddedea06
http://www.bobpowell.net/flashbar.htm

然后你可以使用WPF动画进行闪光 , 摇晃 , 淡入淡出或其他任何一个其他的万亿FX 。 它非常简单,几乎不需要任何代码,如果你有Expression Blend,那么工作变得更加容易。

ProgressState属性设置为TaskbarItemProgressState.Indeterminate将以绿色闪烁图标。 您不必使用进度条。

http://msdn.microsoft.com/en-us/library/system.windows.shell.taskbaritemprogressstate.aspx