yangyin
2025-03-27 b0de14c2670b9ff0079dacfb4b7457b438368f11
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
#region Imports
 
using DPumpHydr.WinFrmUI.RLT.Child.Crown;
using DPumpHydr.WinFrmUI.RLT.Enum.Crown;
using System;
using System.ComponentModel;
using System.Windows.Forms;
using static DPumpHydr.WinFrmUI.RLT.Helper.CrownHelper;
 
#endregion
 
namespace DPumpHydr.WinFrmUI.RLT.Controls
{
    #region CrownMessageBox
 
    public partial class CrownMessageBox : CrownDialog
    {
        #region Field Region
 
        private readonly string _message;
        private int _maximumWidth = 350;
 
        #endregion
 
        #region Property Region
 
        [Description("Determines the maximum width of the message box when it autosizes around the displayed message.")]
        [DefaultValue(350)]
        public int MaximumWidth
        {
            get => _maximumWidth;
            set
            {
                _maximumWidth = value;
                CalculateSize();
            }
        }
 
        #endregion
 
        #region Constructor Region
 
        public CrownMessageBox()
        {
            InitializeComponent();
 
            ThemeProvider.Theme = ThemeProvider.Theme;
            BackColor = ThemeProvider.Theme.Colors.GreyBackground;
            lblText.ForeColor = ThemeProvider.Theme.Colors.LightText;
            Refresh();
        }
 
        public CrownMessageBox(string message, string title, DialogMessageBox icon, DialogButton buttons) : this()
        {
            Text = title;
            _message = message;
 
            DialogButtons = buttons;
            SetIcon(icon);
        }
 
        public CrownMessageBox(string message) : this(message, null, DialogMessageBox.None, DialogButton.Ok)
        { }
 
        public CrownMessageBox(string message, string title) : this(message, title, DialogMessageBox.None, DialogButton.Ok)
        { }
 
        public CrownMessageBox(string message, string title, DialogButton buttons) : this(message, title, DialogMessageBox.None, buttons)
        { }
 
        public CrownMessageBox(string message, string title, DialogMessageBox icon) : this(message, title, icon, DialogButton.Ok)
        { }
 
        #endregion
 
        #region Static Method Region
 
        public static DialogResult ShowInformation(string message, string caption, DialogButton buttons = DialogButton.Ok)
        {
            return ShowDialog(message, caption, DialogMessageBox.Information, buttons);
        }
 
        public static DialogResult ShowWarning(string message, string caption, DialogButton buttons = DialogButton.Ok)
        {
            return ShowDialog(message, caption, DialogMessageBox.Warning, buttons);
        }
 
        public static DialogResult ShowError(string message, string caption, DialogButton buttons = DialogButton.Ok)
        {
            return ShowDialog(message, caption, DialogMessageBox.Error, buttons);
        }
 
        private static DialogResult ShowDialog(string message, string caption, DialogMessageBox icon, DialogButton buttons)
        {
            using CrownMessageBox dlg = new(message, caption, icon, buttons);
            DialogResult result = dlg.ShowDialog();
            return result;
        }
 
        #endregion
 
        #region Method Region
 
        private void SetIcon(DialogMessageBox icon)
        {
            switch (icon)
            {
                case DialogMessageBox.None:
                    picIcon.Visible = false;
                    lblText.Left = 10;
                    break;
                case DialogMessageBox.Information:
                    picIcon.Image = Properties.Resources.info;
                    break;
                case DialogMessageBox.Warning:
                    picIcon.Image = Properties.Resources.warn;
                    break;
                case DialogMessageBox.Error:
                    picIcon.Image = Properties.Resources.err;
                    break;
            }
        }
 
        private void CalculateSize()
        {
            int width = 260; int height = 124;
 
            // Reset form back to original size
            Size = new(width, height);
 
            lblText.Text = string.Empty;
            lblText.AutoSize = true;
            lblText.Text = _message;
 
            // Set the minimum dialog size to whichever is bigger - the original size or the buttons.
            int minWidth = Math.Max(width, TotalButtonSize + 15);
 
            // Calculate the total size of the message
            int totalWidth = lblText.Right + 25;
 
            // Make sure we're not making the dialog bigger than the maximum size
            if (totalWidth < _maximumWidth)
            {
                // Width is smaller than the maximum width.
                // This means we can have a single-line message box.
                // Move the label to accomodate 
                width = totalWidth;
                lblText.Top = picIcon.Top + (picIcon.Height / 2) - (lblText.Height / 2);
            }
            else
            {
                // Width is larger than the maximum width.
                // Change the label size and wrap it.
                width = _maximumWidth;
                int offsetHeight = Height - picIcon.Height;
                lblText.AutoUpdateHeight = true;
                lblText.Width = width - lblText.Left - 25;
                height = offsetHeight + lblText.Height;
            }
 
            // Force the width to the minimum width
            if (width < minWidth)
            {
                width = minWidth;
            }
 
            // Set the new size of the dialog
            Size = new(width, height);
        }
 
        #endregion
 
        #region Event Handler Region
 
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
 
            CalculateSize();
        }
 
        #endregion
    }
 
    #endregion
}