[英]Automatically close messagebox in C#
我目前正在用 C# 開發一個應用程序,我在其中顯示一個 MessageBox。 如何在幾秒鍾后自動關閉消息框?
您將需要創建自己的窗口,代碼隱藏包含一個加載的處理程序和一個計時器處理程序,如下所示:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Timer t = new Timer();
t.Interval = 3000;
t.Elapsed += new ElapsedEventHandler(t_Elapsed);
t.Start();
}
void t_Elapsed(object sender, ElapsedEventArgs e)
{
this.Dispatcher.Invoke(new Action(()=>
{
this.Close();
}),null);
}
然后,您可以通過調用 ShowDialog() 來顯示您的自定義消息框:
MyWindow w = new MyWindow();
w.ShowDialog();
System.Windows.MessageBox.Show() 方法有一個重載,它將所有者 Window 作為第一個參數。 如果我們創建一個不可見的所有者窗口,然后在指定時間后關閉它,它的子消息框也會關閉。
這是完整的答案: https : //stackoverflow.com/a/20098381/2190520
[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError=true)] static extern int MessageBoxTimeout(IntPtr hwnd, String text, String title, uint type, Int16 wLanguageId, Int32 milliseconds); MessageBoxTimeout((System.IntPtr)0 ,"Message", "Title",0,0, 1000); //last parameter timeout in milisecond
這個庫https://github.com/DmitryGaravsky/AutoClosingMessageBox實現了一個 MessageBox,它在指定時間后關閉自己。
另請參閱此 stackoverflow 答案https://stackoverflow.com/a/14522952/4856020
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.