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

來源:互聯網
上載者:User

可設定指定時間自動消失的 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
或者直接用手機掃描二維碼查看更多博文:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.