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
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
#region Imports
 
using DPumpHydr.WinFrmUI.RLT.Design.Metro;
using DPumpHydr.WinFrmUI.RLT.Enum.Metro;
using DPumpHydr.WinFrmUI.RLT.Extension.Metro;
using DPumpHydr.WinFrmUI.RLT.Interface.Metro;
using DPumpHydr.WinFrmUI.RLT.Manager;
using DPumpHydr.WinFrmUI.RLT.Native;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Timer = System.Timers.Timer;
 
#endregion
 
namespace DPumpHydr.WinFrmUI.RLT.Controls
{
    #region MetroNumeric
 
    [ToolboxItem(true)]
    [ToolboxBitmap(typeof(MetroNumeric), "Bitmaps.Numeric.bmp")]
    [Designer(typeof(MetroNumericDesigner))]
    [DefaultProperty("Text")]
    [ComVisible(true)]
    public class MetroNumeric : Control, IMetroControl
    {
        #region Interfaces
 
        [Category("Metro"), Description("Gets or sets the style associated with the control.")]
        public Style Style
        {
            get => StyleManager?.Style ?? _style;
            set
            {
                _style = value;
                switch (value)
                {
                    case Style.Light:
                        ApplyTheme();
                        break;
                    case Style.Dark:
                        ApplyTheme(Style.Dark);
                        break;
                    case Style.Custom:
                        ApplyTheme(Style.Custom);
                        break;
                    default:
                        ApplyTheme();
                        break;
                }
                Invalidate();
            }
        }
 
        [Category("Metro"), Description("Gets or sets the Style Manager associated with the control.")]
        public MetroStyleManager StyleManager
        {
            get => _styleManager;
            set { _styleManager = value; Invalidate(); }
        }
 
        [Category("Metro"), Description("Gets or sets the The Author name associated with the theme.")]
        public string ThemeAuthor { get; set; }
 
        [Category("Metro"), Description("Gets or sets the The Theme name associated with the theme.")]
        public string ThemeName { get; set; }
 
        #endregion Interfaces
 
        #region Global Vars
 
        private readonly Methods _mth;
        private readonly Utilites _utl;
 
        #endregion Global Vars
 
        #region Internal Vars
 
        private Style _style;
        private MetroStyleManager _styleManager;
        private Point _point;
        private int _value;
        private readonly Timer _holdTimer;
        private int _holdInterval = 10;
 
        private bool _isDerivedStyle = true;
        private int _maximum = 100;
        private int _minimum;
        private Color _backgroundColor;
        private Color _disabledForeColor;
        private Color _disabledBackColor;
        private Color _disabledBorderColor;
        private Color _borderColor;
        private Color _symbolsColor;
 
        #endregion Internal Vars
 
        #region Constructors
 
        public MetroNumeric()
        {
            SetStyle
            (
                ControlStyles.ResizeRedraw |
                ControlStyles.OptimizedDoubleBuffer |
                ControlStyles.SupportsTransparentBackColor,
                    true
            );
            UpdateStyles();
            base.Font = MetroFonts.SemiLight(10);
            BackColor = Color.Transparent;
            _mth = new Methods();
            _utl = new Utilites();
            ApplyTheme();
            _point = new(0, 0);
            _holdTimer = new Timer()
            {
                Interval = _holdInterval,
                AutoReset = true,
                Enabled = false
            };
            _holdTimer.Elapsed += HoldTimer_Tick;
        }
 
        #endregion Constructors
 
        #region Draw Control
 
        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
            Rectangle rect = new(0, 0, Width - 1, Height - 1);
 
            const char plus = '+';
            const char minus = '-';
 
