-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomMsgBox.cs
304 lines (268 loc) · 13.2 KB
/
CustomMsgBox.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
public enum WinVersionStyle {
WinXP,
Win7,
Win10
}
public partial class CustomMsgBoxForm : Form {
public CustomMsgBoxForm() {
Application.EnableVisualStyles();
InitializeComponent();
}
public string Prompt {
get => txtMain.Text;
set => txtMain.Text = value;
}
public string Title {
get => Text;
set => Text = value;
}
public TextBox MainText => txtMain;
public MessageBoxIcon FormLevel { get; set; }
private string getFormLevelString() {
return FormLevel switch {
MessageBoxIcon.Error => "Error",
MessageBoxIcon.Question => "Question",
MessageBoxIcon.Exclamation => "Exclamation",
MessageBoxIcon.Information => "Information",
_ => ""
};
}
public string Button1Text = null;
public string Button2Text = null;
public string Button3Text = null;
public WinVersionStyle WinVersion = WinVersionStyle.Win10;
public string DialogResultString = null;
public byte DialogResultButtonPressed = 0;
public MessageBoxButtons Buttons {
set {
switch (value) {
case MessageBoxButtons.OK:
Button1Text = "OK";
break;
case MessageBoxButtons.OKCancel:
Button1Text = "OK";
Button3Text = "Cancel";
break;
case MessageBoxButtons.AbortRetryIgnore:
Button1Text = "Abort";
Button2Text = "Retry";
Button3Text = "Ignore";
break;
case MessageBoxButtons.YesNoCancel:
Button1Text = "Yes";
Button2Text = "No";
Button3Text = "Cancel";
break;
case MessageBoxButtons.YesNo:
Button1Text = "Yes";
Button3Text = "No";
break;
case MessageBoxButtons.RetryCancel:
Button1Text = "Retry";
Button3Text = "Cancel";
break;
}
}
}
private void CustomMsgBoxForm_Load(object _, EventArgs __) {
if (Text == null) {
try {
Text = Owner.Text;
} catch {
Text = "CustomMsgBox";
}
}
var resources = new ComponentResourceManager(typeof(CustomMsgBoxForm));
Icon = (Icon)resources.GetObject(WinVersion.ToString() + "_" + getFormLevelString());
if (FormLevel != MessageBoxIcon.None)
pbxMain.Image = Icon.ToBitmap();
try {
Icon = Owner.Icon;
} catch {
// Icon = pbxMain.Image;
// doesn't work, and it's already set above anyway
if (FormLevel == MessageBoxIcon.None)
ShowIcon = false;
}
switch (FormLevel) {
case MessageBoxIcon.Error:
System.Media.SystemSounds.Beep.Play();
break;
case MessageBoxIcon.Exclamation:
System.Media.SystemSounds.Exclamation.Play();
break;
case MessageBoxIcon.Information:
System.Media.SystemSounds.Asterisk.Play();
break;
case MessageBoxIcon.Question:
System.Media.SystemSounds.Question.Play();
break;
}
// as TextBox doesn't have an AutoSize property like Label does, we have to do it manually
using (Graphics g = txtMain.CreateGraphics()) {
SizeF sizeF = g.MeasureString(txtMain.Text, txtMain.Font, new SizeF(txtMain.MaximumSize.Width, float.MaxValue));
txtMain.Height = (int)Math.Ceiling(sizeF.Height / txtMain.Font.Height) * txtMain.Font.Height; // restrain to line height
}
if (txtMain.Height > 13)
Height = 162 + (txtMain.Height - 13);
bool showB1 = !string.IsNullOrWhiteSpace(Button1Text);
bool showB2 = !string.IsNullOrWhiteSpace(Button2Text);
bool showB3 = !string.IsNullOrWhiteSpace(Button3Text);
btnAccept.Visible = showB1;
btnAccept.Text = Button1Text;
btnAnswerMid.Visible = showB2;
btnAnswerMid.Text = Button2Text;
btnCancel.Visible = showB3;
btnCancel.Text = Button3Text;
int getButtonRightX(Button btn) => btn.Location.X + btn.Width;
void setButtonX(Button btn, int x) => btn.Location = new Point(x, btn.Location.Y);
const int maxTotalWidth = 242;
const int buttonSpacing = 8;
int currentTotalWidth = (showB1 ? btnAccept.Width : 0) + (showB2 ? btnAnswerMid.Width : 0) + (showB3 ? btnCancel.Width : 0);
if (showB1 && showB2 && showB3)
currentTotalWidth += buttonSpacing * 2;
else if ((showB1 && showB2) || (showB1 && showB3) || (showB2 && showB3))
currentTotalWidth += buttonSpacing;
if (currentTotalWidth <= maxTotalWidth) { // if enough space, align to left point
const int button1LeftStartX = 152;
if (showB1)
setButtonX(btnAccept, button1LeftStartX);
if (showB1 && showB2)
setButtonX(btnAnswerMid, getButtonRightX(btnAccept) + buttonSpacing);
else if (showB2)
setButtonX(btnAnswerMid, button1LeftStartX);
if (showB2 && showB3)
setButtonX(btnCancel, getButtonRightX(btnAnswerMid) + buttonSpacing);
else if (showB1 && showB3)
setButtonX(btnCancel, getButtonRightX(btnAccept) + buttonSpacing);
else if (showB3)
setButtonX(btnCancel, button1LeftStartX);
} else { // if not enough space, align right
const int button3rightX = 393;
if (showB3)
setButtonX(btnCancel, button3rightX - btnCancel.Width);
if (showB3 && showB2)
setButtonX(btnAnswerMid, btnCancel.Location.X - btnAnswerMid.Width - buttonSpacing);
else if (showB2)
setButtonX(btnAnswerMid, button3rightX - btnAnswerMid.Width);
if (showB2 && showB1)
setButtonX(btnAccept, btnAnswerMid.Location.X - btnAccept.Width - buttonSpacing);
else if (showB3 && showB1)
setButtonX(btnAccept, btnCancel.Location.X - btnAccept.Width - buttonSpacing);
else if (showB1)
setButtonX(btnAccept, button3rightX - btnAccept.Width);
}
if (Owner != null)
CenterToParent();
else
CenterToScreen();
btnAccept.Select();
}
private DialogResult GetDialogResult(string buttonText) =>
Enum.TryParse(buttonText, true, out DialogResult result) ? result : DialogResult.None;
private void btnAccept_Click(object _, EventArgs __) {
DialogResult = GetDialogResult(Button1Text);
DialogResultString = Button1Text; // for use with custom buttons
DialogResultButtonPressed = 1;
if (GetDialogResult(Button1Text) == DialogResult.None)
Close();
}
private void btnAnswerMid_Click(object _, EventArgs __) {
DialogResult = GetDialogResult(Button2Text);
DialogResultString = Button2Text;
DialogResultButtonPressed = 2;
if (GetDialogResult(Button2Text) == DialogResult.None)
Close();
}
private void btnCancel_Click(object _, EventArgs __) {
DialogResult = GetDialogResult(Button3Text);
DialogResultString = Button3Text;
DialogResultButtonPressed = 3;
if (GetDialogResult(Button3Text) == DialogResult.None)
Close();
}
}
public partial class WalkmanLib {
/// <summary>Shows a custom messagebox</summary>
/// <param name="text">The text to display in the message box.</param>
/// <param name="caption">The text to display in the title bar of the message box.</param>
/// <param name="buttons">One of the <see cref="MessageBoxButtons"/> values that specifies which buttons to display in the message box.</param>
/// <param name="style">One of the following: <see cref="MessageBoxIcon.Error"/>, <see cref="MessageBoxIcon.Question"/>, <see cref="MessageBoxIcon.Exclamation"/> or <see cref="MessageBoxIcon.Information"/>.</param>
/// <param name="winVersion">Windows version to use style icons from. Default: <see cref="WinVersionStyle.Win10"/></param>
/// <param name="ownerForm">Used to set the Window's Icon. Set to <see langword="this"/> to copy the current form's icon</param>
/// <returns>The button the user selected, or <see cref="DialogResult.Cancel"/> if Window X button selected.</returns>
public static DialogResult CustomMsgBox(string text, string caption = null, MessageBoxButtons buttons = 0, MessageBoxIcon style = 0,
WinVersionStyle winVersion = WinVersionStyle.Win10, Form ownerForm = null) {
var formToShow = new CustomMsgBoxForm() {
Prompt = text,
Title = caption,
Buttons = buttons,
FormLevel = style,
WinVersion = winVersion,
Owner = ownerForm,
ShowInTaskbar = false
};
return formToShow.ShowDialog();
}
/// <summary>Shows a custom messagebox with custom buttons</summary>
/// <param name="text">The text to display in the message box.</param>
/// <param name="caption">The text to display in the title bar of the message box.</param>
/// <param name="customButton1">Text to show on the first button</param>
/// <param name="customButton2">Text to show on the second button. If left out or set to <see langword="null"/>, this button will be hidden.</param>
/// <param name="customButton3">Text to show on the third button. If left out or set to <see langword="null"/>, this button will be hidden.</param>
/// <param name="style">One of the following: <see cref="MessageBoxIcon.Error"/>, <see cref="MessageBoxIcon.Question"/>, <see cref="MessageBoxIcon.Exclamation"/> or <see cref="MessageBoxIcon.Information"/>.</param>
/// <param name="winVersion">Windows version to use style icons from. Default: <see cref="WinVersionStyle.Win10"/></param>
/// <param name="ownerForm">Used to set the Window's Icon. Set to <see langword="this"/> to copy the current form's icon</param>
/// <returns>Text of the button the user selected, or null if Window X button selected.</returns>
public static string CustomMsgBox(string text, string caption, string customButton1, string customButton2 = null, string customButton3 = null,
MessageBoxIcon style = 0, WinVersionStyle winVersion = WinVersionStyle.Win10, Form ownerForm = null) {
var formToShow = new CustomMsgBoxForm() {
Prompt = text,
Title = caption,
Button1Text = customButton1,
Button2Text = customButton2,
Button3Text = customButton3,
FormLevel = style,
WinVersion = winVersion,
Owner = ownerForm,
ShowInTaskbar = false
};
formToShow.ShowDialog();
return formToShow.DialogResultString;
}
/// <summary>Shows a custom messagebox with custom buttons</summary>
/// <param name="text">The text to display in the message box.</param>
/// <param name="caption">The text to display in the title bar of the message box.</param>
/// <param name="customButton1">Text to show on the first button</param>
/// <param name="customButton2">Text to show on the second button. If left out or set to <see langword="null"/>, this button will be hidden.</param>
/// <param name="customButton3">Text to show on the third button. If left out or set to <see langword="null"/>, this button will be hidden.</param>
/// <param name="style">One of the following: <see cref="MessageBoxIcon.Error"/>, <see cref="MessageBoxIcon.Question"/>, <see cref="MessageBoxIcon.Exclamation"/> or <see cref="MessageBoxIcon.Information"/>.</param>
/// <param name="winVersion">Windows version to use style icons from. Default: <see cref="WinVersionStyle.Win10"/></param>
/// <param name="ownerForm">Used to set the Window's Icon. Set to <see langword="this"/> to copy the current form's icon</param>
/// <returns>Which button the user selected: 1-3. 0 = Window X button selected.</returns>
public static byte CustomMsgBoxBTN(string text, string caption, string customButton1, string customButton2 = null, string customButton3 = null,
MessageBoxIcon style = 0, WinVersionStyle winVersion = WinVersionStyle.Win10, Form ownerForm = null) {
var formToShow = new CustomMsgBoxForm() {
Prompt = text,
Title = caption,
Button1Text = customButton1,
Button2Text = customButton2,
Button3Text = customButton3,
FormLevel = style,
WinVersion = winVersion,
Owner = ownerForm,
ShowInTaskbar = false
};
formToShow.ShowDialog();
return formToShow.DialogResultButtonPressed;
}
}
// visual styles is enabled in a Windows Forms Application with the following:
//static void Main() {
// Application.EnableVisualStyles();
// ...
//}