-
Notifications
You must be signed in to change notification settings - Fork 1
/
DismissableMessageBox.cs
35 lines (29 loc) · 1.07 KB
/
DismissableMessageBox.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
public class DismissableMessageBox
{
private const int IDNO = 7; // The ID for the 'No' button
private string _caption;
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool EndDialog(IntPtr hDlg, IntPtr nResult);
public DismissableMessageBox(string caption)
{
_caption = caption;
}
public DialogResult Show(string text, MessageBoxButtons buttons, MessageBoxIcon icon = MessageBoxIcon.None)
{
return MessageBox.Show(text, _caption, buttons, icon);
}
public void Dismiss(int buttonId = IDNO)
{
IntPtr hWnd = FindWindow(null, _caption); // Find the MessageBox by its title
if (hWnd != IntPtr.Zero)
{
EndDialog(hWnd, (IntPtr)buttonId); // Close the MessageBox with the specified result
}
}
}