            using SolidBrush bg = new(Enabled ? BackColor : DisabledBackColor);
            using Pen p = new(Enabled ? BorderColor : DisabledBorderColor);
            using SolidBrush s = new(Enabled ? SymbolsColor : DisabledForeColor);
            using SolidBrush tb = new(Enabled ? ForeColor : DisabledForeColor);
            using Font f2 = MetroFonts.SemiBold(18);
            using StringFormat sf = new() { LineAlignment = StringAlignment.Center };
            g.FillRectangle(bg, rect);
            g.DrawString(plus.ToString(), f2, s, new Rectangle(Width - 45, 1, 25, Height - 1), sf);
            g.DrawString(minus.ToString(), f2, s, new Rectangle(Width - 25, -1, 20, Height - 1), sf);
            g.DrawString(Value.ToString(), Font, tb, new Rectangle(0, 0, Width - 50, Height - 1), _mth.SetPosition(StringAlignment.Far));
            g.DrawRectangle(p, rect);
 
        }
 
        #endregion
 
        #region ApplyTheme
 
        private void ApplyTheme(Style style = Style.Light)
        {
            if (!IsDerivedStyle)
            {
                return;
            }
 
            switch (style)
            {
                case Style.Light:
                    ForeColor = Color.FromArgb(20, 20, 20);
                    BackColor = Color.White;
                    BorderColor = Color.FromArgb(150, 150, 150);
                    SymbolsColor = Color.FromArgb(128, 128, 128);
                    DisabledBackColor = Color.FromArgb(204, 204, 204);
                    DisabledBorderColor = Color.FromArgb(155, 155, 155);
                    DisabledForeColor = Color.FromArgb(136, 136, 136);
                    ThemeAuthor = "Taiizor";
                    ThemeName = "MetroLight";
                    UpdateProperties();
                    break;
                case Style.Dark:
                    ForeColor = Color.FromArgb(204, 204, 204);
                    BackColor = Color.FromArgb(34, 34, 34);
                    BorderColor = Color.FromArgb(110, 110, 110);
                    SymbolsColor = Color.FromArgb(110, 110, 110);
                    DisabledBackColor = Color.FromArgb(80, 80, 80);
                    DisabledBorderColor = Color.FromArgb(109, 109, 109);
                    DisabledForeColor = Color.FromArgb(109, 109, 109);
                    ThemeAuthor = "Taiizor";
                    ThemeName = "MetroDark";
                    UpdateProperties();
                    break;
                case Style.Custom:
                    if (StyleManager != null)
                    {
                        foreach (System.Collections.Generic.KeyValuePair<string, object> varkey in StyleManager.NumericDictionary)
                        {
                            switch (varkey.Key)
                            {
                                case "ForeColor":
                                    ForeColor = _utl.HexColor((string)varkey.Value);
                                    break;
                                case "BackColor":
                                    BackColor = _utl.HexColor((string)varkey.Value);
                                    break;
                                case "BorderColor":
                                    BorderColor = _utl.HexColor((string)varkey.Value);
                                    break;
                                case "SymbolsColor":
                                    SymbolsColor = _utl.HexColor((string)varkey.Value);
                                    break;
                                case "DisabledBackColor":
                                    DisabledBackColor = _utl.HexColor((string)varkey.Value);
                                    break;
                                case "DisabledBorderColor":
                                    DisabledBorderColor = _utl.HexColor((string)varkey.Value);
                                    break;
                                case "DisabledForeColor":
                                    DisabledForeColor = _utl.HexColor((string)varkey.Value);
                                    break;
                                default:
                                    return;
                            }
                        }
                    }
 
                    UpdateProperties();
                    break;
                default:
                    throw new ArgumentOutOfRangeException(nameof(style), style, null);
            }
        }
 
        private void UpdateProperties()
        {
            Invalidate();
        }
 
        #endregion Theme Changing
 
        #region Properties
 
        [Category("Metro"), Description("Gets or sets the hold interval of the Numeric.")]
        public int HoldInterval
        {
            get => _holdInterval;
            set
            {
                _holdInterval = value;
                _holdTimer.Interval = _holdInterval;
            }
        }
 
        [Category("Metro"), Description("Gets or sets the increment number of the Numeric.")]
        public int Increment { get; set; } = 1;
 
        [Category("Metro"), Description("Gets or sets the decrement number of the Numeric.")]
        public int Decrement { get; set; } = 1;
 
        [Category("Metro"), Description("Gets or sets the maximum number of the Numeric.")]
        public int Maximum
        {
            get => _maximum;
            set
            {
                _maximum = value;
                Refresh();
            }
        }
 
