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
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
#region Imports
 
using DPumpHydr.WinFrmUI.RLT.Enum.Crown;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using static DPumpHydr.WinFrmUI.RLT.Helper.CrownHelper;
 
#endregion
 
namespace DPumpHydr.WinFrmUI.RLT.Controls
{
    #region CrownCheckBox
 
    public class CrownCheckBox : System.Windows.Forms.CheckBox
    {
        #region Field Region
 
        private ControlState _controlState = ControlState.Normal;
 
        private bool _spacePressed;
 
        #endregion
 
        #region Property Region
 
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new Appearance Appearance => base.Appearance;
 
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new bool AutoEllipsis => base.AutoEllipsis;
 
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new Image BackgroundImage => base.BackgroundImage;
 
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new ImageLayout BackgroundImageLayout => base.BackgroundImageLayout;
 
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new bool FlatAppearance => false;
 
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new FlatStyle FlatStyle => base.FlatStyle;
 
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new Image Image => base.Image;
 
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new System.Drawing.ContentAlignment ImageAlign => base.ImageAlign;
 
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new int ImageIndex => base.ImageIndex;
 
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new string ImageKey => base.ImageKey;
 
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new ImageList ImageList => base.ImageList;
 
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new System.Drawing.ContentAlignment TextAlign => base.TextAlign;
 
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new TextImageRelation TextImageRelation => base.TextImageRelation;
 
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new bool ThreeState => base.ThreeState;
 
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new bool UseCompatibleTextRendering => false;
 
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new bool UseVisualStyleBackColor => false;
 
        #endregion
 
        #region Constructor Region
 
        public CrownCheckBox()
        {
            SetStyle
            (
                ControlStyles.SupportsTransparentBackColor |
                ControlStyles.OptimizedDoubleBuffer |
                ControlStyles.ResizeRedraw |
                ControlStyles.UserPaint,
                    true
            );
        }
 
        #endregion
 
        #region Method Region
 
        private void SetControlState(ControlState controlState)
        {
            if (_controlState != controlState)
            {
                _controlState = controlState;
                Invalidate();
            }
        }
 
        #endregion
 
        #region Event Handler Region
 
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
 
            if (_spacePressed)
            {
                return;
            }
 
            if (e.Button == MouseButtons.Left)
            {
                if (ClientRectangle.Contains(e.Location))
                {
                    SetControlState(ControlState.Pressed);
                }
                else
                {
                    SetControlState(ControlState.Hover);
                }
            }
            else
            {
                SetControlState(ControlState.Hover);
            }
        }
 
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
 
            if (!ClientRectangle.Contains(e.Location))
            {
                return;
            }
 
            SetControlState(ControlState.Pressed);
        }
 
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
 
            if (_spacePressed)
            {
                return;
            }
 
            SetControlState(ControlState.Normal);
        }
 
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
 
            if (_spacePressed)
            {
                return;
            }
 
            SetControlState(ControlState.Normal);
        }
 
        protected override void OnMouseCaptureChanged(EventArgs e)
        {
            base.OnMouseCaptureChanged(e);
 
            if (_spacePressed)
            {
                return;
            }
 
            Point location = Cursor.Position;
 
            if (!ClientRectangle.Contains(location))
            {
                SetControlState(ControlState.Normal);
            }
        }
 
        protected override void OnGotFocus(EventArgs e)
        {
            base.OnGotFocus(e);
 
            Invalidate();
        }
 
        protected override void OnLostFocus(EventArgs e)
        {
            base.OnLostFocus(e);
 
            _spacePressed = false;
 
            Point location = Cursor.Position;
 
            if (!ClientRectangle.Contains(location))
            {
                SetControlState(ControlState.Normal);
            }
            else
            {
                SetControlState(ControlState.Hover);
            }
        }
 
        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
 
            if (e.KeyCode == Keys.Space)
            {
                _spacePressed = true;
                SetControlState(ControlState.Pressed);
            }
        }
 
        protected override void OnKeyUp(KeyEventArgs e)
        {
            base.OnKeyUp(e);
 
            if (e.KeyCode == Keys.Space)
            {
                _spacePressed = false;
 
                Point location = Cursor.Position;
 
                if (!ClientRectangle.Contains(location))
                {
                    SetControlState(ControlState.Normal);
                }
                else
                {
                    SetControlState(ControlState.Hover);
                }
            }
        }
 
        #endregion
 
        #region Paint Region
 
        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Rectangle rect = new(0, 0, ClientSize.Width, ClientSize.Height);
 
            int size = ThemeProvider.Theme.Sizes.CheckBoxSize;
 
            Color textColor = ThemeProvider.Theme.Colors.LightText;
            Color borderColor = ThemeProvider.Theme.Colors.LightText;
            Color fillColor = ThemeProvider.Theme.Colors.LightestBackground;
 
            if (Enabled)
            {
                if (Focused)
                {
                    borderColor = ThemeProvider.Theme.Colors.BlueHighlight;
                    fillColor = ThemeProvider.Theme.Colors.BlueSelection;
                }
 
                if (_controlState == ControlState.Hover)
                {
                    borderColor = ThemeProvider.Theme.Colors.BlueHighlight;
                    fillColor = ThemeProvider.Theme.Colors.BlueSelection;
                }
                else if (_controlState == ControlState.Pressed)
                {
                    borderColor = ThemeProvider.Theme.Colors.GreyHighlight;
                    fillColor = ThemeProvider.Theme.Colors.GreySelection;
                }
            }
            else
            {
                textColor = ThemeProvider.Theme.Colors.DisabledText;
                borderColor = ThemeProvider.Theme.Colors.GreyHighlight;
                fillColor = ThemeProvider.Theme.Colors.GreySelection;
            }
 
            using (SolidBrush b = new(ThemeProvider.Theme.Colors.GreyBackground))
            {
                g.FillRectangle(b, rect);
            }
 
            using (Pen p = new(borderColor))
            {
                Rectangle boxRect = new(0, (rect.Height / 2) - (size / 2), size, size);
                g.DrawRectangle(p, boxRect);
            }
 
            if (Checked)
            {
                using SolidBrush b = new(fillColor);
                Rectangle boxRect = new(2, (rect.Height / 2) - ((size - 4) / 2), size - 3, size - 3);
                g.FillRectangle(b, boxRect);
            }
 
            using (SolidBrush b = new(textColor))
            {
                StringFormat stringFormat = new()
                {
                    LineAlignment = StringAlignment.Center,
                    Alignment = StringAlignment.Near
                };
 
                Rectangle modRect = new(size + 4, 0, rect.Width - size, rect.Height);
                g.DrawString(Text, Font, b, modRect, stringFormat);
            }
        }
 
        #endregion
    }
 
    #endregion
}