yangyin
2025-02-24 b3b65a002fe68b1383314098790f5a153b87765f
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
#region Imports
 
using DPumpHydr.WinFrmUI.RLT.Colors;
using System;
using System.Drawing;
using System.Windows.Forms;
 
#endregion
 
namespace DPumpHydr.WinFrmUI.RLT.Controls
{
    #region RoyalMessageBox
 
    public class RoyalMessageBox : Forms.RoyalForm
    {
        private readonly RoyalButton okButton = new();
        private readonly RoyalButton yesButton = new();
        private readonly RoyalButton noButton = new();
        private readonly RoyalButton cancelButton = new();
        private readonly RoyalButton retryButton = new();
 
        public Form FormParent { get; set; }
        public string Content { get; set; }
        public string Caption { get; set; }
        public MessageBoxButtons Buttons { get; set; }
        public MessageBoxIcon Icon { get; set; }
 
        private static bool mode = true;
        public bool Mode
        {
            get => mode;
            set => mode = value;
        }
 
        public RoyalMessageBox() : base()
        {
            Font = new(new FontFamily("Segoe UI"), 9.75f, FontStyle.Regular);
            Moveable = false;
            ControlBox = false;
            ShowInTaskbar = false;
            StartPosition = FormStartPosition.CenterParent;
 
            okButton.Text = "Ok";
            okButton.Size = new(100, 35);
            okButton.Click += OkButton_Click;
 
            yesButton.Text = "Yes";
            yesButton.Size = new(100, 35);
            yesButton.Click += YesButton_Click;
 
            noButton.Text = "No";
            noButton.Size = new(100, 35);
            noButton.Click += NoButton_Click;
 
            cancelButton.Text = "Cancel";
            cancelButton.Size = new(100, 35);
            cancelButton.Click += CancelButton_Click;
 
            retryButton.Text = "Retry";
            retryButton.Size = new(100, 35);
            retryButton.Click += RetryButton_Click;
 
            Controls.Add(okButton);
            Controls.Add(yesButton);
            Controls.Add(noButton);
            Controls.Add(cancelButton);
            Controls.Add(retryButton);
 
            okButton.Hide();
            yesButton.Hide();
            noButton.Hide();
            cancelButton.Hide();
            retryButton.Hide();
        }
 
        private void RetryButton_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Retry;
        }
 
