namespace FlashWindowApi
{
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
[StructLayout(LayoutKind.Sequential)]
public struct FlashWinInfo
{
public uint Size;
public IntPtr Handle;
public FlashWinFlags Flags;
public uint Count;
public uint Timeout;
}
[Flags]
public enum FlashWinFlags : uint
{
FlashStop = 0x0000,
FlashCaption = 0x0001,
FlashTray = 0x0002,
FlashAll = 0x0003,
FlashTimer = 0x0004,
FlashTimerNoForeground = 0x000c,
}
public static class FlashWindowManager
{
public static void FlashWindow(
IntPtr winHandle,
FlashWinFlags flags = FlashWinFlags.FlashAll,
int count = int.MaxValue,
int timeout = 0)
{
var flashWinInfo = new FlashWinInfo();
flashWinInfo.Size = (uint)Marshal.SizeOf(flashWinInfo);
flashWinInfo.Handle = winHandle;
flashWinInfo.Flags = flags;
flashWinInfo.Count = (uint)count;
flashWinInfo.Timeout = (uint)timeout;
FlashWindowEx(ref flashWinInfo);
}
public static void FlashWindow(
this Form form,
FlashWinFlags flags = FlashWinFlags.FlashAll,
int count = int.MaxValue,
int timeout = 0)
{
FlashWindow(form.Handle, flags, count, timeout);
}
public static void FlashStop(IntPtr winHandle)
{
FlashWindow(winHandle, FlashWinFlags.FlashStop);
}
public static void FlashStop(this Form form)
{
FlashStop(form.Handle);
}
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool FlashWindowEx(
ref FlashWinInfo flashWinInfo);
}
}
|
This is an asynchronous function, that means it returns immediately after beginning the flashing.
Once the handle of a window is known, it is possible to flash the window with a caption and/or taskbar button.
Another code sample shows how to enumerate handles for the child windows of the main VFP window. Apply the FlashWindowEx to any of these windows and watch the result.
* * *
The FlashWindowManager class shown here once added to C# WinForm project automatically adds FlashWindow and FlashStop extension methods to every form in the project.
With fairly small changes the FlashWindowManager class can be used with .NET WPF forms. public static void FlashWindow(
this Window window,
FlashWinFlags flags = FlashWinFlags.FlashAll,
int count = int.MaxValue,
nt timeout = 0)
{
var windowInteropHelper = new WindowInteropHelper(window);
FlashWindow(windowInteropHelper.Handle, flags, count, timeout);
} |