FlatUpDown.cs
9.5 KB
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace Asa.Theme
{
public partial class FlatUpDown : FlatBase
{
private bool _upDown; //鼠标按下
private bool _downDown; //鼠标按下
private bool _upOver; //鼠标经过
private bool _downOver; //鼠标经过
private Rectangle _borderRect = new Rectangle();
private Rectangle _upRect = new Rectangle();
private Rectangle _downRect = new Rectangle();
private Point[] _updownLine = new Point[4];
private Point[] _upIcon;
private Point[] _downIcon;
private float _max; //最大值
private float _min; //最小值
private float _value; //当前值
private int _decimal; //小数
private float _increment; //增减数量
private const int BORDER_W = 2;
private readonly int BORDER_HALF = BORDER_W / 2;
public FlatUpDown()
{
InitializeComponent();
Font = new Font("宋体", 9f);
Padding = new Padding(3);
_max = 100;
_min = 0;
_value = 0;
_decimal = 0;
_increment = 1;
Txt.BackColor = Common.BACK_COLOR;
Txt.ForeColor = Common.FORE_COLOR;
CalcSize();
}
[Browsable(true), Category(""), Description("显示的文本的字体")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override Font Font
{
set
{
base.Font = value;
Txt.Font = value;
CalcSize();
Refresh();
}
get
{
return base.Font;
}
}
[Browsable(true), Category(""), Description("控件内部间距")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public new Padding Padding
{
set
{
base.Padding = value;
CalcSize();
Refresh();
}
get
{
return base.Padding;
}
}
[Browsable(true), Category(""), Description("当前值"), DefaultValue(0)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public float Value
{
set
{
_value = value;
CalcSize();
Refresh();
}
get
{
return _value;
}
}
[Browsable(true), Category(""), Description("最大值"), DefaultValue(100)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public float Maximum
{
set
{
_max = value;
if (_value > _max)
Value = _max;
}
get
{
return _max;
}
}
[Browsable(true), Category(""), Description("最小值"), DefaultValue(0)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public float Minimum
{
set
{
_min = value;
if (_value < _min)
Value = _min;
}
get
{
return _min;
}
}
[Browsable(true), Category(""), Description("小数位数"), DefaultValue(0)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public int DecimalPlaces
{
set
{
if (value < 0)
_decimal = 0;
else if (value > 10)
_decimal = 10;
else
_decimal = value;
CalcSize();
Refresh();
}
get
{
return _decimal;
}
}
[Browsable(true), Category(""), Description("每次单击按钮时增加或减少的数量"), DefaultValue(1)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public float Increment
{
set
{
_increment = value;
CalcSize();
Refresh();
}
get
{
return _increment;
}
}
protected override void CalcSize()
{
Txt.Left = BORDER_W + Padding.Left;
Txt.Width = Width - BORDER_W * 2 - Padding.Horizontal - Common.RBUTTON_W;
Txt.Top = BORDER_W + (Height - Txt.Height) / 2;
_borderRect = new Rectangle(BORDER_HALF, BORDER_HALF, Width - BORDER_W, Height - BORDER_W);
_upRect.X = Width - BORDER_W - Common.RBUTTON_W;
_upRect.Y = BORDER_W;
_upRect.Width = Common.RBUTTON_W;
_upRect.Height = (Height - BORDER_W * 3) / 2;
_upIcon = Shape.Triangle(_upRect, 5, true);
_downRect.X = _upRect.X;
_downRect.Y = _upRect.Bottom + BORDER_W;
_downRect.Width = _upRect.Width;
_downRect.Height = _upRect.Height;
_downIcon = Shape.Triangle(_downRect, 5, false);
_updownLine[0] = new Point(_upRect.X - BORDER_HALF, _upRect.Y);
_updownLine[1] = new Point(_upRect.X - BORDER_HALF, _downRect.Bottom + BORDER_HALF);
_updownLine[2] = new Point(_upRect.X, _upRect.Bottom + BORDER_HALF);
_updownLine[3] = new Point(_upRect.Right, _upRect.Bottom + BORDER_HALF);
Txt.Text = string.Format("{0:F" + _decimal + "}", _value);
}
private void FlatTextBox_Paint(object sender, PaintEventArgs e)
{
//e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
//e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
//e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
e.Graphics.Clear(Common.BACK_COLOR);
if (Inside)
{
e.Graphics.DrawRectangle(Common.BLUE_PEN, _borderRect);
e.Graphics.DrawLine(Common.BLUE_PEN, _updownLine[0], _updownLine[1]);
e.Graphics.DrawLine(Common.BLUE_PEN, _updownLine[2], _updownLine[3]);
if(_upDown)
e.Graphics.FillRectangle(Common.DOWN_BRUSH, _upRect);
else if(_downDown)
e.Graphics.FillRectangle(Common.DOWN_BRUSH, _downRect);
else if (_upOver)
e.Graphics.FillRectangle(Common.OVER_BRUSH, _upRect);
else if (_downOver)
e.Graphics.FillRectangle(Common.OVER_BRUSH, _downRect);
}
else
{
e.Graphics.DrawRectangle(Common.BORDER_PEN, _borderRect);
}
e.Graphics.FillPolygon(Common.FORE_BRUSH, _upIcon);
e.Graphics.FillPolygon(Common.FORE_BRUSH, _downIcon);
}
private void FlatTextBox_MouseDown(object sender, MouseEventArgs e)
{
if (_upOver)
{
_upDown = true;
_value += _increment;
if (_value > _max) _value = _max;
Txt.Text = string.Format("{0:F" + _decimal + "}", _value);
Refresh();
}
else if (_downOver)
{
_downDown = true;
_value -= _increment;
if (_value < _min) _value = _min;
Txt.Text = string.Format("{0:F" + _decimal + "}", _value);
Refresh();
}
}
private void FlatTextBox_MouseUp(object sender, MouseEventArgs e)
{
_upDown = false;
_downDown = false;
Refresh();
}
private void FlatTextBox_MouseMove(object sender, MouseEventArgs e)
{
bool bb = false;
bool bln = _upRect.Contains(e.Location);
if (bln != _upOver)
{
_upOver = bln;
bb = true;
}
bln = _downRect.Contains(e.Location);
if (bln != _downOver)
{
_downOver = bln;
bb = true;
}
if (bb) Refresh();
}
private void Txt_MouseEnter(object sender, EventArgs e)
{
//MouseFocus(true);
}
private void Txt_TextChanged(object sender, EventArgs e)
{
//if (Text == Txt.Text) return;
//Text = Txt.Text;
}
private void Txt_KeyDown(object sender, KeyEventArgs e)
{
}
private void Txt_KeyPress(object sender, KeyPressEventArgs e)
{
}
private void Txt_KeyUp(object sender, KeyEventArgs e)
{
if (float.TryParse(Txt.Text, out float result))
{
if (result < _min)
_value = _min;
else if (result > _max)
_value = _max;
else
_value = result;
Txt.Text = string.Format("{0:F" + _decimal + "}", _value);
}
else
{
e.Handled = true;
}
}
}
}