-
Notifications
You must be signed in to change notification settings - Fork 1
/
TopMostMessageBox.cs
53 lines (41 loc) · 1.88 KB
/
TopMostMessageBox.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System.Windows.Forms;
namespace EasyToInstall
{
public static class TopMostMessageBox
{
public static DialogResult Show(string message)
{
return Show(message, string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
public static DialogResult Show(string message, string title)
{
return Show(message, title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
public static DialogResult Show(string message, string title, MessageBoxButtons buttons)
{
return Show(message, title, buttons, MessageBoxIcon.Exclamation);
}
public static DialogResult Show(string message, string title, MessageBoxButtons buttons, MessageBoxIcon icon)
{
// Create a host form that is a TopMost window which will be the
// parent of the MessageBox.
Form topmostForm = new Form();
// We do not want anyone to see this window so position it off the
// visible screen and make it as small as possible
topmostForm.ShowInTaskbar = false;
topmostForm.Size = new System.Drawing.Size(1, 1);
topmostForm.StartPosition = FormStartPosition.Manual;
var rect = SystemInformation.VirtualScreen;
topmostForm.Location = new System.Drawing.Point(rect.Bottom + 10, rect.Right + 10);
topmostForm.Show();
// Make this form the active form and make it TopMost
topmostForm.Focus();
topmostForm.BringToFront();
topmostForm.TopMost = true;
// Finally show the MessageBox with the form just created as its owner
DialogResult result = MessageBox.Show(topmostForm, message, title, buttons, icon);
topmostForm.Dispose(); // clean it up all the way
return result;
}
}
}