Friday, March 23, 2012

Flashing your .Net Application

While in my professional carrier, I came across a requirement in which I need to make my application Flash/Blink in task bar and window title bar. After searching alot, it reveals that there is no such API available for performing such function. I need to wrap a method imported from user32: FlashWindowEx

FLASHWINFO struct is defined which holds the required fields and information:

[StructLayout(LayoutKind.Sequential)]
public struct FLASHWINFO
{
    public UInt32 cbSize;
    public IntPtr hwnd;
    public Int32 dwFlags;
    public UInt32 uCount;
    public Int32 dwTimeout;
} 


dwFlags plays an important rule of providing kind of Flash Required:

// stop flashing
FLASHW_STOP = 0; 

// flash the window title 
FLASHW_CAPTION = 1; 

// flash the taskbar button
FLASHW_TRAY = 2; 

// 1 | 2
FLASHW_ALL = 3; 

// flash continuously 
FLASHW_TIMER = 4; 

// flash until the window comes to the foreground 
FLASHW_TIMERNOFG = 12; 


C# Flash function will look like so:

public static bool Flash()
{
    FLASHWINFO fw = new FLASHWINFO();

    fw.cbSize = Convert.ToUInt32(Marshal.SizeOf(typeof(FLASHWINFO)));
    fw.hwnd = this.Handle;
    fw.dwFlags = 2;
    fw.uCount = UInt32.MaxValue;

    FlashWindowEx(ref fw);
}
 
Sample Flash Window
       
Sample Available here....

2 comments: