SurTrack.cs
11.4 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
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.Face
{
/// <summary>
/// 滑轨控件
/// </summary>
[DefaultProperty("Value")]
[DefaultEvent("ValueChanged")]
public partial class SurTrack : BaseCtl
{
private GraphicsPath _trackPath; //轨道路径
private GraphicsPath _trackCurrPath;
private RectangleF _adjustRect; //调节器
private GraphicsPath _adjustPath;
private RectangleF _textRect;
private bool _adjustDown;
private int _min = 0;
private int _max = 100;
private int _value = 0;
private float _len;
private float _step;
private int _radius; //半径
private int _diameter = 10; //直径
private Appearance _appearance = Appearance.LeftRight;
private const int SPACE = 6;
/// <summary>
/// 滑轨控件
/// </summary>
public SurTrack()
{
InitializeComponent();
}
//==========事件==========
/// <summary>
/// 当前值改变时发生的事件
/// </summary>
[Browsable(true), Category(""), Description("当前值改变时发生的事件")]
public event MyEvent.Changed ValueChanged;
//==========属性==========
/// <summary>
/// 控件的外观
/// </summary>
[Browsable(true), Category(""), Description("控件的外观"), DefaultValue(Appearance.LeftRight)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public Appearance Appearance
{
set
{
_appearance = value;
CalcSize();
Refresh();
}
get
{
return _appearance;
}
}
/// <summary>
/// 最小值
/// </summary>
[Browsable(true), Category(""), Description("最小值"), DefaultValue(0)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public int Min
{
set
{
_min = value;
if (_value < _min) _value = _min;
CalcSize();
Refresh();
}
get
{
return _min;
}
}
/// <summary>
/// 最大值
/// </summary>
[Browsable(true), Category(""), Description("最大值"), DefaultValue(100)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public int Max
{
set
{
_max = value;
if (_value > _max) _value = _max;
CalcSize();
Refresh();
}
get
{
return _max;
}
}
/// <summary>
/// 当前值
/// </summary>
[Browsable(true), Category(""), Description("当前值"), DefaultValue(0)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public int Value
{
set
{
_value = value;
if (_value < _min)
_value = _min;
else if (_value > _max)
_value = _max;
CalcSize();
Refresh();
ValueChanged?.Invoke(this);
}
get
{
return _value;
}
}
/// <summary>
/// 轨道宽度
/// </summary>
[Browsable(true), Category(""), Description("轨道宽度"), DefaultValue(8)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public int TrackWidth
{
set
{
if (value > 0)
_diameter = value;
CalcSize();
Refresh();
}
get
{
return _diameter;
}
}
//==========私有==========
/// <summary>
/// 改变大小
/// </summary>
protected override void CalcSize()
{
base.CalcSize();
_radius = _diameter / 2;
RectangleF rect1, rect2;
PointF pt;
if (_appearance == Appearance.UpDown)
{
rect1 = new RectangleF(Width / 2f - _radius, SPACE, _diameter, _diameter); //上半圆
rect2 = new RectangleF(Width / 2f - _radius, Height - SPACE - _diameter, _diameter, _diameter); //下半圆
_len = rect2.Y - rect1.Y;
_step = _len / (_max - _min);
//轨道
_trackPath = new GraphicsPath();
_trackPath.AddArc(rect1, 180, 180);
_trackPath.AddLine(rect1.Right, rect1.Y + _radius, rect2.Right, rect2.Y + _radius);
_trackPath.AddArc(rect2, 0, 180);
_trackPath.AddLine(rect2.X, rect2.Y + _radius, rect1.X, rect1.Y + _radius);
//轨道当前部分
pt = new PointF(rect1.X + _radius, rect1.Y + _radius + (_value - _min) * _step);
_trackCurrPath = new GraphicsPath();
_trackCurrPath.AddArc(rect1, 180, 180);
_trackCurrPath.AddLine(rect1.Right, rect1.Y + _radius, pt.X + _radius, pt.Y);
_trackCurrPath.AddLine(pt.X + _radius, pt.Y, pt.X - _radius, pt.Y);
_trackCurrPath.AddLine(pt.X - _radius, pt.Y, rect1.X, rect1.Y + _radius);
//调节器
int r = 8;
_adjustRect = new RectangleF(pt.X - _radius - r, pt.Y - r, _diameter + r + r, r + r);
rect1 = new RectangleF(pt.X - _radius - r, pt.Y - r, r * 2, r * 2);
rect2 = new RectangleF(pt.X + _radius - r, pt.Y - r, r * 2, r * 2);
_adjustPath = new GraphicsPath();
_adjustPath.AddArc(rect1, 90, 180);
_adjustPath.AddLine(rect1.X + r, rect1.Y, rect2.X + r, rect2.Y);
_adjustPath.AddArc(rect2, -90, 180);
_adjustPath.AddLine(rect2.X + r, rect2.Bottom, rect1.X + r, rect1.Bottom);
//数值显示
SizeF sf = CreateGraphics().MeasureString(_value.ToString(), Common.FONT_SONG9);
_textRect = new RectangleF(_adjustRect.X + (_adjustRect.Width - sf.Width) / 2f + 1, _adjustRect.Y + (_adjustRect.Height - sf.Height) / 2f + 1, sf.Width, sf.Height);
}
else if (_appearance == Appearance.LeftRight)
{
rect1 = new RectangleF(SPACE, Height / 2f - _radius, _diameter, _diameter); //左半圆
rect2 = new RectangleF(Width - SPACE - _diameter, Height / 2f - _radius, _diameter, _diameter); //右半圆
_len = rect2.X - rect1.X;
_step = _len / (_max - _min);
//轨道
_trackPath = new GraphicsPath();
_trackPath.AddArc(rect1, 90, 180);
_trackPath.AddLine(rect1.X + _radius, rect1.Y, rect2.X + _radius, rect2.Y);
_trackPath.AddArc(rect2, -90, 180);
_trackPath.AddLine(rect2.X + _radius, rect2.Bottom, rect1.X + _radius, rect1.Bottom);
//轨道当前部分
pt = new PointF(rect1.X + _radius + (_value - _min) * _step, rect1.Y + _radius);
_trackCurrPath = new GraphicsPath();
_trackCurrPath.AddArc(rect1, 90, 180);
_trackCurrPath.AddLine(rect1.X + _radius, rect1.Y, pt.X, pt.Y - _radius);
_trackCurrPath.AddLine(pt.X, pt.Y - _radius, pt.X, pt.Y + _radius);
_trackCurrPath.AddLine(pt.X, pt.Y + _radius, rect1.X + _radius, rect1.Bottom);
//调节器
int r = 8;
_adjustRect = new RectangleF(pt.X - r, pt.Y - _radius - r, r + r, _diameter + r + r);
rect1 = new RectangleF(pt.X - r, pt.Y - _radius - r, r * 2, r * 2);
rect2 = new RectangleF(pt.X - r, pt.Y + _radius - r, r * 2, r * 2);
_adjustPath = new GraphicsPath();
_adjustPath.AddArc(rect1, 180, 180);
_adjustPath.AddLine(rect1.Right, rect1.Y + r, rect2.Right, rect2.Y + r);
_adjustPath.AddArc(rect2, 0, 180);
_adjustPath.AddLine(rect2.X, rect2.Y + r, rect1.X, rect1.Y + r);
}
}
private void FlatTrack_Paint(object sender, PaintEventArgs e)
{
BufferedGraphicsContext current = BufferedGraphicsManager.Current;
BufferedGraphics bg = current.Allocate(e.Graphics, e.ClipRectangle);
Graphics g = bg.Graphics;
g.SmoothingMode = SmoothingMode.HighQuality;
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.Clear(Common.BACK_COLOR); //背景
if (Inside && Enabled)
{
if (BorderWidth > 0)
g.DrawRectangle(BorderInsidePen, BorderRect);
}
else
{
if (BorderWidth > 0)
g.DrawRectangle(BorderOutsidePen, BorderRect);
}
if (Enabled)
{
g.DrawPath(Common.FORE_PEN, _trackPath);
g.FillPath(Common.BLUE_BRUSH, _trackCurrPath);
if (_adjustDown)
g.FillPath(Common.BLUE_BRUSH, _adjustPath);
else
g.FillPath(Common.FORE_BRUSH, _adjustPath);
}
else
{
g.DrawPath(Common.BORDER_PEN, _trackPath);
g.FillPath(Common.BORDER_BRUSH, _adjustPath);
}
if (_appearance == Appearance.UpDown)
g.DrawString(_value.ToString(), Common.FONT_SONG9, Common.BACK_BRUSH, _textRect);
bg.Render();
bg.Dispose();
}
private void FlatTrack_MouseDown(object sender, MouseEventArgs e)
{
if (!Enabled) return;
if (e.Button == MouseButtons.Left)
{
bool rtn = _adjustRect.Contains(e.Location);
if (rtn != _adjustDown)
{
_adjustDown = rtn;
Refresh();
}
}
}
private void FlatTrack_MouseMove(object sender, MouseEventArgs e)
{
if (!Enabled) return;
if (_adjustDown)
{
int tt = 0;
if (_appearance == Appearance.UpDown)
tt = e.Y;
else if (_appearance == Appearance.LeftRight)
tt = e.X;
float val = (tt - SPACE - _radius) / _step + _min;
int n = (int)val;
if (n == _value) return;
Value = n;
}
}
private void FlatTrack_MouseUp(object sender, MouseEventArgs e)
{
if (!Enabled) return;
if (_adjustDown)
{
_adjustDown = false;
Refresh();
}
}
}
}