        private void CancelButton_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }
 
        private void NoButton_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.No;
        }
 
        private void YesButton_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Yes;
        }
 
        private void OkButton_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
        }
 
        public static DialogResult Show(Form form, string content)
        {
            return Show(form, content, "Royal Message Box Title", MessageBoxButtons.OK, MessageBoxIcon.None);
        }
 
        public static DialogResult Show(Form form, string content, string caption)
        {
            return Show(form, content, caption, MessageBoxButtons.OK, MessageBoxIcon.None);
        }
 
        public static DialogResult Show(Form form, string content, string caption, MessageBoxButtons buttons)
        {
            return Show(form, content, caption, buttons, MessageBoxIcon.None);
        }
 
        public static DialogResult Show(Form form, string content, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
        {
            return Show(form, content, caption, buttons, icon, mode);
        }
 
        public static DialogResult Show(Form form, string content, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, bool mode)
        {
            if (form == null)
            {
                throw new ArgumentNullException("RoyalMessageBox requires a valid form object in the first argument.");
            }
 
            RoyalMessageBox msgBox = new()
            {
                FormParent = form,
                Content = content,
                Caption = caption,
                Buttons = buttons,
                Icon = icon,
                Mode = mode,
 
                Size = form.Size,
                Location = form.Location
            };
 
            return msgBox.ShowDialog();
        }
 
        protected new DialogResult ShowDialog()
        {
            okButton.Hide();
            yesButton.Hide();
            noButton.Hide();
            cancelButton.Hide();
            retryButton.Hide();
 
            int messageBoxHeight = Height / 3;
            int buttonHeight = (messageBoxHeight * 2) - 45;
            int buttonWidth = okButton.Width;
            int oneButtonRight = Width - buttonWidth - 10;
            int twoButtonRight = Width - (buttonWidth * 2) - 20;
            int threeButtonRight = Width - (buttonWidth * 3) - 30;
 
            if (Buttons == MessageBoxButtons.OK)
            {
                okButton.Location = new(oneButtonRight, buttonHeight);
                okButton.Show();
            }
            else if (Buttons == MessageBoxButtons.OKCancel)
            {
                okButton.Location = new(twoButtonRight, buttonHeight);
                okButton.Show();
 
                cancelButton.Location = new(oneButtonRight, buttonHeight);
                cancelButton.Show();
            }
            else if (Buttons == MessageBoxButtons.YesNo)
            {
                yesButton.Location = new(twoButtonRight, buttonHeight);
                yesButton.Show();
 
                noButton.Location = new(oneButtonRight, buttonHeight);
                noButton.Show();
            }
            else if (Buttons == MessageBoxButtons.YesNoCancel)
            {
                yesButton.Location = new(threeButtonRight, buttonHeight);
                yesButton.Show();
 
                noButton.Location = new(twoButtonRight, buttonHeight);
                noButton.Show();
 
                cancelButton.Location = new(oneButtonRight, buttonHeight);
                cancelButton.Show();
            }
            else if (Buttons == MessageBoxButtons.RetryCancel)
            {
                retryButton.Location = new(twoButtonRight, buttonHeight);
                retryButton.Show();
 
                cancelButton.Location = new(oneButtonRight, buttonHeight);
                cancelButton.Show();
            }
            else
            {
                okButton.Location = new(oneButtonRight, buttonHeight);
                okButton.Show();
            }
 
            return base.ShowDialog();
        }
 
        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(150, Color.DarkGray)), e.ClipRectangle);
 
            Rectangle messageRect = new()
            {
                Size = new(Width, Height / 3),
                Location = new(0, (Height - (Height / 3)) / 2)
            };
 
            Font messageFont = new(Font.FontFamily, 12.75f, FontStyle.Regular);
 
            if (mode)
            {
                e.Graphics.FillRectangle(new SolidBrush(FormParent.BackColor), messageRect);
            }
            else
            {
                if (Icon is MessageBoxIcon.Warning or MessageBoxIcon.Exclamation)
                {
                    e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(255, 128, 0)), messageRect);
                }
                else if (Icon is MessageBoxIcon.Information or MessageBoxIcon.Asterisk)
                {
                    e.Graphics.FillRectangle(new SolidBrush(Color.Gray), messageRect);
                }
                else if (Icon is MessageBoxIcon.Error or MessageBoxIcon.Hand or MessageBoxIcon.Stop)
                {
                    e.Graphics.FillRectangle(new SolidBrush(Color.Crimson), messageRect);
                }
                else if (Icon == MessageBoxIcon.Question)
                {
                    e.Graphics.FillRectangle(new SolidBrush(Color.DodgerBlue), messageRect);
                }
                else
                {
                    e.Graphics.FillRectangle(new SolidBrush(FormParent.BackColor), messageRect);
                }
            }
 
            SolidBrush textBrush = new(FormParent.ForeColor);
            //SolidBrush backBrush = new(parent.BackColor);
 
            if (!string.IsNullOrEmpty(Caption))
            {
                e.Graphics.DrawString(Caption, messageFont, new SolidBrush(RoyalColors.AccentColor), new PointF(messageRect.Left + 10, messageRect.Top + 10));
            }
 
            if (Icon != MessageBoxIcon.None)
            {
                if (mode)
                {
                    if (Icon is MessageBoxIcon.Warning or MessageBoxIcon.Exclamation)
                    {
                        e.Graphics.DrawImage(Properties.Resources.Warning, new Rectangle(messageRect.Left + 10, messageRect.Top + 40, 64, 64));
                        if (!string.IsNullOrEmpty(Content))
                        {
                            e.Graphics.DrawString(Content, messageFont, textBrush, new PointF(messageRect.Left + 64 + 10, messageRect.Top + 18 + 40));
                        }
                    }
                    else if (Icon is MessageBoxIcon.Information or MessageBoxIcon.Asterisk)
                    {
                        e.Graphics.DrawImage(Properties.Resources.Information, new Rectangle(messageRect.Left + 10, messageRect.Top + 40, 64, 64));
                        if (!string.IsNullOrEmpty(Content))
                        {
                            e.Graphics.DrawString(Content, messageFont, textBrush, new PointF(messageRect.Left + 64 + 10, messageRect.Top + 18 + 40));
                        }
                    }
                    else if (Icon is MessageBoxIcon.Error or MessageBoxIcon.Hand or MessageBoxIcon.Stop)
                    {
                        e.Graphics.DrawImage(Properties.Resources.Error, new Rectangle(messageRect.Left + 10, messageRect.Top + 40, 64, 64));
                        if (!string.IsNullOrEmpty(Content))
                        {
                            e.Graphics.DrawString(Content, messageFont, textBrush, new PointF(messageRect.Left + 64 + 10, messageRect.Top + 18 + 40));
                        }
                    }
                    else if (Icon == MessageBoxIcon.Question)
                    {
                        e.Graphics.DrawImage(Properties.Resources.Question, new Rectangle(messageRect.Left + 10, messageRect.Top + 40, 64, 64));
                        if (!string.IsNullOrEmpty(Content))
                        {
                            e.Graphics.DrawString(Content, messageFont, textBrush, new PointF(messageRect.Left + 64 + 10, messageRect.Top + 18 + 40));
                        }
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(Content))
                        {
                            e.Graphics.DrawString(Content, messageFont, textBrush, new PointF(messageRect.Left + 10, messageRect.Top + 40));
                        }
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(Content))
                    {
                        e.Graphics.DrawString(Content, messageFont, textBrush, new PointF(messageRect.Left + 10, messageRect.Top + 40));
                    }
                }
 
            }
            else if (!string.IsNullOrEmpty(Content))
            {
                e.Graphics.DrawString(Content, messageFont, textBrush, new PointF(messageRect.Left + 10, messageRect.Top + 40));
            }
 
            base.OnPaint(e);
        }
    }
 
    #endregion
}