SoFunction
Updated on 2025-03-07

C# Windows API application method to implement window flicker based on FlashWindowEx

This article describes the method of implementing window flickering based on FlashWindowEx in C# Windows API application. Share it for your reference, as follows:

Windows API

In addition to coordinating the execution of applications, allocating memory, and managing resources..., Windows is also a large service center. It calls various services in this service center (each service is a function) and can help applications achieve the purpose of opening windows, drawing graphics, and using peripheral devices. Since the object of these functions service is an application, it is called Application Programming Interface, or API functions. The WIN32 API is the application programming interface of the Microsoft Windows 32-bit platform.

FlashWindowEx

Function function: flashes the specified window. It does not change the activation state of the window.

Function prototype:

BOOL WINAPI FlashWindowEx(
__in PFLASHWINFO pfwi
);

Parameters: pfwi pointer to the FLASHWINFO structure. .

Return value: Returns the specified window state before calling the FlashWindowEx function. If the window title was active before the call, the return value is non-zero.

Methods to implement window flickering

API import

/// <summary>
/// Flashing window/// </summary>
/// <param name="pwfi">Window flashing information structure</param>/// &lt;returns&gt;&lt;/returns&gt;
[DllImport("")]
public static extern bool FlashWindowEx(ref FLASHWINFO pwfi);

Flashing type enumeration definition

/// &lt;summary&gt;
/// Blinking type/// &lt;/summary&gt;
public enum flashType : uint
{
  FLASHW_STOP = 0, //Stop flashing  FALSHW_CAPTION = 1, //Only flash the title  FLASHW_TRAY = 2, //Only blink the taskbar  FLASHW_ALL = 3, //The title and taskbar blink at the same time  FLASHW_PARAM1 = 4,
  FLASHW_PARAM2 = 12,
  FLASHW_TIMER = FLASHW_TRAY | FLASHW_PARAM1, //Unconditionally flashes the taskbar until the send stop flag or the window is activated. If it is not activated, it will be highlighted when stopping  FLASHW_TIMERNOFG = FLASHW_TRAY | FLASHW_PARAM2 // When not activated, the taskbar is flashed until the send stop flag or the form is activated, and it will be highlighted after stopping}

FLASHWINFO Structure Definition

/// &lt;summary&gt;
/// Contains information about the number of windows and states that the system should flash within a specified time/// &lt;/summary&gt;
public struct FLASHWINFO
{
  /// &lt;summary&gt;
  /// Structure size  /// &lt;/summary&gt;
  public uint cbSize;
  /// &lt;summary&gt;
  /// The window handle to flash or stop  /// &lt;/summary&gt;
  public IntPtr hwnd;
  /// &lt;summary&gt;
  /// Type of flashing  /// &lt;/summary&gt;
  public uint dwFlags;
  /// &lt;summary&gt;
  /// Number of times the window is flashed  /// &lt;/summary&gt;
  public uint uCount;
  /// &lt;summary&gt;
  /// The frequency of the window flashing, in milliseconds; if this value is 0, it is the flashing frequency of the default icon  /// &lt;/summary&gt;
  public uint dwTimeout;
}

Flashing window method encapsulation

/// &lt;summary&gt;
/// Flashing window/// &lt;/summary&gt;
/// <param name="hWnd">Window handle</param>/// <param name="type">Flashing type</param>/// &lt;returns&gt;&lt;/returns&gt;
public static bool FlashWindowEx(IntPtr hWnd, flashType type)
{
  FLASHWINFO fInfo = new FLASHWINFO();
   = Convert.ToUInt32((fInfo));
   = hWnd;//The handle of the window to be flashed, the window can be open or minimized   = (uint)type;//The type of flashing   = ;//The number of times the window is flashed   = 0; //The frequency of the window flashing, in milliseconds; if this value is 0, it is the flashing frequency of the default icon.  return FlashWindowEx(ref fInfo);
}

For more information about C# related content, please check out the topic of this site:Summary of C# form operation skills》、《C# data structure and algorithm tutorial》、《Tutorial on the usage of common C# controls》、《Introduction to C# object-oriented programming tutorial"and"Summary of thread usage techniques for C# programming

I hope this article will be helpful to everyone's C# programming.