14

I am using C# / WPF to make an application. In that application, I want to blink the window if a particular event occurs so that user of that application knows that something happened. How can I get this in my C# WPF application.

Like in Yahoo Messenger, if you get a message then the message window blinks to get your focus, I want to use that effect in my application.

Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
Aqeel
  • 689
  • 5
  • 20
  • 42
  • i am new in WPF and i searched on google but i didn't find any easy solution that i can understand easily... – Aqeel Jan 20 '12 at 05:31
  • @Marco That link shows how to make a window blink in the taskbar, not how to make a regular sized window blink. – Rachel May 21 '14 at 18:28

3 Answers3

31

Flashing the window and taskbar in a similar way to IM notifications can be accomplished in WPF using the following code. It uses PlatformInvoke to call the WinAPI function FlashWindowEx using the Win32 Handle of the WPF Application.Current.MainWindow

Code

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
    {
        /// <summary>
        /// The size of the structure in bytes.
        /// </summary>
        public uint cbSize;
        /// <summary>
        /// A Handle to the Window to be Flashed. The window can be either opened or minimized.
        /// </summary>
        public IntPtr hwnd;
        /// <summary>
        /// The Flash Status.
        /// </summary>
        public uint dwFlags;
        /// <summary>
        /// The number of times to Flash the window.
        /// </summary>
        public uint uCount;
        /// <summary>
        /// The rate at which the Window is to be flashed, in milliseconds. If Zero, the function uses the default cursor blink rate.
        /// </summary>
        public uint dwTimeout;
    }

    /// <summary>
    /// Stop flashing. The system restores the window to its original stae.
    /// </summary>
    public const uint FLASHW_STOP = 0;

    /// <summary>
    /// Flash the window caption.
    /// </summary>
    public const uint FLASHW_CAPTION = 1;

    /// <summary>
    /// Flash the taskbar button.
    /// </summary>
    public const uint FLASHW_TRAY = 2;

    /// <summary>
    /// Flash both the window caption and taskbar button.
    /// This is equivalent to setting the FLASHW_CAPTION | FLASHW_TRAY flags.
    /// </summary>
    public const uint FLASHW_ALL = 3;

    /// <summary>
    /// Flash continuously, until the FLASHW_STOP flag is set.
    /// </summary>
    public const uint FLASHW_TIMER = 4;

    /// <summary>
    /// Flash continuously until the window comes to the foreground.
    /// </summary>
    public const uint FLASHW_TIMERNOFG = 12;

    /// <summary>
    /// Flash the spacified Window (Form) until it recieves focus.
    /// </summary>
    /// <param name="hwnd"></param>
    /// <returns></returns>
    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;
    }

    /// <summary>
    /// Flash the specified Window (form) for the specified number of times
    /// </summary>
    /// <param name="hwnd">The handle of the Window to Flash.</param>
    /// <param name="count">The number of times to Flash.</param>
    /// <returns></returns>
    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;
    }

    /// <summary>
    /// A boolean value indicating whether the application is running on Windows 2000 or later.
    /// </summary>
    private static bool Win2000OrLater
    {
        get { return Environment.OSVersion.Version.Major >= 5; }
    }
}

Usage

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();
ThomasCle
  • 6,792
  • 7
  • 41
  • 81
Dr. Andrew Burnett-Thompson
  • 20,980
  • 8
  • 88
  • 178
  • This is a great example..except for me the taskbar icon blinks only once for less then a second :( – Julien Aug 17 '12 at 19:34
  • Really? On what OS? I used the above (it was on WinXP mind) and it would flash for a good 5 seconds, then stay solid orange. – Dr. Andrew Burnett-Thompson Aug 18 '12 at 18:58
  • My apologies, the window handle being used by your class was incorrect for my project. I modified it slightly to accept custom handles and all is well. thanks much! – Julien Aug 20 '12 at 15:58
  • 2
    It's worth noting that this works across multiple versions of Windows (rather than the TaskBarItem / System.Windows.Shell stuff mentioned in other answers that is Win 7 (+later?) only). – David Mar 18 '13 at 15:27
  • @Dr.ABT in my applications "this.mainWindowHWnd = new System.Windows.Interop.WindowInteropHelper(mainWindow).Handle" is setting the property to 0. What am I missing? – Chrisjan Lodewyks Mar 02 '15 at 13:54
  • Gosh sorry, no idea. I wrote this 3 years ago and long since moved off this project! ... – Dr. Andrew Burnett-Thompson Mar 02 '15 at 17:36
  • I had the same problem with the taskbar only flashing once. This solution seems to work: http://stackoverflow.com/a/5118285/170990 – Bryan Matthews Mar 09 '16 at 20:53
7

You could use TaskBarItem class to make the taskbar icon of your app blink.

Here is something that can help you achieve it.

Then you can flash, shake, fade-in fade-out, or whatever one of the other zillion FX using WPF Animations. It's very simple and requires almost no code at all, if you have Expression Blend the job is made even easier.

Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
  • i just want to blink WPF window i do not want to show progress. i use this code but it did not help me in my task. Can you simply tell me how i wan blink WPF window on particular event, whether i click on button or on specific time window blinks. – Aqeel Jan 19 '12 at 12:53
  • Answer updated. I didn't refer to the progress bar, you can make the taskbar icon blink itself, except in addition to window blinking. – Shimmy Weitzhandler Jan 19 '12 at 20:56
  • 1
    Some links dead, pls fix them. – Rand Random May 18 '16 at 13:12
5

Setting the ProgressState property to TaskbarItemProgressState.Indeterminate will blink the icon in green. You don have to use the progress bar.

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

tbt
  • 718
  • 5
  • 7
  • This will not blink, but show a "wave" animation, corresponding to an operation with unknown progress. – Error404 Sep 20 '20 at 23:03