FaceNumeric.cs
12.9 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
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
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace FaceControl
{
[DefaultProperty("Value")]
[DefaultEvent("ValueChanged")]
public partial class FaceNumeric : ControlBase
{
private TextBox txt;
private bool[] mouseDown = new bool[2];
private bool[] buttonOver = new bool[2];
private bool borderOver = false;
private GraphicsPath borderPath = new GraphicsPath();
private RectangleF[] buttonRect = new RectangleF[2];
private RectangleF[] textRect = new RectangleF[2];
private float _min = 0;
private float _max = 100;
private float _value = 0;
private int _decimalPlaces = 2;
private const int BTN_WIDTH = 30;
private readonly string[] BTN_TEXT = new string[] { "-", "+" };
public FaceNumeric()
{
InitializeComponent();
txt = new TextBox
{
BorderStyle = BorderStyle.None,
BackColor = BackColor,
ForeColor = ForeColor,
ImeMode = ImeMode.Disable
};
txt.TextChanged += Txt_TextChanged;
txt.MouseEnter += Txt_MouseEnter;
txt.MouseLeave += Txt_MouseLeave;
txt.KeyPress += Txt_KeyPress;
Controls.Add(txt);
_decimalPlaces = 2;
Increment = 1;
ShowNumeric();
FaceNumeric_Resize(null, EventArgs.Empty);
}
#region 事件
[Browsable(true), Category("操作"), Description("控件中的值更改时发生")]
public event EventHandler ValueChanged;
#endregion
#region 属性
[Browsable(true), Category("数据"), Description("控件的最小值"), DefaultValue(0)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public float Minimum
{
get => _min;
set
{
_min = value;
if (_value < _min) _value = _min;
ShowNumeric();
CalcSize();
Refresh();
}
}
[Browsable(true), Category("数据"), Description("控件的最大值"), DefaultValue(100)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public float Maximum
{
get => _max;
set
{
_max = value;
if (_value > _max) _value = _max;
ShowNumeric();
CalcSize();
Refresh();
}
}
[Browsable(true), Category("外观"), Description("控件的当前值"), DefaultValue(0)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public float Value
{
get => _value;
set
{
if (_value == value) return;
_value = value;
if (_value < _min)
_value = _min;
else if (_value > _max)
_value = _max;
ShowNumeric();
CalcSize();
Refresh();
ValueChanged?.Invoke(this, new EventArgs());
}
}
[Browsable(true), Category("数据"), Description("指示要显示的小数位数"), DefaultValue(2)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public int DecimalPlaces
{
get => _decimalPlaces;
set
{
_decimalPlaces = value;
ShowNumeric();
}
}
[Browsable(true), Category("数据"), Description("增加或减少的数量"), DefaultValue(1)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public float Increment { set; get; }
[Browsable(true), Category("行为"), Description("控制能否更改编辑控件中的文本"), DefaultValue(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public bool ReadOnly { set => txt.ReadOnly = value; get => txt.ReadOnly; }
[Browsable(true), Category("外观"), Description("指示应该如何对齐编辑控件的文本"), DefaultValue(HorizontalAlignment.Left)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public new HorizontalAlignment TextAlign { set => txt.TextAlign = value; get => txt.TextAlign; }
[Browsable(true), Category("外观"), Description("用于显示控件中文本的字体")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override Font Font
{
get => base.Font;
set
{
base.Font = value;
FaceNumeric_Resize(null, EventArgs.Empty);
}
}
#endregion
protected override void CalcSize()
{
base.CalcSize();
CalcTextLocation();
CalcShape();
}
protected override void DrawEnabled(Graphics g)
{
base.DrawEnabled(g);
//控件边框
if (CursorInside && borderOver)
{
if (mouseDown[0])
g.FillRectangle(new SolidBrush(theme.DOWN_COLOR), buttonRect[0]); //按下
else if (buttonOver[0])
g.FillRectangle(new SolidBrush(theme.OVER_COLOR), buttonRect[0]); //经过
if (mouseDown[1])
g.FillRectangle(new SolidBrush(theme.DOWN_COLOR), buttonRect[1]); //按下
else if (buttonOver[1])
g.FillRectangle(new SolidBrush(theme.OVER_COLOR), buttonRect[1]); //经过
if (BorderWidth > 0)
g.DrawPath(borderInsidePen, borderPath);
}
else
{
g.FillPath(new SolidBrush(BackColor), borderPath);
if (BorderWidth > 0)
g.DrawPath(borderOutsidePen, borderPath);
}
g.DrawString(BTN_TEXT[0], new Font("宋体", Font.Size), new SolidBrush(ForeColor), textRect[0]);
g.DrawString(BTN_TEXT[1], new Font("宋体", Font.Size), new SolidBrush(ForeColor), textRect[1]);
}
protected override void DrawDisabled(Graphics g)
{
base.DrawDisabled(g);
//控件边框
if (BorderWidth > 0)
g.DrawPath(borderOutsidePen, borderPath);
g.DrawString(BTN_TEXT[0], new Font("宋体", Font.Size), new SolidBrush(theme.LOST_FOCUS_COLOR), textRect[0]);
g.DrawString(BTN_TEXT[1], new Font("宋体", Font.Size), new SolidBrush(theme.LOST_FOCUS_COLOR), textRect[1]);
}
private void CalcShape()
{
borderPath = new GraphicsPath();
float n;
switch (FaceShape)
{
case Model.ButtonShape.Rectangle:
borderPath.AddRectangle(borderRect);
break;
case Model.ButtonShape.RoundRectangle:
n = borderRect.Height / 8f;
borderPath.AddLine(borderRect.X + n, borderRect.Y, borderRect.Right - n, borderRect.Y);
borderPath.AddArc(borderRect.Right - n - n, borderRect.Y, n + n, n + n, 270, 90);
borderPath.AddLine(borderRect.Right, borderRect.Y + n, borderRect.Right, borderRect.Bottom - n);
borderPath.AddArc(borderRect.Right - n - n, borderRect.Bottom - n - n, n + n, n + n, 0, 90);
borderPath.AddLine(borderRect.Right - n, borderRect.Bottom, borderRect.X + n, borderRect.Bottom);
borderPath.AddArc(borderRect.X, borderRect.Bottom - n - n, n + n, n + n, 90, 90);
borderPath.AddLine(borderRect.X, borderRect.Bottom - n, borderRect.X, borderRect.Y + n);
borderPath.AddArc(borderRect.X, borderRect.Y, n + n, n + n, 180, 90);
break;
case Model.ButtonShape.Ellipse:
borderPath.AddEllipse(borderRect);
break;
case Model.ButtonShape.EllipseRectangle:
n = borderRect.Height / 2f;
borderPath.AddLine(borderRect.X + n, borderRect.Y, borderRect.Right - n, borderRect.Y);
borderPath.AddArc(borderRect.Right - n - n, borderRect.Y, n + n, n + n, 270, 180);
borderPath.AddLine(borderRect.Right - n, borderRect.Bottom, borderRect.X + n, borderRect.Bottom);
borderPath.AddArc(borderRect.X, borderRect.Y, n + n, n + n, 90, 180);
break;
case Model.ButtonShape.Circle:
if (borderRect.Width > borderRect.Height)
borderPath.AddEllipse((borderRect.Width - borderRect.Height) / 2f, borderRect.Y, borderRect.Height, borderRect.Height);
else
borderPath.AddEllipse(borderRect.X, (borderRect.Height - borderRect.Width) / 2f, borderRect.Width, borderRect.Width);
break;
}
}
private void CalcTextLocation()
{
buttonRect[0] = new RectangleF(borderRect.X, borderRect.Y, BTN_WIDTH, borderRect.Height);
buttonRect[1] = new RectangleF(borderRect.Right - BTN_WIDTH, borderRect.Y, BTN_WIDTH, borderRect.Height);
Graphics g = CreateGraphics();
SizeF sf = g.MeasureString(BTN_TEXT[0], new Font("宋体", Font.Size));
float x = buttonRect[0].X + (buttonRect[0].Width - sf.Width) / 2f;
float y = buttonRect[0].Y + (buttonRect[0].Height - sf.Height) / 2f;
textRect[0] = new RectangleF(x, y, sf.Width, sf.Height);
sf = g.MeasureString(BTN_TEXT[1], new Font("宋体", Font.Size));
x = buttonRect[1].X + (buttonRect[1].Width - sf.Width) / 2f;
y = buttonRect[1].Y + (buttonRect[1].Height - sf.Height) / 2f;
textRect[1] = new RectangleF(x, y, sf.Width, sf.Height);
}
private void ShowNumeric()
{
txt.Text = string.Format("{0:N"+ _decimalPlaces + "}", _value);
txt.SelectionStart = txt.Text.Length;
}
private void Txt_TextChanged(object sender, EventArgs e)
{
//TextChanged?.Invoke(this, new EventArgs());
}
private void Txt_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
bool rtn = float.TryParse(txt.Text, out float result);
if (rtn) _value = result;
ShowNumeric();
}
else
{
if ((e.KeyChar < 48 || e.KeyChar > 57) && //0-9
(e.KeyChar != 8) && //Backspace
(e.KeyChar != 46) && //.
(e.KeyChar != 45)) //-
{
e.Handled = true;
}
}
}
private void Txt_MouseLeave(object sender, EventArgs e)
{
CursorInside = false;
borderOver = false;
Refresh();
}
private void Txt_MouseEnter(object sender, EventArgs e)
{
CursorInside = true;
borderOver = true;
Refresh();
}
private void FaceNumeric_MouseDown(object sender, MouseEventArgs e)
{
if (!Enabled) return;
if (e.Button == MouseButtons.Left)
{
if (buttonOver[0])
{
mouseDown[0] = true;
Value -= Increment;
}
else if (buttonOver[1])
{
mouseDown[1] = true;
Value += Increment;
}
Refresh();
}
}
private void FaceNumeric_MouseMove(object sender, MouseEventArgs e)
{
buttonOver[0] = buttonOver[1] = false;
if (buttonRect[0].Contains(e.Location))
buttonOver[0] = true;
else if (buttonRect[1].Contains(e.Location))
buttonOver[1] = true;
borderOver = borderPath.IsVisible(e.Location);
Refresh();
}
private void FaceNumeric_MouseUp(object sender, MouseEventArgs e)
{
if (!Enabled) return;
if (e.Button == MouseButtons.Left)
{
mouseDown[0] = mouseDown[1] = false;
Refresh();
}
}
private void FaceNumeric_Resize(object sender, EventArgs e)
{
txt.Left = BorderWidth + BTN_WIDTH + 3;
txt.Width = Width - (BorderWidth + BTN_WIDTH + 3) * 2;
txt.Top = BorderWidth + (Height - BorderWidth - BorderWidth - txt.Height) / 2;
}
}
}