tx
2025-04-09 fa7510e1ed63df0366787fa4ed1b3db6426d2b46
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
 
namespace WinCustControls
{
    public partial class UInstrumentControl : UserControl
    {
        public UInstrumentControl()
        {
            InitializeComponent();
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);//忽略窗口消息,减少闪烁
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);//绘制到缓冲区,减少闪烁
            SetStyle(ControlStyles.UserPaint, true);//控件由其自身而不是操作系统绘制
            SetStyle(ControlStyles.ResizeRedraw, true);//控件调整其大小时重绘
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);//支持透明背景
            this.SizeChanged += UInstrumentControl_SizeChanged;
            this.Size = new Size(300, 200);
        }
        Rectangle m_rectWorking;//绘制工作区
        private void UInstrumentControl_SizeChanged(object sender, EventArgs e)
        {
            m_rectWorking = new Rectangle(10, 10, Width - 20, Height - 20);
        }
 
        private int splitCount = 10;
        /// <summary>
        /// 刻度数量
        /// </summary>
        /// <value></value>
        [Description("分隔刻度数量,数量必须 >1"), Category("自定义")]
        public int SplitCount
        {
            get { return splitCount; }
            set
            {
                if (value < 1)
                    return;
                splitCount = value;
                Invalidate();
            }
        }
 
        private int meterDegrees = 180;
        /// <summary>
        /// 跨度角度
        /// </summary>
        /// <value></value>
        [Description("表盘内跨度角度,0-360"), Category("自定义")]
        public int MeterDegrees
        {
            get { return meterDegrees; }
            set
            {
                if (value > 360 || value <= 0)
                    return;
                meterDegrees = value;
                Invalidate();
            }
        }
 
        private decimal minValue = 0;
        /// <summary>
        /// 仪表的最小值
        /// </summary>
        [Description("最小值,必须比最大值小"), Category("自定义")]
        public decimal MinValue
        {
            get { return minValue; }
            set
            {
                if (value >= maxValue)
                    return;
                minValue = value;
                Invalidate();
            }
        }
 
        private decimal maxValue = 100;
        /// <summary>
        ///最大值
        /// </summary>
        [Description("仪表的最大值,必须比最小值大"), Category("自定义")]
        public decimal MaxValue
        {
            get { return maxValue; }
            set
            {
                if (value <= minValue)
                    return;
                maxValue = value;
                Invalidate();
            }
        }
 
        [Description("刻度值的字体"), Category("自定义")]
        public override Font Font
        {
            get
            {
                return base.Font;
            }
            set
            {
                base.Font = value;
                Invalidate();
            }
        }
 
        private decimal m_value = 0;
        /// <summary>
        /// 仪表值
        /// </summary>
        [Description("仪表实际值,最大值与最小值之间的值"), Category("自定义")]
        public decimal Value
        {
            get { return m_value; }
            set
            {
                if (value < minValue || value > maxValue)
                    return;
                m_value = value;
                Invalidate();
            }
        }
 
        private TextLocation textLocation = TextLocation.None;
        /// <summary>
        /// 文字位置
        /// </summary>
        [Description("值和固定文字显示位置"), Category("自定义")]
        public TextLocation TextLocation
        {
            get { return textLocation; }
            set
            {
                textLocation = value;
                Invalidate();
            }
        }
 
        private string fixedText;
        /// <summary>
        ///固定文字
        /// </summary>
        [Description("固定文字"), Category("自定义")]
        public string FixedText
        {
            get { return fixedText; }
            set
            {
                fixedText = value;
                Invalidate();
            }
        }
 
        private Font textFont = DefaultFont;
        /// <summary>
        /// 固定文字字体
        /// </summary>
        [Description("值和固定文字字体"), Category("自定义")]
        public Font TextFont
        {
            get { return textFont; }
            set
            {
                textFont = value;
                Invalidate();
            }
        }
 
        private Color textColor = Color.Red;
        /// <summary>
        /// 值和固定文字颜色
        /// </summary>
        [Description("值和固定文字颜色"), Category("自定义")]
        public Color TextColor
        {
            get { return textColor; }
            set
            {
                textColor = value;
                Invalidate();
            }
        }
 
        private Color externalRoundColor = Color.Red;
        /// <summary>
        /// 外边圆线的颜色
        /// </summary>
        [Description("外圆线颜色"), Category("自定义")]
        public Color ExternalRoundColor
        {
            get { return externalRoundColor; }
            set
            {
                externalRoundColor = value;
                Invalidate();
            }
        }
 
        private Color insideRoundColor = Color.Gray;
        /// <summary>
        ///内圆线颜色
        /// </summary>
        /// <value></value>
        [Description("内圆线颜色"), Category("自定义")]
        public Color InsideRoundColor
        {
            get { return insideRoundColor; }
            set
            {
                insideRoundColor = value;
                Invalidate();
            }
        }
 
        private Color boundaryLineColor = Color.Red;
        /// <summary>
        /// 边界线颜色
        /// </summary>
        [Description("边界线颜色"), Category("自定义")]
        public Color BoundaryLineColor
        {
            get { return boundaryLineColor; }
            set
            {
                boundaryLineColor = value;
                Invalidate();
            }
        }
 
        private Color scaleColor = Color.Gray;
        /// <summary>
        /// 刻度线颜色
        /// </summary>
        [Description("刻度线颜色"), Category("自定义")]
        public Color ScaleColor
        {
            get { return scaleColor; }
            set
            {
                scaleColor = value;
                Invalidate();
            }
        }
 
        private Color scaleValueColor = Color.Red;
        /// <summary>
        /// 刻度值文字颜色
        /// </summary>
        [Description("刻度值文字颜色"), Category("自定义")]
        public Color ScaleValueColor
        {
            get { return scaleValueColor; }
            set
            {
                scaleValueColor = value;
                Invalidate();
            }
        }
 
        private Color pointerColor = Color.FromArgb(255, 77, 59);
        /// <summary>
        /// 指针颜色
        /// </summary>
        [Description("指针颜色"), Category("自定义")]
        public Color PointerColor
        {
            get { return pointerColor; }
            set
            {
                pointerColor = value;
                Invalidate();
            }
        }
 
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            g.SmoothingMode = SmoothingMode.HighQuality;
            //外圆 -90-75+360=195
            float fltStartAngle = -90 - (meterDegrees) / 2 + 360;//开始角度
                                                                 //绘制外圆弧的矩形区域
            var r1 = new Rectangle(m_rectWorking.Location, new Size(m_rectWorking.Width, m_rectWorking.Width));
            //画外圆弧
            g.DrawArc(new Pen(new SolidBrush(externalRoundColor), 3), r1, fltStartAngle, meterDegrees);
 
            //内圆矩形区域
            var r2 = new Rectangle(m_rectWorking.Left + (m_rectWorking.Width - m_rectWorking.Width / 4) / 2, m_rectWorking.Top + (m_rectWorking.Width - m_rectWorking.Width / 4) / 2, m_rectWorking.Width / 4, m_rectWorking.Width / 4);
            //画内圆弧
            g.DrawArc(new Pen(new SolidBrush(insideRoundColor), 2), r2, fltStartAngle, meterDegrees);
            //边界线
            if (meterDegrees != 360)
            {
                float fltAngle = fltStartAngle - 180;
                //左上的点坐标
                float intY = (float)(m_rectWorking.Top + m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - m_rectWorking.Width / 8) * Math.Sin(Math.PI * (fltAngle / 180.00F))));
                float intX = (float)(m_rectWorking.Left + (m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - m_rectWorking.Width / 8) * Math.Cos(Math.PI * (fltAngle / 180.00F)))));
                //左下的点坐标
                float fltY1 = (float)(m_rectWorking.Top + m_rectWorking.Width / 2 - (m_rectWorking.Width / 8 * Math.Sin(Math.PI * (fltAngle / 180.00F))));
                float fltX1 = (float)(m_rectWorking.Left + (m_rectWorking.Width / 2 - (m_rectWorking.Width / 8 * Math.Cos(Math.PI * (fltAngle / 180.00F)))));
                //画左边界线
                g.DrawLine(new Pen(new SolidBrush(boundaryLineColor), 2), new PointF(intX, intY), new PointF(fltX1, fltY1));
                //右边两点的x坐标
                float fltx2 = m_rectWorking.Right - (fltX1 - m_rectWorking.Left);
                float intX2 = m_rectWorking.Right - (intX - m_rectWorking.Left);
                //画右边界线
                g.DrawLine(new Pen(new SolidBrush(boundaryLineColor), 2), new PointF(fltx2, fltY1), new PointF(intX2, intY));
            }
            //分割份数
            int _splitCount = splitCount * 2;
            //每份的角度
            float fltSplitValue = (float)meterDegrees / (float)_splitCount;
            for (int i = 0; i <= _splitCount; i++)
            {
                //刻度的起始角度 
                float fltAngle = (fltStartAngle + fltSplitValue * i - 180) % 360;
                //刻度线的起点坐标
                float fltY1 = (float)(m_rectWorking.Top + m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2) * Math.Sin(Math.PI * (fltAngle / 180.00F))));
                float fltX1 = (float)(m_rectWorking.Left + (m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2) * Math.Cos(Math.PI * (fltAngle / 180.00F)))));
                //结束点坐标
                float fltY2 = 0;
                float fltX2 = 0;
                if (i % 2 == 0)//长刻度
                {
                    fltY2 = (float)(m_rectWorking.Top + m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - 10) * Math.Sin(Math.PI * (fltAngle / 180.00F))));
                    fltX2 = (float)(m_rectWorking.Left + (m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - 10) * Math.Cos(Math.PI * (fltAngle / 180.00F)))));
                    if (!(meterDegrees == 360 && i == _splitCount))
                    {
                        //长刻度线的文本
                        decimal decValue = minValue + (maxValue - minValue) / _splitCount * i;
                        var txtSize = g.MeasureString(decValue.ToString("0.##"), this.Font);
                        //刻度值文本左上角坐标
                        float fltFY1 = (float)(m_rectWorking.Top - txtSize.Height / 2 + m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - 20) * Math.Sin(Math.PI * (fltAngle / 180.00F))));
                        float fltFX1 = (float)(m_rectWorking.Left - txtSize.Width / 2 + (m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - 20) * Math.Cos(Math.PI * (fltAngle / 180.00F)))));
                        //画刻度文本
                        g.DrawString(decValue.ToString("0.##"), Font, new SolidBrush(scaleValueColor), fltFX1, fltFY1);
                    }
                }
                else//短刻度
                {
                    fltY2 = (float)(m_rectWorking.Top + m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - 5) * Math.Sin(Math.PI * (fltAngle / 180.00F))));
                    fltX2 = (float)(m_rectWorking.Left + (m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - 5) * Math.Cos(Math.PI * (fltAngle / 180.00F)))));
                }
                //画刻度线
                g.DrawLine(new Pen(new SolidBrush(scaleColor), i % 2 == 0 ? 2 : 1), new PointF(fltX1, fltY1), new PointF(fltX2, fltY2));
            }
            //值文字和固定文字
            if (textLocation == TextLocation.Top)
            {
                //值文本格式化
                string str = m_value.ToString("0.##");
                var txtSize = g.MeasureString(str, textFont);
                //值文本的左上角坐标
                float fltY = m_rectWorking.Top + m_rectWorking.Width / 4 - txtSize.Height / 2;
                float fltX = m_rectWorking.Left + m_rectWorking.Width / 2 - txtSize.Width / 2;
                //画当前值
                g.DrawString(str, textFont, new SolidBrush(textColor), new PointF(fltX, fltY));
                //如果固定文本有设置
                if (!string.IsNullOrEmpty(fixedText))
                {
                    //固定文本
                    str = fixedText;
                    txtSize = g.MeasureString(str, textFont);
                    //固定文本的左上角坐标
                    fltY = m_rectWorking.Top + m_rectWorking.Width / 4 + txtSize.Height / 2;
                    fltX = m_rectWorking.Left + m_rectWorking.Width / 2 - txtSize.Width / 2;
                    //画固定文本
                    g.DrawString(str, textFont, new SolidBrush(textColor), new PointF(fltX, fltY));
                }
            }
            //画指针中心外圆  半径10
            g.FillEllipse(new SolidBrush(Color.FromArgb(100, pointerColor.R, pointerColor.G, pointerColor.B)), new Rectangle(m_rectWorking.Left + m_rectWorking.Width / 2 - 10, m_rectWorking.Top + m_rectWorking.Width / 2 - 10, 20, 20));
            //画指针中心内圆 半径5
            g.FillEllipse(Brushes.Red, new Rectangle(m_rectWorking.Left + m_rectWorking.Width / 2 - 5, m_rectWorking.Top + m_rectWorking.Width / 2 - 5, 10, 10));
            //值的角色
            float fltValueAngle = (fltStartAngle + ((float)(m_value - minValue) / (float)(maxValue - minValue)) * (float)meterDegrees - 180) % 360;
            //值对应指向点坐标
            float intValueY1 = (float)(m_rectWorking.Top + m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - 30) * Math.Sin(Math.PI * (fltValueAngle / 180.00F))));
            float intValueX1 = (float)(m_rectWorking.Left + (m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - 30) * Math.Cos(Math.PI * (fltValueAngle / 180.00F)))));
            //画指针线
            g.DrawLine(new Pen(new SolidBrush(pointerColor), 3), intValueX1, intValueY1, m_rectWorking.Left + m_rectWorking.Width / 2, m_rectWorking.Top + m_rectWorking.Width / 2);
        }
    }
 
    /// <summary>
    /// 值和固定文本是否显示
    /// </summary>
    public enum TextLocation
    {
        None,
        Top
    }
}