        [Category("Metro"), Description("Gets or sets the minimum number of the Numeric.")]
        public int Minimum
        {
            get => _minimum;
            set
            {
                _minimum = value;
                Refresh();
            }
        }
 
        [Category("Metro"), Description("Gets or sets the current number of the Numeric.")]
        public int Value
        {
            get => _value;
            set
            {
                if (value <= Maximum & value >= Minimum)
                {
                    _value = value;
                }
 
                Invalidate();
            }
        }
 
        [Browsable(false)]
        public sealed override Color BackColor => Color.Transparent;
 
        [Category("Metro"), Description("Gets or sets the control backcolor.")]
        [DisplayName("BackColor")]
        public Color BackgroundColor
        {
            get => _backgroundColor;
            set
            {
                _backgroundColor = value;
                Refresh();
            }
        }
 
        [Category("Metro"), Description("Gets or sets the forecolor of the control whenever while disabled.")]
        public Color DisabledForeColor
        {
            get => _disabledForeColor;
            set
            {
                _disabledForeColor = value;
                Refresh();
            }
        }
 
        [Category("Metro"), Description("Gets or sets disabled backcolor used by the control.")]
        public Color DisabledBackColor
        {
            get => _disabledBackColor;
            set
            {
                _disabledBackColor = value;
                Refresh();
            }
        }
 
        [Category("Metro"), Description("Gets or sets the border color while the control disabled.")]
        public Color DisabledBorderColor
        {
            get => _disabledBorderColor;
            set
            {
                _disabledBorderColor = value;
                Refresh();
            }
        }
 
        [Category("Metro"), Description("Gets or sets the border color.")]
        public Color BorderColor
        {
            get => _borderColor;
            set
            {
                _borderColor = value;
                Refresh();
            }
        }
 
        [Category("Metro"), Description("Gets or sets forecolor used by the control.")]
        public override Color ForeColor { get; set; }
 
        [Category("Metro"), Description("Gets or sets arrow color used by the control.")]
        public Color SymbolsColor
        {
            get => _symbolsColor;
            set
            {
                _symbolsColor = value;
                Refresh();
            }
        }
 
        [Category("Metro")]
        [Description("Gets or sets the whether this control reflect to parent(s) style. \n " +
                     "Set it to false if you want the style of this control be independent. ")]
        public bool IsDerivedStyle
        {
            get => _isDerivedStyle;
            set
            {
                _isDerivedStyle = value;
                Refresh();
            }
        }
 
        #endregion
 
        #region Events
 
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            _point = e.Location;
            Invalidate();
            Cursor = _point.X > Width - 50 ? Cursors.Hand : Cursors.IBeam;
        }
 
        protected override void OnClick(EventArgs e)
        {
            base.OnClick(e);
            Revaluate();
        }
 
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == User32.WM_SETCURSOR)
            {
                User32.SetCursor(User32.LoadCursor(IntPtr.Zero, User32.IDC_HAND));
                m.Result = IntPtr.Zero;
                return;
            }
 
            base.WndProc(ref m);
        }
 
        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            Height = 26;
        }
 
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            if (_point.X <= Width - 45 || _point.X >= Width - 3)
            {
                return;
            }
 
            if (e.Button == MouseButtons.Left)
            {
                _holdTimer.Enabled = true;
            }
 
            Invalidate();
        }
 
        protected override void OnMouseUp(MouseEventArgs e)
        {
            _holdTimer.Enabled = false;
        }
 
        private void HoldTimer_Tick(object sender, EventArgs args)
        {
            Revaluate();
        }
 
        private void Revaluate()
        {
            if (_point.X <= Width - 45 || _point.X >= Width - 3)
            {
                return;
            }
 
            if (_point.X > Width - 45 && _point.X < Width - 25)
            {
                if (Value + Increment <= Maximum)
                {
                    Value += Increment;
                }
                else
                {
                    Value = Maximum;
                }
            }
            else
            {
                if (Value - Decrement >= Minimum)
                {
                    Value -= Decrement;
                }
                else
                {
                    Value = Minimum;
                }
            }
        }
 
        #endregion
    }
 
    #endregion
}