1. 程式人生 > >可設定指定時間自動消失的 MessageBox實現

可設定指定時間自動消失的 MessageBox實現

本文主要是講如何實現可設定指定時間自動消失的 MessageBox提示框ShowMessageBoxTimeout實現;

在開發客戶端應用程式的時候,經常用得WinForm中MessageBox提示框。但是有時候還是滿足不了一些使用者要求,客戶要求千奇百怪,例如客戶需要做某些提示的時候,不去點選確定或取消的時候,等待一段時間自動消失,為此我們可以使用下面類來實現,採用 Thread.Sleep來關掉當前提示框,具體程式碼如下:

ShowMessageBoxTimeout實現

using System;
using System.Collections.Generic;
using System.Linq;
using
System.Text; using System.Runtime.InteropServices; using System.Windows.Forms; using System.Threading; namespace Tools.App { public class ShowMsg { [DllImport("user32.dll", SetLastError = true)] static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll"
)] static extern bool EndDialog(IntPtr hDlg, out IntPtr nResult); //三個引數:1、文字提示-text,2、提示框標題-caption,3、按鈕型別-MessageBoxButtons ,4、自動消失時間設定-timeout public static void ShowMessageBoxTimeout(string text, string caption, MessageBoxButtons buttons, int timeout) { ThreadPool.QueueUserWorkItem(new
WaitCallback(CloseMessageBox), new CloseState(caption, timeout)); MessageBox.Show(text, caption, buttons); } private static void CloseMessageBox(object state) { CloseState closeState = state as CloseState; Thread.Sleep(closeState.Timeout); IntPtr dlg = FindWindow(null, closeState.Caption); if (dlg != IntPtr.Zero) { IntPtr result; EndDialog(dlg, out result); } } } }

ShowMessageBoxTimeout呼叫

//三個引數:1、文字提示,2、提示框標題,3、按鈕型別,4、自動消失時間設定
ShowMsg.ShowMessageBoxTimeout("歡迎使用資料匯出服務程式,本程式預設最小化到電腦托盤,1分鐘後正式啟動。", 
"程式啟動溫馨提示-視窗1分鐘內無操作會自動關閉", MessageBoxButtons.OK, 1000 * 60 * 1);

希望以上分享對初學朋友有些幫助,謝謝!
更多關注付義方技術部落格:http://blog.csdn.net/fuyifang
或者直接用手機掃描二維碼檢視更多博文:
付義方CSDN部落格二維碼