MessageBox.Show相信用.net開發程序的同仁們都非常熟悉。這個彈出窗口的函數有很多優點,可以編輯顯示內容,可以編輯窗口名稱,可以指定顯示哪些按鈕,可以指定提示圖標,可以自動排文同時調整窗體大小,可以...,可以的確實很多,信手拈來很方便。但無論多么強大的控件,總會有開發者自己的一些功能需求不能滿足。
以下針對MessageBox的自動關閉功能擴展了一個類和三個靜態方法。主要的思路有兩個:
1. 在調用MessageBox.Show之前打開一個計時器,計時器的間隔時間由外部通過函數傳入。到了計時器的間隔時間后,調用系統的API函數FindWindow和EndDialog來找到彈出窗口並關閉窗口。完成這一切之后停止計時器,釋放資源。
2. 直接調用API的隱藏函數MessageBoxTimeout。相較第一個方法來講,代碼量更加少,而且窗口的樣式和返回的結果更加多樣化,更加靈活。
在此我將兩個方法都完整的寫出來,以供參考。
/*------------------------------調用代碼---------------------------- * AutoClosedMsgBox.Show("噓...\r\n窗口5秒后關閉...", "提示", 5000); * AutoClosedMsgBox.Show("噓...\r\n窗口5秒后關閉...", "提示", 5000, 0); * AutoClosedMsgBox.Show("噓...\r\n窗口5秒后關閉...", "提示", 5000, MsgBoxStyle.OK); */ public class AutoClosedMsgBox { [DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)] static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll")] static extern bool EndDialog(IntPtr hDlg, int nResult); [DllImport("user32.dll")] static extern int MessageBoxTimeout(IntPtr hwnd, string txt, string caption, int wtype, int wlange, int dwtimeout); const int WM_CLOSE = 0x10; /// <summary> /// 彈出自動關閉的MessageBox窗口,只有“確定”按鈕 /// </summary> /// <param name="text">彈出窗口的顯示內容</param> /// <param name="caption">彈出窗口的標題</param> /// <param name="milliseconds">窗口持續顯示時間(毫秒)</param> /// <returns>固定返回DialogResult.OK</returns> public static DialogResult Show(string text, string caption, int milliseconds) { Timer timer = new Timer(); timer.Interval = milliseconds; timer.Tick += (a, b) => { IntPtr ptr = FindWindow(null, caption); if (ptr != IntPtr.Zero) EndDialog(ptr, 0); timer.Stop(); }; timer.Start(); MessageBox.Show(text, caption); return DialogResult.OK; } /// <summary> /// 彈出自動關閉的MessageBox窗口,有多種顯示方式 /// </summary> /// <param name="txt">彈出窗口的顯示內容</param> /// <param name="caption">彈出窗口的標題</param> /// <param name="style">窗口樣式(枚舉)</param> /// <param name="dwtimeout">窗口持續顯示時間(毫秒)</param> /// <returns>0-無顯示 1-確定 2-取消 3-終止 4-重試 5-忽略 6-是 7-否 10-重試 11-繼續 32000-系統關閉</returns> public static int Show(string text, string caption, int milliseconds, MsgBoxStyle style) { return MessageBoxTimeout(IntPtr.Zero, text, caption, (int)style, 0, milliseconds); } public static int Show(string text, string caption, int milliseconds, int style) { return MessageBoxTimeout(IntPtr.Zero, text, caption, style, 0, milliseconds); } } public enum MsgBoxStyle { OK = 0, OKCancel = 1, AbortRetryIgnore = 2, YesNoCancel = 3, YesNo = 4, RetryCancel = 5, CancelRetryContinue = 6, //紅叉 + ... RedCritical_OK = 16, RedCritical_OKCancel = 17, RedCritical_AbortRetryIgnore = 18, RedCritical_YesNoCancel = 19, RedCritical_YesNo = 20, RedCritical_RetryCancel = 21, RedCritical_CancelRetryContinue = 22, //藍問號 + ... BlueQuestion_OK = 32, BlueQuestion_OKCancel = 33, BlueQuestion_AbortRetryIgnore = 34, BlueQuestion_YesNoCancel = 35, BlueQuestion_YesNo = 36, BlueQuestion_RetryCancel = 37, BlueQuestion_CancelRetryContinue = 38, //黃嘆號 + ... YellowAlert_OK = 48, YellowAlert_OKCancel = 49, YellowAlert_AbortRetryIgnore = 50, YellowAlert_YesNoCancel = 51, YellowAlert_YesNo = 52, YellowAlert_RetryCancel = 53, YellowAlert_CancelRetryContinue = 54, //藍嘆號 + ... BlueInfo_OK = 64, BlueInfo_OKCancel = 65, BlueInfo_AbortRetryIgnore = 66, BlueInfo_YesNoCancel = 67, BlueInfo_YesNo = 68, BlueInfo_RetryCancel = 69, BlueInfo_CancelRetryContinue = 70, }
筆者(IcyJiang)著。轉載請注明出處:http://www.cnblogs.com/icyJ/archive/2012/11/08/AutoClosedMsgBox.html
留文一則交流,二則備用。