-
Notifications
You must be signed in to change notification settings - Fork 0
/
MsgBox.cs
614 lines (515 loc) · 19.4 KB
/
MsgBox.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Timer = System.Windows.Forms.Timer;
namespace SNESMiniLuaCompiler
{
public partial class MsgBox : Form
{
private const int CS_DROPSHADOW = 0x00020000;
private static MsgBox _msgBox;
private readonly Panel _plHeader = new Panel();
private readonly Panel _plFooter = new Panel();
private readonly Panel _plIcon = new Panel();
private readonly PictureBox _picIcon = new PictureBox();
private readonly FlowLayoutPanel _flpButtons = new FlowLayoutPanel();
private readonly Label _lblTitle;
private readonly Label _lblMessage;
private readonly List<Button> _buttonCollection = new List<Button>();
private static DialogResult _buttonResult;
private static Timer _timer;
[System.Diagnostics.CodeAnalysis.SuppressMessage("Globalization", "CA1304:Specify CultureInfo", Justification = "<Pending>")]
public MsgBox()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
FormBorderStyle = FormBorderStyle.None;
BackColor = Color.FromArgb(39, 41, 45);
StartPosition = FormStartPosition.CenterScreen;
Padding = new Padding(3);
//Width = 400;
Icon = (Icon)resources.GetObject("$this.Icon");
_lblTitle = new Label
{
ForeColor = Color.White,
Font = new Font("Microsoft Sans Serif", 18),
Dock = DockStyle.Top,
Height = 50
};
_lblMessage = new Label
{
ForeColor = Color.White,
Font = new Font("Microsoft Sans Serif", 10),
Dock = DockStyle.Fill
};
_flpButtons.FlowDirection = FlowDirection.RightToLeft;
_flpButtons.Dock = DockStyle.Fill;
_plHeader.Dock = DockStyle.Fill;
_plHeader.Padding = new Padding(20);
_plHeader.Controls.Add(_lblMessage);
_plHeader.Controls.Add(_lblTitle);
_plFooter.Dock = DockStyle.Bottom;
_plFooter.Padding = new Padding(10);
_plFooter.BackColor = Color.Transparent;
_plFooter.Height = 60;
_plFooter.Controls.Add(_flpButtons);
_picIcon.Width = 38;
_picIcon.Height = 38;
_picIcon.Location = new Point(15, 15);
_picIcon.SizeMode = PictureBoxSizeMode.StretchImage;
_picIcon.BringToFront();
_plIcon.Dock = DockStyle.Left;
_plIcon.Padding = new Padding(10);
_plIcon.Width = 70;
_plIcon.Controls.Add(_picIcon);
Controls.Add(_plHeader);
Controls.Add(_plIcon);
Controls.Add(_plFooter);
}
public MsgBox(Panel plHeader, Panel plFooter, Panel plIcon, PictureBox picIcon, FlowLayoutPanel flpButtons, Label lblTitle, Label lblMessage, List<Button> buttonCollection, IContainer components)
{
_plHeader = plHeader;
_plFooter = plFooter;
_plIcon = plIcon;
_picIcon = picIcon;
_flpButtons = flpButtons;
_lblTitle = lblTitle;
_lblMessage = lblMessage;
_buttonCollection = buttonCollection;
this.components = components;
}
public static void Show(string message)
{
_msgBox = new MsgBox();
_msgBox._lblMessage.Text = message;
_msgBox.ShowDialog();
NativeMethods.MessageBeepNative(0);
}
public static void Show(string message, string title)
{
AppUtils.ThrowArgNull(message);
_msgBox = new MsgBox();
_msgBox._lblMessage.Text = message;
_msgBox._lblTitle.Text = title;
_msgBox.StartPosition = FormStartPosition.CenterParent;
_msgBox.Size = MessageSize(message);
_msgBox.ShowDialog();
NativeMethods.MessageBeepNative(0);
}
public static DialogResult Show(string message, string title, ButtonType buttons)
{
AppUtils.ThrowArgNull(message);
_msgBox = new MsgBox();
_msgBox._lblMessage.Text = message;
_msgBox._lblTitle.Text = title;
_msgBox._plIcon.Hide();
InitButtons(buttons);
_msgBox.Size = MessageSize(message);
_msgBox.ShowDialog();
NativeMethods.MessageBeepNative(0);
return _buttonResult;
}
public static DialogResult Show(string message, string title, ButtonType buttons, Ico icon)
{
AppUtils.ThrowArgNull(message);
_msgBox = new MsgBox();
_msgBox._lblMessage.Text = message;
_msgBox._lblTitle.Text = title;
InitButtons(buttons);
InitIcon(icon);
_msgBox.Size = MsgBox.MessageSize(message);
_msgBox.ShowDialog();
NativeMethods.MessageBeepNative(0);
return _buttonResult;
}
public static DialogResult Show(string message, string title, ButtonType buttons, Ico icon, AnimateStyle style)
{
AppUtils.ThrowArgNull(message);
_msgBox = new MsgBox();
_msgBox._lblMessage.Text = message;
_msgBox._lblTitle.Text = title;
_msgBox.Height = 0;
InitButtons(buttons);
InitIcon(icon);
_timer = new Timer();
Size formSize = MessageSize(message);
switch (style)
{
case AnimateStyle.SlideDown:
_msgBox.Size = new Size(formSize.Width, 0);
_timer.Interval = 1;
_timer.Tag = new AnimateMsgBox(formSize, style);
break;
case AnimateStyle.FadeIn:
_msgBox.AutoSize = true;
_msgBox.AutoSizeMode = AutoSizeMode.GrowOnly;
_msgBox.Size = formSize;
_msgBox.Opacity = 0;
_timer.Interval = 20;
_timer.Tag = new AnimateMsgBox(formSize, style);
break;
case AnimateStyle.FadeInHelp:
_msgBox.Size = new Size(500, 360);
_msgBox.Opacity = 0;
_timer.Interval = 20;
_timer.Tag = new AnimateMsgBox(formSize, style);
break;
case AnimateStyle.ZoomIn:
_msgBox.Size = new Size(formSize.Width + 100, formSize.Height + 100);
_timer.Tag = new AnimateMsgBox(formSize, style);
_timer.Interval = 1;
break;
}
_timer.Tick += Timer_Tick;
_timer.Start();
_msgBox.ShowDialog();
NativeMethods.MessageBeepNative(0);
return _buttonResult;
}
static void Timer_Tick(object sender, EventArgs e)
{
Timer timer = (Timer)sender;
AnimateMsgBox animate = (AnimateMsgBox)timer.Tag;
switch (animate.Style)
{
case AnimateStyle.SlideDown:
if (_msgBox.Height < animate.FormSize.Height)
{
_msgBox.Height += 17;
_msgBox.Invalidate();
}
else
{
_timer.Stop();
_timer.Dispose();
}
break;
case AnimateStyle.FadeIn:
if (_msgBox.Opacity < 1)
{
_msgBox.Opacity += 0.1;
_msgBox.Invalidate();
}
else
{
_timer.Stop();
_timer.Dispose();
}
break;
case AnimateStyle.FadeInHelp:
if (_msgBox.Opacity < 1)
{
_msgBox.Opacity += 0.1;
_msgBox.Invalidate();
}
else
{
_timer.Stop();
_timer.Dispose();
}
break;
case AnimateStyle.ZoomIn:
if (_msgBox.Width > animate.FormSize.Width)
{
_msgBox.Width -= 17;
_msgBox.Invalidate();
}
if (_msgBox.Height > animate.FormSize.Height)
{
_msgBox.Height -= 17;
_msgBox.Invalidate();
}
break;
}
}
private static void InitButtons(ButtonType buttons)
{
switch (buttons)
{
case ButtonType.AbortRetryIgnore:
_msgBox.InitAbortRetryIgnoreButtons();
break;
case ButtonType.OK:
_msgBox.InitOKButton();
break;
case ButtonType.OKCancel:
_msgBox.InitOKCancelButtons();
break;
case ButtonType.RetryCancel:
_msgBox.InitRetryCancelButtons();
break;
case ButtonType.YesNo:
_msgBox.InitYesNoButtons();
break;
case ButtonType.YesNoCancel:
_msgBox.InitYesNoCancelButtons();
break;
}
foreach (Button btn in _msgBox._buttonCollection)
{
btn.ForeColor = Color.FromArgb(170, 170, 170);
btn.Font = new Font("Microsoft Sans Serif", 8.25F, FontStyle.Bold, GraphicsUnit.Point, ((byte)(0)));
btn.Padding = new Padding(3);
btn.FlatStyle = FlatStyle.Flat;
btn.Height = 30;
btn.FlatAppearance.BorderSize = 2;
btn.FlatAppearance.BorderColor = Color.FromArgb(99, 99, 98);
//btn.Size = new Size(77, 28);
btn.FlatAppearance.MouseDownBackColor = Color.FromArgb(((int)(((byte)(47)))), ((int)(((byte)(49)))), ((int)(((byte)(54)))));
btn.FlatAppearance.MouseOverBackColor = Color.FromArgb(((int)(((byte)(47)))), ((int)(((byte)(49)))), ((int)(((byte)(54)))));
btn.UseVisualStyleBackColor = false;
_msgBox._flpButtons.Controls.Add(btn);
}
}
private static void InitIcon(Ico icon)
{
switch (icon)
{
case Ico.Application:
_msgBox._picIcon.Image = Properties.Resources.icn_application;
break;
case Ico.Exclamation:
_msgBox._picIcon.Image = Properties.Resources.icn_ok;
break;
case Ico.Error:
_msgBox._picIcon.Image = Properties.Resources.icn_stop;
break;
case Ico.Info:
_msgBox._picIcon.Image = Properties.Resources.icn_info;
break;
case Ico.Question:
_msgBox._picIcon.Image = Properties.Resources.icn_question;
break;
case Ico.Shield:
_msgBox._picIcon.Image = SystemIcons.Shield.ToBitmap();
break;
case Ico.Warning:
_msgBox._picIcon.Image = Properties.Resources.icn_warn;
break;
}
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Globalization", "CA1303:Do not pass literals as localized parameters", Justification = "<Pending>")]
private void InitAbortRetryIgnoreButtons()
{
Button btnAbort = new Button
{
Text = "Abort"
};
btnAbort.Click += ButtonClick;
Button btnRetry = new Button
{
Text = "Retry"
};
btnRetry.Click += ButtonClick;
Button btnIgnore = new Button
{
Text = "Ignore"
};
btnIgnore.Click += ButtonClick;
_buttonCollection.Add(btnAbort);
_buttonCollection.Add(btnRetry);
_buttonCollection.Add(btnIgnore);
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Globalization", "CA1303:Do not pass literals as localized parameters", Justification = "<Pending>")]
private void InitOKButton()
{
Button btnOK = new Button
{
Text = "OK"
};
btnOK.Click += ButtonClick;
_buttonCollection.Add(btnOK);
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Globalization", "CA1303:Do not pass literals as localized parameters", Justification = "<Pending>")]
private void InitOKCancelButtons()
{
Button btnOK = new Button
{
Text = "OK"
};
btnOK.Click += ButtonClick;
Button btnCancel = new Button
{
Text = "Cancel"
};
btnCancel.Click += ButtonClick;
_buttonCollection.Add(btnOK);
_buttonCollection.Add(btnCancel);
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Globalization", "CA1303:Do not pass literals as localized parameters", Justification = "<Pending>")]
private void InitRetryCancelButtons()
{
Button btnRetry = new Button
{
Text = "OK"
};
btnRetry.Click += ButtonClick;
Button btnCancel = new Button
{
Text = "Cancel"
};
btnCancel.Click += ButtonClick;
_buttonCollection.Add(btnRetry);
_buttonCollection.Add(btnCancel);
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Globalization", "CA1303:Do not pass literals as localized parameters", Justification = "<Pending>")]
private void InitYesNoButtons()
{
Button btnYes = new Button
{
Text = "Yes"
};
btnYes.Click += ButtonClick;
Button btnNo = new Button
{
Text = "No"
};
btnNo.Click += ButtonClick;
_buttonCollection.Add(btnYes);
_buttonCollection.Add(btnNo);
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Globalization", "CA1303:Do not pass literals as localized parameters", Justification = "<Pending>")]
private void InitYesNoCancelButtons()
{
Button btnYes = new Button
{
Text = "Abort"
};
btnYes.Click += ButtonClick;
Button btnNo = new Button
{
Text = "Retry"
};
btnNo.Click += ButtonClick;
Button btnCancel = new Button
{
Text = "Cancel"
};
btnCancel.Click += ButtonClick;
_buttonCollection.Add(btnYes);
_buttonCollection.Add(btnNo);
_buttonCollection.Add(btnCancel);
}
private static void ButtonClick(object sender, EventArgs e)
{
Button btn = (Button)sender;
switch (btn.Text)
{
case "Abort":
_buttonResult = DialogResult.Abort;
break;
case "Retry":
_buttonResult = DialogResult.Retry;
break;
case "Ignore":
_buttonResult = DialogResult.Ignore;
break;
case "OK":
_buttonResult = DialogResult.OK;
break;
case "Cancel":
_buttonResult = DialogResult.Cancel;
break;
case "Yes":
_buttonResult = DialogResult.Yes;
break;
case "No":
_buttonResult = DialogResult.No;
break;
}
_msgBox.Dispose();
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope", Justification = "<Pending>")]
private static Size MessageSize(string message)
{
Graphics g = _msgBox.CreateGraphics();
int width = 350;
int height = 230;
SizeF size = g.MeasureString(message, new Font("Microsoft Sans Serif", 10));
if (message.Length < 150)
{
if ((int)size.Width > 350)
{
width = (int)size.Width;
}
}
else
{
string[] groups = (from Match m in Regex.Matches(message, ".{1,180}") select m.Value).ToArray();
int lines = groups.Length + 1;
width = 700;
height += (int)(size.Height + 10) * lines;
}
return new Size(width, height);
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ClassStyle |= CS_DROPSHADOW;
return cp;
}
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope", Justification = "<Pending>")]
protected override void OnPaint(PaintEventArgs e)
{
AppUtils.ThrowArgNull(e);
base.OnPaint(e);
Graphics g = e.Graphics;
Rectangle rect = new Rectangle(new Point(0, 0), new Size(Width - 1, Height - 1));
Pen pen = new Pen(Color.FromArgb(0, 0, 0));
g.DrawRectangle(pen, rect);
}
public enum ButtonType
{
AbortRetryIgnore = 1,
OK = 2,
OKCancel = 3,
RetryCancel = 4,
YesNo = 5,
YesNoCancel = 6
}
public enum Ico
{
Application = 1,
Exclamation = 2,
Error = 3,
Warning = 4,
Info = 5,
Question = 6,
Shield = 7,
Search = 8
}
public enum AnimateStyle
{
SlideDown = 1,
FadeIn = 2,
ZoomIn = 3,
FadeInHelp = 4
}
}
class AnimateMsgBox
{
public Size FormSize;
public MsgBox.AnimateStyle Style;
public AnimateMsgBox(Size formSize, MsgBox.AnimateStyle style)
{
FormSize = formSize;
Style = style;
}
}
public static class NativeMethods
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern bool MessageBeep(uint type);
public static void MessageBeepNative(uint type)
{
MessageBeep(type);
}
}
}