MessageBoxTimeOut.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows.Forms;
  6. using System.Runtime.InteropServices;
  7. namespace Wongoing.Basic
  8. {
  9. /// <summary>
  10. /// 自动超时消息提示框
  11. /// </summary>
  12. public class MessageBoxTimeOut
  13. {
  14. /// <summary>
  15. /// 标题
  16. /// </summary>
  17. private static string _caption;
  18. /// <summary>
  19. /// 显示消息框
  20. /// </summary>
  21. /// <param name="text">消息内容</param>
  22. /// <param name="caption">标题</param>
  23. /// <param name="timeout">超时时间,单位:毫秒</param>
  24. public static void Show(string text, string caption, int timeout)
  25. {
  26. _caption = caption;
  27. StartTimer(timeout);
  28. MessageBox.Show(text, caption);
  29. }
  30. /// <summary>
  31. /// 显示消息框
  32. /// </summary>
  33. /// <param name="text">消息内容</param>
  34. /// <param name="caption">标题</param>
  35. /// <param name="timeout">超时时间,单位:毫秒</param>
  36. /// <param name="buttons">消息框上的按钮</param>
  37. public static void Show(string text, string caption, int timeout, MessageBoxButtons buttons)
  38. {
  39. _caption = caption;
  40. StartTimer(timeout);
  41. MessageBox.Show(text, caption, buttons);
  42. }
  43. /// <summary>
  44. /// 显示消息框
  45. /// </summary>
  46. /// <param name="text">消息内容</param>
  47. /// <param name="caption">标题</param>
  48. /// <param name="timeout">超时时间,单位:毫秒</param>
  49. /// <param name="buttons">消息框上的按钮</param>
  50. /// <param name="icon">消息框上的图标</param>
  51. public static void Show(string text, string caption, int timeout, MessageBoxButtons buttons, MessageBoxIcon icon)
  52. {
  53. _caption = caption;
  54. StartTimer(timeout);
  55. MessageBox.Show(text, caption, buttons, icon);
  56. }
  57. /// <summary>
  58. /// 显示消息框
  59. /// </summary>
  60. /// <param name="owner">消息框所有者</param>
  61. /// <param name="text">消息内容</param>
  62. /// <param name="caption">标题</param>
  63. /// <param name="timeout">超时时间,单位:毫秒</param>
  64. public static void Show(IWin32Window owner, string text, string caption, int timeout)
  65. {
  66. _caption = caption;
  67. StartTimer(timeout);
  68. MessageBox.Show(owner, text, caption);
  69. }
  70. /// <summary>
  71. /// 显示消息框
  72. /// </summary>
  73. /// <param name="owner">消息框所有者</param>
  74. /// <param name="text">消息内容</param>
  75. /// <param name="caption">标题</param>
  76. /// <param name="timeout">超时时间,单位:毫秒</param>
  77. /// <param name="buttons">消息框上的按钮</param>
  78. public static void Show(IWin32Window owner, string text, string caption, int timeout, MessageBoxButtons buttons)
  79. {
  80. _caption = caption;
  81. StartTimer(timeout);
  82. MessageBox.Show(owner, text, caption, buttons);
  83. }
  84. /// <summary>
  85. /// 显示消息框
  86. /// </summary>
  87. /// <param name="owner">消息框所有者</param>
  88. /// <param name="text">消息内容</param>
  89. /// <param name="caption">标题</param>
  90. /// <param name="timeout">超时时间,单位:毫秒</param>
  91. /// <param name="buttons">消息框上的按钮</param>
  92. /// <param name="icon">消息框上的图标</param>
  93. public static void Show(IWin32Window owner, string text, string caption, int timeout, MessageBoxButtons buttons, MessageBoxIcon icon)
  94. {
  95. _caption = caption;
  96. StartTimer(timeout);
  97. MessageBox.Show(owner, text, caption, buttons, icon);
  98. }
  99. private static void StartTimer(int interval)
  100. {
  101. Timer timer = new Timer();
  102. timer.Interval = interval;
  103. timer.Tick += new EventHandler(Timer_Tick);
  104. timer.Enabled = true;
  105. }
  106. private static void Timer_Tick(object sender, EventArgs e)
  107. {
  108. KillMessageBox();
  109. //停止计时器
  110. ((Timer)sender).Enabled = false;
  111. }
  112. [DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
  113. private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
  114. [DllImport("user32.dll", CharSet = CharSet.Auto)]
  115. private extern static int PostMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
  116. private const int WM_CLOSE = 0x10;
  117. private static void KillMessageBox()
  118. {
  119. //查找MessageBox的弹出窗口,注意对应标题
  120. IntPtr ptr = FindWindow(null, _caption);
  121. if (ptr != IntPtr.Zero)
  122. {
  123. //查找到窗口则关闭
  124. PostMessage(ptr, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
  125. }
  126. }
  127. }
  128. }