Fixed auto closing message boxes that were not closing.

This commit is contained in:
ruri
2019-10-01 12:20:07 +02:00
parent e9f183688f
commit 2dd41fa1c7
+6 -32
View File
@@ -6,6 +6,7 @@ using RuriLib.ViewModels;
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
@@ -71,7 +72,11 @@ namespace OpenBullet
}
else
{
AutoClosingMessageBox.Show(message, level.ToString(), timeout * 1000);
var w = new System.Windows.Forms.Form() { Size = new System.Drawing.Size(0, 0) };
Task.Delay(TimeSpan.FromSeconds(timeout))
.ContinueWith((t) => w.Close(), TaskScheduler.FromCurrentSynchronizationContext());
System.Windows.Forms.MessageBox.Show(w, message, level.ToString());
}
}
@@ -145,35 +150,4 @@ namespace OpenBullet
catch { return (SolidColorBrush)App.Current.Resources["ForegroundMain"]; }
}
}
// Source: https://stackoverflow.com/questions/14522540/close-a-messagebox-after-several-seconds
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);
using (_timeoutTimer)
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("#32770", _caption); // lpClassName is #32770 for MessageBox
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);
}
}