This repository has been archived by the owner on Mar 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomMessageBox.cs
474 lines (382 loc) · 15.4 KB
/
CustomMessageBox.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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using TheArtOfDev.HtmlRenderer.WinForms;
namespace lxMsgBox {
class lxMessageBox : Form {
private const int CS_DROPSHADOW = 0x00020000;
private static lxMessageBox _msgBox;
private Panel _plHeader = new Panel();
private Panel _plFooter = new Panel();
private Panel _plIcon = new Panel();
private PictureBox _picIcon = new PictureBox();
private FlowLayoutPanel _flpButtons = new FlowLayoutPanel();
private Label _lblTitle;
private HtmlPanel _lblMessage = new HtmlPanel();
private List<Button> _buttonCollection = new List<Button>();
private static DialogResult _buttonResult = new DialogResult();
private static Timer _timer;
private static Point lastMousePos;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern bool MessageBeep(uint type);
private lxMessageBox() {
this.FormBorderStyle = FormBorderStyle.None;
this.BackColor = Color.FromArgb(45, 45, 48);
this.StartPosition = FormStartPosition.CenterScreen;
this.Padding = new Padding(3);
this.Width = 200;
_lblTitle = new Label();
_lblTitle.ForeColor = Color.White;
_lblTitle.Font = new System.Drawing.Font("Segoe UI", 18);
_lblTitle.Dock = DockStyle.Top;
_lblTitle.Height = 50;
_lblMessage.ForeColor = Color.White;
_lblMessage.BackColor = BackColor;
_lblMessage.Font = new Font("Segoe UI", 10);
_lblMessage.Dock = DockStyle.Fill;
_flpButtons.FlowDirection = FlowDirection.RightToLeft;
_flpButtons.Dock = DockStyle.Fill;
_plHeader.Dock = DockStyle.Fill;
_plHeader.Padding = new Padding(10);
_plHeader.Controls.Add(_lblMessage);
_plHeader.Controls.Add(_lblTitle);
_plFooter.Dock = DockStyle.Bottom;
_plFooter.Padding = new Padding(20);
_plFooter.BackColor = Color.FromArgb(37, 37, 38);
_plFooter.Height = 75;
_plFooter.Controls.Add(_flpButtons);
List<Control> controlCollection = new List<Control>();
controlCollection.Add(this);
controlCollection.Add(_lblTitle);
controlCollection.Add(_lblMessage);
controlCollection.Add(_flpButtons);
controlCollection.Add(_plHeader);
controlCollection.Add(_plFooter);
this.Controls.Add(_plHeader);
this.Controls.Add(_plIcon);
this.Controls.Add(_plFooter);
}
public static DialogResult Show(string message) {
_msgBox = new lxMessageBox();
_msgBox._lblMessage.Text = message;
lxMessageBox.InitButtons(Buttons.OK);
_msgBox.ShowDialog();
MessageBeep(0);
return _buttonResult;
}
public static DialogResult Show(string message, string title) {
_msgBox = new lxMessageBox();
_msgBox._lblMessage.Text = message;
_msgBox._lblTitle.Text = title;
_msgBox.Size = lxMessageBox.MessageSize(message);
lxMessageBox.InitButtons(Buttons.OK);
_msgBox.ShowDialog();
MessageBeep(0);
return _buttonResult;
}
public static DialogResult Show(string message, string title, Buttons buttons) {
_msgBox = new lxMessageBox();
_msgBox._lblMessage.Text = message;
_msgBox._lblTitle.Text = title;
_msgBox._plIcon.Hide();
lxMessageBox.InitButtons(buttons);
_msgBox.Size = lxMessageBox.MessageSize(message);
_msgBox.ShowDialog();
MessageBeep(0);
return _buttonResult;
}
public static DialogResult Show(string message, string title, Buttons buttons, Icon icon) {
_msgBox = new lxMessageBox();
_msgBox._lblMessage.Text = message;
_msgBox._lblTitle.Text = title;
lxMessageBox.InitButtons(buttons);
_msgBox.Size = lxMessageBox.MessageSize(message);
_msgBox.ShowDialog();
MessageBeep(0);
return _buttonResult;
}
public static DialogResult Show(string message, string title, Buttons buttons, Icon icon, AnimateStyle style) {
_msgBox = new lxMessageBox();
_msgBox._lblMessage.Text = message;
_msgBox._lblTitle.Text = title;
_msgBox.Height = 0;
lxMessageBox.InitButtons(buttons);
_timer = new Timer();
Size formSize = lxMessageBox.MessageSize(message);
switch (style) {
case lxMessageBox.AnimateStyle.SlideDown:
_msgBox.Size = new Size(formSize.Width, 0);
_timer.Interval = 1;
_timer.Tag = new ApplyAnimation(formSize, style);
break;
case lxMessageBox.AnimateStyle.FadeIn:
_msgBox.Size = formSize;
_msgBox.Opacity = 0;
_timer.Interval = 20;
_timer.Tag = new ApplyAnimation(formSize, style);
break;
case lxMessageBox.AnimateStyle.ZoomIn:
_msgBox.Size = new Size(formSize.Width + 100, formSize.Height + 100);
_timer.Tag = new ApplyAnimation(formSize, style);
_timer.Interval = 1;
break;
}
_timer.Tick += timer_Tick;
_timer.Start();
_msgBox.ShowDialog();
MessageBeep(0);
return _buttonResult;
}
static void timer_Tick(object sender, EventArgs e) {
Timer timer = (Timer)sender;
ApplyAnimation animate = (ApplyAnimation)timer.Tag;
switch (animate.Style) {
case lxMessageBox.AnimateStyle.SlideDown:
if (_msgBox.Height < animate.FormSize.Height) {
_msgBox.Height += 17;
_msgBox.Invalidate();
} else {
_timer.Stop();
_timer.Dispose();
}
break;
case lxMessageBox.AnimateStyle.FadeIn:
if (_msgBox.Opacity < 1) {
_msgBox.Opacity += 0.1;
_msgBox.Invalidate();
} else {
_timer.Stop();
_timer.Dispose();
}
break;
case lxMessageBox.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(Buttons buttons) {
switch (buttons) {
case lxMessageBox.Buttons.AbortRetryIgnore:
_msgBox.InitAbortRetryIgnoreButtons();
break;
case lxMessageBox.Buttons.OK:
_msgBox.InitOKButton();
break;
case lxMessageBox.Buttons.OKCancel:
_msgBox.InitOKCancelButtons();
break;
case lxMessageBox.Buttons.RetryCancel:
_msgBox.InitRetryCancelButtons();
break;
case lxMessageBox.Buttons.YesNo:
_msgBox.InitYesNoButtons();
break;
case lxMessageBox.Buttons.YesNoCancel:
_msgBox.InitYesNoCancelButtons();
break;
}
foreach (Button btn in _msgBox._buttonCollection) {
btn.ForeColor = Color.FromArgb(170, 170, 170);
btn.Font = new System.Drawing.Font("Segoe UI", 8);
btn.Padding = new Padding(3);
btn.FlatStyle = FlatStyle.Flat;
btn.Height = 30;
btn.FlatAppearance.BorderColor = Color.FromArgb(99, 99, 98);
btn.Cursor = Cursors.Hand;
_msgBox._flpButtons.Controls.Add(btn);
}
}
private static void InitIcon(Icon icon) {
switch (icon) {
case lxMessageBox.Icon.Application:
_msgBox._picIcon.Image = SystemIcons.Application.ToBitmap();
break;
case lxMessageBox.Icon.Exclamation:
_msgBox._picIcon.Image = SystemIcons.Exclamation.ToBitmap();
break;
case lxMessageBox.Icon.Error:
_msgBox._picIcon.Image = SystemIcons.Error.ToBitmap();
break;
case lxMessageBox.Icon.Info:
_msgBox._picIcon.Image = SystemIcons.Information.ToBitmap();
break;
case lxMessageBox.Icon.Question:
_msgBox._picIcon.Image = SystemIcons.Question.ToBitmap();
break;
case lxMessageBox.Icon.Shield:
_msgBox._picIcon.Image = SystemIcons.Shield.ToBitmap();
break;
case lxMessageBox.Icon.Warning:
_msgBox._picIcon.Image = SystemIcons.Warning.ToBitmap();
break;
}
}
private void InitAbortRetryIgnoreButtons() {
Button btnAbort = new Button();
btnAbort.Text = "Abort";
btnAbort.Click += ButtonClick;
Button btnRetry = new Button();
btnRetry.Text = "Retry";
btnRetry.Click += ButtonClick;
Button btnIgnore = new Button();
btnIgnore.Text = "Ignore";
btnIgnore.Click += ButtonClick;
this._buttonCollection.Add(btnAbort);
this._buttonCollection.Add(btnRetry);
this._buttonCollection.Add(btnIgnore);
}
private void InitOKButton() {
Button btnOK = new Button();
btnOK.Text = "Aceptar";
btnOK.Click += ButtonClick;
this._buttonCollection.Add(btnOK);
}
private void InitOKCancelButtons() {
Button btnOK = new Button();
btnOK.Text = "Actualizar";
btnOK.Click += ButtonClick;
Button btnCancel = new Button();
btnCancel.Text = "Cerrar";
btnCancel.Click += ButtonClick;
this._buttonCollection.Add(btnOK);
this._buttonCollection.Add(btnCancel);
}
private void InitRetryCancelButtons() {
Button btnRetry = new Button();
btnRetry.Text = "OK";
btnRetry.Click += ButtonClick;
Button btnCancel = new Button();
btnCancel.Text = "Cancelar";
btnCancel.Click += ButtonClick;
this._buttonCollection.Add(btnRetry);
this._buttonCollection.Add(btnCancel);
}
private void InitYesNoButtons() {
Button btnYes = new Button();
btnYes.Text = "Yes";
btnYes.Click += ButtonClick;
Button btnNo = new Button();
btnNo.Text = "No";
btnNo.Click += ButtonClick;
this._buttonCollection.Add(btnYes);
this._buttonCollection.Add(btnNo);
}
private void InitYesNoCancelButtons() {
Button btnYes = new Button();
btnYes.Text = "Abrir";
btnYes.Click += ButtonClick;
Button btnNo = new Button();
btnNo.Text = "Copiar URL";
btnNo.Click += ButtonClick;
Button btnCancel = new Button();
btnCancel.Text = "Cancel";
btnCancel.Click += ButtonClick;
this._buttonCollection.Add(btnYes);
this._buttonCollection.Add(btnNo);
this._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 "Actualizar":
_buttonResult = DialogResult.OK;
break;
case "Cancel":
_buttonResult = DialogResult.Cancel;
break;
case "Abrir":
_buttonResult = DialogResult.Yes;
break;
case "Copiar URL":
_buttonResult = DialogResult.No;
break;
}
_msgBox.Dispose();
}
private static Size MessageSize(string message) {
Graphics g = _msgBox.CreateGraphics();
int width = 350;
int height = 260;
SizeF size = g.MeasureString(message, new System.Drawing.Font("Segoe UI", 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;
width = 490;
height += (int)(size.Height) * lines;
}
return new Size(width, height);
}
protected override CreateParams CreateParams {
get {
CreateParams cp = base.CreateParams;
cp.ClassStyle |= CS_DROPSHADOW;
return cp;
}
}
public enum Buttons {
AbortRetryIgnore = 1,
OK = 2,
OKCancel = 3,
RetryCancel = 4,
YesNo = 5,
YesNoCancel = 6
}
public enum Icon {
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
}
class ApplyAnimation {
public Size FormSize;
public lxMessageBox.AnimateStyle Style;
public ApplyAnimation(Size formSize, lxMessageBox.AnimateStyle style) {
this.FormSize = formSize;
this.Style = style;
}
}
private void InitializeComponent() {
this.SuspendLayout();
//
// lxMessageBox
//
this.ClientSize = new System.Drawing.Size(284, 261);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "lxMessageBox";
this.ResumeLayout(false);
}
}
}