MessageBoxTimeOut.cs 5.0 KB

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