FlashWindow.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. namespace Core.Mes.Client.Comm.Tool
  7. {
  8. public static class FlashWindow
  9. {
  10. [DllImport("user32.dll")]
  11. [return: MarshalAs(UnmanagedType.Bool)]
  12. private static extern bool FlashWindowEx(ref FLASHWINFO pwfi);
  13. [StructLayout(LayoutKind.Sequential)]
  14. private struct FLASHWINFO
  15. {
  16. /// <summary>
  17. /// The size of the structure in bytes.
  18. /// </summary>
  19. public uint cbSize;
  20. /// <summary>
  21. /// A Handle to the Window to be Flashed. The window can be either opened or minimized.
  22. /// </summary>
  23. public IntPtr hwnd;
  24. /// <summary>
  25. /// The Flash Status.
  26. /// </summary>
  27. public uint dwFlags;
  28. /// <summary>
  29. /// The number of times to Flash the window.
  30. /// </summary>
  31. public uint uCount;
  32. /// <summary>
  33. /// The rate at which the Window is to be flashed, in milliseconds. If Zero, the function uses the default cursor blink rate.
  34. /// </summary>
  35. public uint dwTimeout;
  36. }
  37. /// <summary>
  38. /// Stop flashing. The system restores the window to its original stae.
  39. /// </summary>
  40. public const uint FLASHW_STOP = 0;
  41. /// <summary>
  42. /// Flash the window caption.
  43. /// </summary>
  44. public const uint FLASHW_CAPTION = 1;
  45. /// <summary>
  46. /// Flash the taskbar button.
  47. /// </summary>
  48. public const uint FLASHW_TRAY = 2;
  49. /// <summary>
  50. /// Flash both the window caption and taskbar button.
  51. /// This is equivalent to setting the FLASHW_CAPTION | FLASHW_TRAY flags.
  52. /// </summary>
  53. public const uint FLASHW_ALL = 3;
  54. /// <summary>
  55. /// Flash continuously, until the FLASHW_STOP flag is set.
  56. /// </summary>
  57. public const uint FLASHW_TIMER = 4;
  58. /// <summary>
  59. /// Flash continuously until the window comes to the foreground.
  60. /// </summary>
  61. public const uint FLASHW_TIMERNOFG = 12;
  62. /// <summary>
  63. /// Flash the spacified Window (Form) until it recieves focus.
  64. /// </summary>
  65. /// <param name="form">The Form (Window) to Flash.</param>
  66. /// <returns></returns>
  67. public static bool Flash(System.Windows.Forms.Form form)
  68. {
  69. // Make sure we're running under Windows 2000 or later
  70. if (Win2000OrLater)
  71. {
  72. FLASHWINFO fi = Create_FLASHWINFO(form.Handle, FLASHW_ALL | FLASHW_TIMERNOFG, uint.MaxValue, 0);
  73. return FlashWindowEx(ref fi);
  74. }
  75. return false;
  76. }
  77. private static FLASHWINFO Create_FLASHWINFO(IntPtr handle, uint flags, uint count, uint timeout)
  78. {
  79. FLASHWINFO fi = new FLASHWINFO();
  80. fi.cbSize = Convert.ToUInt32(Marshal.SizeOf(fi));
  81. fi.hwnd = handle;
  82. fi.dwFlags = flags;
  83. fi.uCount = count;
  84. fi.dwTimeout = timeout;
  85. return fi;
  86. }
  87. /// <summary>
  88. /// Flash the specified Window (form) for the specified number of times
  89. /// </summary>
  90. /// <param name="form">The Form (Window) to Flash.</param>
  91. /// <param name="count">The number of times to Flash.</param>
  92. /// <returns></returns>
  93. public static bool Flash(System.Windows.Forms.Form form, uint count)
  94. {
  95. if (Win2000OrLater)
  96. {
  97. FLASHWINFO fi = Create_FLASHWINFO(form.Handle, FLASHW_ALL, count, 0);
  98. return FlashWindowEx(ref fi);
  99. }
  100. else
  101. {
  102. return false;
  103. }
  104. }
  105. /// <summary>
  106. /// Start Flashing the specified Window (form)
  107. /// </summary>
  108. /// <param name="form">The Form (Window) to Flash.</param>
  109. /// <returns></returns>
  110. public static bool Start(System.Windows.Forms.Form form)
  111. {
  112. if (Win2000OrLater)
  113. {
  114. FLASHWINFO fi = Create_FLASHWINFO(form.Handle, FLASHW_ALL, uint.MaxValue, 0);
  115. return FlashWindowEx(ref fi);
  116. }
  117. return false;
  118. }
  119. /// <summary>
  120. /// Stop Flashing the specified Window (form)
  121. /// </summary>
  122. /// <param name="form"></param>
  123. /// <returns></returns>
  124. public static bool Stop(System.Windows.Forms.Form form)
  125. {
  126. if (Win2000OrLater)
  127. {
  128. FLASHWINFO fi = Create_FLASHWINFO(form.Handle, FLASHW_STOP, uint.MaxValue, 0);
  129. return FlashWindowEx(ref fi);
  130. }
  131. return false;
  132. }
  133. /// <summary>
  134. /// A boolean value indicating whether the application is running on Windows 2000 or later.
  135. /// </summary>
  136. private static bool Win2000OrLater
  137. {
  138. get { return System.Environment.OSVersion.Version.Major >= 5; }
  139. }
  140. }
  141. }