tangxu
2024-10-16 56b9a880c8def4c0fc06f89919157fa238beeeb4
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
#region Imports
 
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
 
#endregion
 
namespace DPumpHydr.WinFrmUI.RLT.Controls
{
    #region SkyNumeric
 
    public class SkyNumeric : Control
    {
        #region "Properties & Flicker Control"
        private int X;
        private int Y;
        private long _Value;
        private long _Max;
        private long _Min;
        private bool Typing;
 
        public long Value
        {
            get => _Value;
            set
            {
                if (value <= _Max & value >= _Min)
                {
                    _Value = value;
                }
 
                Invalidate();
            }
        }
 
        public long Maximum
        {
            get => _Max;
            set
            {
                if (value > _Min)
                {
                    _Max = value;
                }
 
                if (_Value > _Max)
                {
                    _Value = _Max;
                }
 
                Invalidate();
            }
        }
 
        public long Minimum
        {
            get => _Min;
            set
            {
                if (value < _Max)
                {
                    _Min = value;
                }
 
                if (_Value < _Min)
                {
                    _Value = _Min;
                }
 
                Invalidate();
            }
        }
        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 OnResize(EventArgs e)
        {
            base.OnResize(e);
            Height = 30;
        }
 
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseClick(e);
            if (X > Width - 21 && X < Width - 3)
            {
                if (Y < 15)
                {
                    if ((Value + 1) <= _Max)
                    {
                        _Value += 1;
                    }
                }
                else
                {
                    if ((Value - 1) >= _Min)
                    {
                        _Value -= 1;
                    }
                }
            }
            else
            {
                Typing = !Typing;
                Focus();
            }
            Invalidate();
        }
 
        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            base.OnKeyPress(e);
            try
            {
                if (Typing)
                {
                    _Value = Convert.ToInt32(Convert.ToString(Convert.ToString(_Value) + e.KeyChar.ToString()));
                }
 
                if (_Value > _Max) { _Value = _Max; }
 
            }
            catch
            {
            }
        }
 
        protected override void OnKeyUp(KeyEventArgs e)
        {
            base.OnKeyUp(e);
            if (e.KeyCode == Keys.Up)
            {
                if ((Value + 1) <= _Max)
                {
                    _Value += 1;
                }
 
                Invalidate();
            }
            else if (e.KeyCode == Keys.Down)
            {
                if ((Value - 1) >= _Min)
                {
                    _Value -= 1;
                }
            }
            else if (e.KeyCode == Keys.Back)
            {
                string tmp = _Value.ToString();
                tmp = tmp.Remove(Convert.ToInt32(tmp.Length - 1));
                if (tmp.Length == 0) { tmp = "0"; }
                _Value = Convert.ToInt32(tmp);
            }
            Invalidate();
        }
 
        protected void DrawTriangle(Color Clr, Point FirstPoint, Point SecondPoint, Point ThirdPoint, Graphics G)
        {
            List<Point> points = new()
            {
                FirstPoint,
                SecondPoint,
                ThirdPoint
            };
            G.FillPolygon(new SolidBrush(Clr), points.ToArray());
        }
        #endregion
 
        #region Variables
        private SmoothingMode _SmoothingType = SmoothingMode.HighQuality;
        #endregion
 
        #region Settings
        public SmoothingMode SmoothingType
        {
            get => _SmoothingType;
            set
            {
                _SmoothingType = value;
                Invalidate();
            }
        }
 
        public Color TopTriangleColor { get; set; } = Color.FromArgb(27, 94, 137);
 
        public Color BotTriangleColor { get; set; } = Color.FromArgb(27, 94, 137);
 
        public Color BorderColorA { get; set; } = Color.FromArgb(220, 220, 220);
 
        public Color BorderColorB { get; set; } = Color.FromArgb(228, 228, 228);
 
        public Color BorderColorC { get; set; } = Color.FromArgb(191, 191, 191);
 
        public Color BorderColorD { get; set; } = Color.FromArgb(254, 254, 254);
 
        public Color ButtonBackColorA { get; set; } = Color.FromArgb(245, 245, 245);
 
        public Color ButtonBackColorB { get; set; } = Color.FromArgb(232, 232, 232);
 
        public Color ButtonBorderColorA { get; set; } = Color.FromArgb(252, 252, 252);
 
        public Color ButtonBorderColorB { get; set; } = Color.FromArgb(190, 190, 190);
 
        public Color ButtonBorderColorC { get; set; } = Color.FromArgb(200, 167, 167, 167);
 
        public Color ButtonBorderColorD { get; set; } = Color.FromArgb(188, 188, 188);
 
        public Color ButtonBorderColorE { get; set; } = Color.FromArgb(252, 252, 252);
        #endregion
 
        public SkyNumeric()
        {
            _Max = 9999999;
            _Min = 0;
            Cursor = Cursors.IBeam;
            SetStyle(ControlStyles.UserPaint | ControlStyles.SupportsTransparentBackColor, true);
            BackColor = Color.FromArgb(233, 233, 233);
            ForeColor = Color.FromArgb(27, 94, 137);
            DoubleBuffered = true;
            Font = new("Verdana", 6.75f, FontStyle.Bold);
        }
 
        protected override void OnPaint(PaintEventArgs e)
        {
            Bitmap B = new(Width, Height);
            Graphics G = Graphics.FromImage(B);
 
            G.Clear(BackColor);
            G.SmoothingMode = SmoothingType;
 
            LinearGradientBrush innerBorderBrush = new(new Rectangle(1, 1, Width - 3, Height - 3), BorderColorA, BorderColorB, 90);
            Pen innerBorderPen = new(innerBorderBrush);
            G.DrawRectangle(innerBorderPen, new Rectangle(1, 1, Width - 3, Height - 3));
            G.DrawLine(new(BorderColorC), new Point(1, 1), new Point(Width - 3, 1));
 
            G.DrawRectangle(new(BorderColorD), new Rectangle(0, 0, Width - 1, Height - 1));
 
            LinearGradientBrush buttonGradient = new(new Rectangle(Width - 23, 4, 19, 21), ButtonBackColorA, ButtonBackColorB, 90);
            G.FillRectangle(buttonGradient, buttonGradient.Rectangle);
            G.DrawRectangle(new(ButtonBorderColorA), new Rectangle(Width - 22, 5, 17, 19));
            G.DrawRectangle(new(ButtonBorderColorB), new Rectangle(Width - 23, 4, 19, 21));
            G.DrawLine(new(ButtonBorderColorC), new Point(Width - 22, Height - 4), new Point(Width - 5, Height - 4));
            G.DrawLine(new(ButtonBorderColorD), new Point(Width - 22, Height - 16), new Point(Width - 5, Height - 16));
            G.DrawLine(new(ButtonBorderColorE), new Point(Width - 22, Height - 15), new Point(Width - 5, Height - 15));
 
            //Top Triangle
            DrawTriangle(TopTriangleColor, new Point(Width - 17, 18), new Point(Width - 9, 18), new Point(Width - 13, 21), G);
 
            //Bottom Triangle
            DrawTriangle(BotTriangleColor, new Point(Width - 17, 10), new Point(Width - 9, 10), new Point(Width - 13, 7), G);
 
            G.DrawString(Value.ToString(), Font, new SolidBrush(ForeColor), new Rectangle(5, 0, Width - 1, Height - 1), new StringFormat { LineAlignment = StringAlignment.Center });
 
            e.Graphics.DrawImage(B, 0, 0);
            G.Dispose();
            B.Dispose();
        }
    }
 
    #endregion
}