1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Windows.Forms;
6 using System.Drawing;
7 using System.Drawing.Drawing2D;
8 using System.ComponentModel;
9
10 namespace HZH_Controls.Controls
11 {
12 public class UCTrackBar : Control
13 {
14 [Description("值改变事件"), Category("自定义")]
15 public event EventHandler ValueChanged;
16
17 private int dcimalDigits = 0;
18
19 [Description("值小数精确位数"), Category("自定义")]
20 public int DcimalDigits
21 {
22 get { return dcimalDigits; }
23 set { dcimalDigits = value; }
24 }
25
26 private float lineWidth = 10;
27
28 [Description("线宽度"), Category("自定义")]
29 public float LineWidth
30 {
31 get { return lineWidth; }
32 set { lineWidth = value; }
33 }
34
35 private float minValue = 0;
36
37 [Description("最小值"), Category("自定义")]
38 public float MinValue
39 {
40 get { return minValue; }
41 set
42 {
43 if (minValue > m_value)
44 return;
45 minValue = value;
46 this.Refresh();
47 }
48 }
49
50 private float maxValue = 100;
51
52 [Description("最大值"), Category("自定义")]
53 public float MaxValue
54 {
55 get { return maxValue; }
56 set
57 {
58 if (value < m_value)
59 return;
60 maxValue = value;
61 this.Refresh();
62 }
63 }
64
65 private float m_value = 0;
66
67 [Description("值"), Category("自定义")]
68 public float Value
69 {
70 get { return this.m_value; }
71 set
72 {
73 if (value > maxValue || value < minValue)
74 return;
75 var v = (float)Math.Round((double)value, dcimalDigits);
76 if (value == v)
77 return;
78 this.m_value = v;
79 this.Refresh();
80 if (ValueChanged != null)
81 {
82 ValueChanged(this, null);
83 }
84 }
85 }
86
87 private Color m_lineColor = Color.FromArgb(255, 77, 59);
88
89 [Description("线颜色"), Category("自定义")]
90 public Color LineColor
91 {
92 get { return m_lineColor; }
93 set
94 {
95 m_lineColor = value;
96 this.Refresh();
97 }
98 }
99 RectangleF m_lineRectangle;
100 RectangleF m_trackRectangle;
101
102 public UCTrackBar()
103 {
104 this.Size = new Size(250, 30);
105 this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
106 this.SetStyle(ControlStyles.DoubleBuffer, true);
107 this.SetStyle(ControlStyles.ResizeRedraw, true);
108 this.SetStyle(ControlStyles.Selectable, true);
109 this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
110 this.SetStyle(ControlStyles.UserPaint, true);
111 this.MouseDown += UCTrackBar_MouseDown;
112 this.MouseMove += UCTrackBar_MouseMove;
113 this.MouseUp += UCTrackBar_MouseUp;
114 }
115
116 bool blnDown = false;
117 void UCTrackBar_MouseDown(object sender, MouseEventArgs e)
118 {
119 if (m_lineRectangle.Contains(e.Location) || m_trackRectangle.Contains(e.Location))
120 {
121 blnDown = true;
122 Value = ((float)e.Location.X / (float)this.Width) * (maxValue - minValue);
123 }
124 }
125 void UCTrackBar_MouseMove(object sender, MouseEventArgs e)
126 {
127 if (blnDown)
128 {
129 Value = ((float)e.Location.X / (float)this.Width) * (maxValue - minValue);
130 }
131 }
132 void UCTrackBar_MouseUp(object sender, MouseEventArgs e)
133 {
134 blnDown = false;
135 }
136
137
138 protected override void OnPaint(PaintEventArgs e)
139 {
140 base.OnPaint(e);
141 Graphics g = e.Graphics;
142 g.SetGDIHigh();
143 m_lineRectangle = new RectangleF(lineWidth, (this.Size.Height - lineWidth) / 2, this.Size.Width - lineWidth * 2, lineWidth);
144 GraphicsPath pathLine = ControlHelper.CreateRoundedRectanglePath(m_lineRectangle, 5);
145 g.FillPath(new SolidBrush(m_lineColor), pathLine);
146 m_trackRectangle = new RectangleF(m_lineRectangle.Left - lineWidth + (((float)m_value / (float)(maxValue - minValue)) * (this.Size.Width - lineWidth * 2)), (this.Size.Height - lineWidth * 2) / 2, lineWidth * 2, lineWidth * 2);
147 g.FillEllipse(new SolidBrush(m_lineColor), m_trackRectangle);
148 g.FillEllipse(Brushes.White, new RectangleF(m_trackRectangle.X + m_trackRectangle.Width / 4, m_trackRectangle.Y + m_trackRectangle.Height / 4, m_trackRectangle.Width / 2, m_trackRectangle.Height / 2));
149 }
150 }
151 }