C# 自動關閉的 MessageBox

  • 60744
  • 0
  • C#
  • 2008-07-13

C# 自動關閉的 MessageBox

在有限定時間內自動關閉的 MessageBox。

Sample Code:

   1:  using System;
   2:  using System.Windows.Forms;
   3:   
   4:  namespace WindowsApplication1
   5:  {
   6:      public class Form1: Form
   7:      {
   8:          public Form1()
   9:          {
  10:              InitializeComponent();
  11:          }
  12:   
  13:          private void Form1_Load(object sender, EventArgs e)
  14:          {
  15:              MessageBoxTimeout.Show("It will be closed automatically in 5 seconds", "MessageBox With Timeout", 5000);
  16:          }
  17:      }
  18:  }

App_Code\MessageBoxTimeout.cs

   1:  using System;
   2:  using System.Runtime.InteropServices;
   3:  using System.Text;
   4:  using System.Threading;
   5:  using System.Windows.Forms;
   6:   
   7:  public class MessageBoxTimeout
   8:  {
   9:      [DllImport("kernel32.dll")]
  10:      private static extern uint GetCurrentThreadId();
  11:   
  12:      private delegate int EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
  13:   
  14:      [DllImport("user32.dll")]
  15:      private static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
  16:   
  17:      [DllImport("user32.dll", SetLastError = true)]
  18:      private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
  19:   
  20:      [DllImport("user32.dll")]
  21:      private static extern int GetClassName(IntPtr hWnd, [Out] StringBuilder lpClassName, int nMaxCount);
  22:   
  23:      [DllImport("user32.dll")]
  24:      private static extern bool IsWindowEnabled(IntPtr hWnd);
  25:   
  26:      [DllImport("user32.dll", SetLastError = true)]
  27:      [return: MarshalAs(UnmanagedType.Bool)]
  28:      private static extern bool PostMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
  29:   
  30:      private class TimerThread
  31:      {
  32:          private readonly DateTime timeoutTime;
  33:          private readonly uint currentThreadId;
  34:          private bool terminateFlag;
  35:          private readonly Thread thread;
  36:   
  37:          public TimerThread(int timeoutMillisec)
  38:          {
  39:              timeoutTime = DateTime.Now.AddMilliseconds(timeoutMillisec);
  40:              currentThreadId = GetCurrentThreadId();
  41:              terminateFlag = false;
  42:              thread = new Thread(ThreadProc);
  43:              thread.Start();
  44:          }
  45:   
  46:          private void ThreadProc()
  47:          {
  48:              while (!terminateFlag)
  49:              {
  50:                  Thread.Sleep(100);
  51:                  if (DateTime.Now > timeoutTime)
  52:                  {
  53:                      EnumWindows(EnumWindowsProc, new IntPtr(0));
  54:                      return;
  55:                  }
  56:              }
  57:          }
  58:   
  59:          private int EnumWindowsProc(IntPtr hWnd, IntPtr lParam)
  60:          {
  61:              uint processId;
  62:              uint threadId;
  63:              threadId = GetWindowThreadProcessId(hWnd, out processId);
  64:              if (threadId == currentThreadId)
  65:              {
  66:                  StringBuilder className = new StringBuilder("", 256);
  67:                  GetClassName(hWnd, className, 256);
  68:                  if (className.ToString() == "#32770" && IsWindowEnabled(hWnd))
  69:                  {
  70:                      const int WM_COMMAND = 0x111;
  71:                      PostMessage(hWnd, WM_COMMAND, new IntPtr(2), new IntPtr(0));
  72:                      return 0;
  73:                  }
  74:              }
  75:              return 1;
  76:          }
  77:   
  78:          public void Terminate()
  79:          {
  80:              terminateFlag = true;
  81:              thread.Join();
  82:          }
  83:      }
  84:   
  85:      public static DialogResult Show(string text, int timeoutMillsec)
  86:      {
  87:          TimerThread tt = new TimerThread(timeoutMillsec);
  88:          try
  89:          {
  90:              return MessageBox.Show(text);
  91:          }
  92:          finally
  93:          {
  94:              tt.Terminate();
  95:          }
  96:      }
  97:   
  98:      public static DialogResult Show(string text, string caption, int timeoutMillsec)
  99:      {
 100:          TimerThread tt = new TimerThread(timeoutMillsec);
 101:          try
 102:          {
 103:              return MessageBox.Show(text, caption);
 104:          }
 105:          finally
 106:          {
 107:              tt.Terminate();
 108:          }
 109:      }
 110:   
 111:      public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, int timeoutMillsec)
 112:      {
 113:          TimerThread tt = new TimerThread(timeoutMillsec);
 114:          try
 115:          {
 116:              return MessageBox.Show(text, caption, buttons);
 117:          }
 118:          finally
 119:          {
 120:              tt.Terminate();
 121:          }
 122:      }
 123:   
 124:      public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, int timeoutMillsec)
 125:      {
 126:          TimerThread tt = new TimerThread(timeoutMillsec);
 127:          try
 128:          {
 129:              return MessageBox.Show(text, caption, buttons, icon);
 130:          }
 131:          finally
 132:          {
 133:              tt.Terminate();
 134:          }
 135:      }
 136:   
 137:      public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, int timeoutMillsec)
 138:      {
 139:          TimerThread tt = new TimerThread(timeoutMillsec);
 140:          try
 141:          {
 142:              return MessageBox.Show(text, caption, buttons, icon, defaultButton);
 143:          }
 144:          finally
 145:          {
 146:              tt.Terminate();
 147:          }
 148:      }
 149:   
 150:      public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions options, int timeoutMillsec)
 151:      {
 152:          TimerThread tt = new TimerThread(timeoutMillsec);
 153:          try
 154:          {
 155:              return MessageBox.Show(text, caption, buttons, icon, defaultButton, options);
 156:          }
 157:          finally
 158:          {
 159:              tt.Terminate();
 160:          }
 161:      }
 162:  }