PDA

View Full Version : سوال: حذف نمایش یک مسیج باکس بعد از 5 ثانیه نمایش



Iran58
دوشنبه 29 اردیبهشت 1393, 21:48 عصر
سلام
چگونه می توانم بعداز اینکه یک مسج باکس نمایش داده شده بعداز 5ثانیه نمایش خود مسج باکس حذف شود

aliagamon
دوشنبه 29 اردیبهشت 1393, 22:08 عصر
یه کلاس برای اینکار هست تو اینترنت
public class AutoClosingMessageBox {
System.Threading.Timer _timeoutTimer;
string _caption;
AutoClosingMessageBox(string text, string caption, int timeout)
{
_caption = caption;
_timeoutTimer = new System.Threading.Timer(OnTimerElapsed,
null, timeout, System.Threading.Timeout.Infinite);
MessageBox.Show(text, caption);
}
public static void Show(string text, string caption, int timeout)
{
new AutoClosingMessageBox(text, caption, timeout);
}
void OnTimerElapsed(object state)
{
IntPtr mbWnd = FindWindow(null, _caption);
if (mbWnd != IntPtr.Zero)
SendMessage(mbWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
_timeoutTimer.Dispose();
}
const int WM_CLOSE = 0x0010;
[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
}
اینطوری هم استفاده میشه:
AutoClosingMessageBox.Show("Text", "Caption", 1000);
رفرنس:http://stackoverflow.com/questions/14522540/close-a-messagebox-after-several-seconds