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
#region Imports
 
using DPumpHydr.WinFrmUI.RLT.Colors;
using DPumpHydr.WinFrmUI.RLT.Util;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Windows.Forms;
 
#endregion
 
namespace DPumpHydr.WinFrmUI.RLT.Controls
{
    #region ForeverNumeric
 
    public class ForeverNumeric : Control
    {
        private int W;
        private int H;
        private readonly MouseStateForever State = MouseStateForever.None;
        private int x;
        private int y;
        private long _Value;
        private long _Min;
        private long _Max;
        private bool Bool;
 
        public event EventHandler ValueChanged;
 
        public long Value
        {
            get => _Value;
            set
            {
                if (value <= _Max & value >= _Min)
                {
                    _Value = value;
                    OnValueChanged(EventArgs.Empty);
                }
 
                Invalidate();
            }
        }
 
        public long Maximum
        {
            get => _Max;
            set
            {
                if (value > _Min)
                {
                    _Max = value;
                }
 
                if (_Value > _Max)
                {
                    _Value = _Max;
                    OnValueChanged(EventArgs.Empty);
                }
 
                Invalidate();
            }
        }
 
        public long Minimum
        {
            get => _Min;
            set
            {
                if (value < _Max)
                {
                    _Min = value;
                }
 
                if (_Value < _Min)
                {
                    _Value = Minimum;
                    OnValueChanged(EventArgs.Empty);
                }
 
                Invalidate();
            }
        }
 
        protected virtual void OnValueChanged(EventArgs e)
        {
            ValueChanged?.Invoke(this, e);
        }
 
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
 
            x = e.Location.X;
            y = e.Location.Y;
 
            Invalidate();
 
            if (e.X < Width - 23)
            {
                Cursor = Cursors.Default;
            }
            else
            {
                Cursor = Cursors.Hand;
            }
        }
 
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
 
            if (x > Width - 21 && x < Width - 3)
            {
                if (y < 15)
                {
                    if ((Value + 1) <= _Max)
                    {
                        _Value += 1;
                        OnValueChanged(EventArgs.Empty);
                    }
                }
                else
                {
                    if ((Value - 1) >= _Min)
                    {
                        _Value -= 1;
                        OnValueChanged(EventArgs.Empty);
                    }
                }
            }
            else
            {
                Bool = !Bool;
                Focus();
            }
 
            Invalidate();
        }
 
        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            base.OnKeyPress(e);
 
            try
            {
                if (Bool)
                {
                    _Value = Convert.ToInt64(_Value.ToString() + e.KeyChar.ToString());
                    OnValueChanged(EventArgs.Empty);
                }
 
                if (_Value > _Max)
                {
                    _Value = _Max;
                    OnValueChanged(EventArgs.Empty);
                }
 
                Invalidate();
            }
            catch
            {
            }
        }
 
        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
 
            if (e.KeyCode == Keys.Back)
            {
                Value = 0;
            }
        }
 
        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            Height = 30;
        }
 
        [Category("Colors")]
        public Color BaseColor { get; set; } = Color.FromArgb(45, 47, 49);
 
        [Category("Colors")]
        public Color ButtonColorA { get; set; } = ForeverLibrary.ForeverColor;
 
        [Category("Colors")]
        public Color ButtonColorB { get; set; } = Color.White;
 
        [Category("Colors")]
        public Color ButtonColorC { get; set; } = Color.White;
 
        public ForeverNumeric()
        {
            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.OptimizedDoubleBuffer | ControlStyles.SupportsTransparentBackColor, true);
            DoubleBuffered = true;
            Font = new("Segoe UI", 10);
            _Min = 0;
            _Max = 100;
            ForeColor = Color.Silver;
        }
 
        protected override void OnPaint(PaintEventArgs e)
        {
            //UpdateColors();
 
            Bitmap B = new(Width, Height);
            Graphics G = Graphics.FromImage(B);
            W = Width;
            H = Height;
 
            Rectangle Base = new(0, 0, W, H);
 
            Graphics _with18 = G;
            _with18.SmoothingMode = SmoothingMode.HighQuality;
            _with18.PixelOffsetMode = PixelOffsetMode.HighQuality;
            _with18.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
            _with18.Clear(BaseColor);
 
            //-- Base
            _with18.FillRectangle(new SolidBrush(BaseColor), Base);
            _with18.FillRectangle(new SolidBrush(ButtonColorA), new Rectangle(Width - 24, 0, 24, H));
 
            //-- Add
            _with18.DrawString("+", new Font("Segoe UI", 12), new SolidBrush(ButtonColorB), new Point(Width - 12, 8), ForeverLibrary.CenterSF);
            //-- Subtract
            _with18.DrawString("-", new Font("Segoe UI", 10, FontStyle.Bold), new SolidBrush(ButtonColorC), new Point(Width - 12, 22), ForeverLibrary.CenterSF);
 
            //-- Text
            _with18.DrawString(Value.ToString(), Font, new SolidBrush(ForeColor), new Rectangle(5, 1, W, H), new StringFormat { LineAlignment = StringAlignment.Center });
 
            base.OnPaint(e);
            G.Dispose();
            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            e.Graphics.DrawImageUnscaled(B, 0, 0);
            B.Dispose();
        }
 
        private void UpdateColors()
        {
            ForeverColors Colors = ForeverLibrary.GetColors(this);
 
            ButtonColorA = Colors.Forever;
        }
    }
 
    #endregion
}