服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - c# Winform自定义控件-仪表盘功能

c# Winform自定义控件-仪表盘功能

2022-08-05 10:58冰封一夏 C#

这篇文章主要介绍了c#Winform自定义控件-仪表盘功能,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下

前提

入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。

GitHub:https://github.com/kwwwvagaa/NetWinformControl

码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

NuGet

Install-Package HZH_Controls

目录

https://www.cnblogs.com/bfyx/p/11364884.html

用处及效果

c# Winform自定义控件-仪表盘功能

准备工作

依然使用GDI+画的,不懂的话就百度一下吧

另外主要用到了三角函数,如果不懂,可以向初中的数学老师再问问(你也可以百度一下)

开始

添加一个类UCMeter 继承 UserControl

首先添加一个需要控制的属性

?
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
private int splitCount = 10;
     /// <summary>
     /// Gets or sets the split count.
     /// </summary>
     /// <value>The split count.</value>
     [Description("分隔刻度数量,>1"), Category("自定义")]
     public int SplitCount
     {
       get { return splitCount; }
      set
      {
        if (value < 1)
          return;
        splitCount = value;
        Refresh();
      }
    }
    private int meterDegrees = 150;
    /// <summary>
    /// Gets or sets the meter degrees.
    /// </summary>
    /// <value>The meter degrees.</value>
    [Description("表盘跨度角度,0-360"), Category("自定义")]
    public int MeterDegrees
    {
      get { return meterDegrees; }
      set
      {
        if (value > 360 || value <= 0)
          return;
        meterDegrees = value;
        Refresh();
      }
    }
    private decimal minValue = 0;
    /// <summary>
    /// Gets or sets the minimum value.
    /// </summary>
    /// <value>The minimum value.</value>
    [Description("最小值,<MaxValue"), Category("自定义")]
    public decimal MinValue
    {
      get { return minValue; }
      set
      {
        if (value >= maxValue)
          return;
        minValue = value;
        Refresh();
      }
    }
    private decimal maxValue = 100;
    /// <summary>
    /// Gets or sets the maximum value.
    /// </summary>
    /// <value>The maximum value.</value>
    [Description("最大值,>MinValue"), Category("自定义")]
    public decimal MaxValue
    {
      get { return maxValue; }
      set
      {
        if (value <= minValue)
          return;
        maxValue = value;
        Refresh();
      }
    }
    /// <summary>
    /// 获取或设置控件显示的文字的字体。
    /// </summary>
    /// <value>The font.</value>
    /// <PermissionSet>
    ///  <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
    ///  <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
    ///  <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
    ///  <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
    /// </PermissionSet>
    [Description("刻度字体"), Category("自定义")]
    public override Font Font
    {
      get
      {
        return base.Font;
      }
      set
      {
        base.Font = value;
        Refresh();
      }
    }
    private decimal m_value = 0;
    /// <summary>
    /// Gets or sets the value.
    /// </summary>
    /// <value>The value.</value>
    [Description("值,>=MinValue并且<=MaxValue"), Category("自定义")]
    public decimal Value
    {
      get { return m_value; }
      set
      {
        if (value < minValue || value > maxValue)
          return;
        m_value = value;
        Refresh();
      }
    }
    private MeterTextLocation textLocation = MeterTextLocation.None;
    /// <summary>
    /// Gets or sets the text location.
    /// </summary>
    /// <value>The text location.</value>
    [Description("值和固定文字显示位置"), Category("自定义")]
    public MeterTextLocation TextLocation
    {
      get { return textLocation; }
      set
      {
        textLocation = value;
        Refresh();
      }
    }
    private string fixedText;
    /// <summary>
    /// Gets or sets the fixed text.
    /// </summary>
    /// <value>The fixed text.</value>
    [Description("固定文字"), Category("自定义")]
    public string FixedText
    {
      get { return fixedText; }
      set
      {
        fixedText = value;
        Refresh();
      }
    }
    private Font textFont = DefaultFont;
    /// <summary>
    /// Gets or sets the text font.
    /// </summary>
    /// <value>The text font.</value>
    [Description("值和固定文字字体"), Category("自定义")]
    public Font TextFont
    {
      get { return textFont; }
      set
      {
        textFont = value;
        Refresh();
      }
    }
    private Color externalRoundColor = Color.FromArgb(255, 77, 59);
    /// <summary>
    /// Gets or sets the color of the external round.
    /// </summary>
    /// <value>The color of the external round.</value>
    [Description("外圆颜色"), Category("自定义")]
    public Color ExternalRoundColor
    {
      get { return externalRoundColor; }
      set
      {
        externalRoundColor = value;
        Refresh();
      }
    }
    private Color insideRoundColor = Color.FromArgb(255, 77, 59);
    /// <summary>
    /// Gets or sets the color of the inside round.
    /// </summary>
    /// <value>The color of the inside round.</value>
    [Description("内圆颜色"), Category("自定义")]
    public Color InsideRoundColor
    {
      get { return insideRoundColor; }
      set
      {
        insideRoundColor = value;
        Refresh();
      }
    }
    private Color boundaryLineColor = Color.FromArgb(255, 77, 59);
    /// <summary>
    /// Gets or sets the color of the boundary line.
    /// </summary>
    /// <value>The color of the boundary line.</value>
    [Description("边界线颜色"), Category("自定义")]
    public Color BoundaryLineColor
    {
      get { return boundaryLineColor; }
      set
      {
        boundaryLineColor = value;
        Refresh();
      }
    }
    private Color scaleColor = Color.FromArgb(255, 77, 59);
    /// <summary>
    /// Gets or sets the color of the scale.
    /// </summary>
    /// <value>The color of the scale.</value>
    [Description("刻度颜色"), Category("自定义")]
    public Color ScaleColor
    {
      get { return scaleColor; }
      set
      {
        scaleColor = value;
        Refresh();
      }
    }
    private Color scaleValueColor = Color.FromArgb(255, 77, 59);
    /// <summary>
    /// Gets or sets the color of the scale value.
    /// </summary>
    /// <value>The color of the scale value.</value>
    [Description("刻度值文字颜色"), Category("自定义")]
    public Color ScaleValueColor
    {
      get { return scaleValueColor; }
      set
      {
        scaleValueColor = value;
        Refresh();
      }
    }
    private Color pointerColor = Color.FromArgb(255, 77, 59);
    /// <summary>
    /// Gets or sets the color of the pointer.
    /// </summary>
    /// <value>The color of the pointer.</value>
    [Description("指针颜色"), Category("自定义")]
    public Color PointerColor
    {
      get { return pointerColor; }
      set
      {
        pointerColor = value;
        Refresh();
      }
    }
    private Color textColor = Color.FromArgb(255, 77, 59);
    /// <summary>
    /// Gets or sets the color of the text.
    /// </summary>
    /// <value>The color of the text.</value>
    [Description("值和固定文字颜色"), Category("自定义")]
    public Color TextColor
    {
      get { return textColor; }
      set
      {
        textColor = value;
        Refresh();
      }
    }
 
    Rectangle m_rectWorking;

重绘

?
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
protected override void OnPaint(PaintEventArgs e)
     {
       base.OnPaint(e);
       var g = e.Graphics;
       g.SetGDIHigh();
       //外圆
       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), 1), 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), 1), 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), 1), new PointF(intX, intY), new PointF(fltX1, fltY1));
         g.DrawLine(new Pen(new SolidBrush(boundaryLineColor), 1), new PointF(m_rectWorking.Right - (fltX1 - m_rectWorking.Left), fltY1), new PointF(m_rectWorking.Right - (intX - m_rectWorking.Left), 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 != MeterTextLocation.None)
       {
         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));
         }
       }
       //画指针
       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));
       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);
     }

还有一个显示文字位置的枚举

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/// <summary>
  /// Enum MeterTextLocation
  /// </summary>
  public enum MeterTextLocation
  {
    /// <summary>
    /// The none
    /// </summary>
    None,
    /// <summary>
    /// The top
    /// </summary>
    Top,
    /// <summary>
    /// The bottom
    /// </summary>
    Bottom
  }

代码就这么多了,看完整代码

?
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
399
400
401
402
403
404
405
// ***********************************************************************
// Assembly     : HZH_Controls
// Created     : 2019-09-03
//
// ***********************************************************************
// <copyright file="UCMeter.cs">
//   Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
// </copyright>
//
// Blog: https://www.cnblogs.com/bfyx
// GitHub:https://github.com/kwwwvagaa/NetWinformControl
// gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
//
// If you use this code, please keep this note.
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;
namespace HZH_Controls.Controls
{
  /// <summary>
  /// Class UCMeter.
  /// Implements the <see cref="System.Windows.Forms.UserControl" />
  /// </summary>
  /// <seealso cref="System.Windows.Forms.UserControl" />
  public class UCMeter : UserControl
  {
    private int splitCount = 10;
    /// <summary>
    /// Gets or sets the split count.
    /// </summary>
    /// <value>The split count.</value>
    [Description("分隔刻度数量,>1"), Category("自定义")]
    public int SplitCount
    {
      get { return splitCount; }
      set
      {
        if (value < 1)
          return;
        splitCount = value;
        Refresh();
      }
    }
    private int meterDegrees = 150;
    /// <summary>
    /// Gets or sets the meter degrees.
    /// </summary>
    /// <value>The meter degrees.</value>
    [Description("表盘跨度角度,0-360"), Category("自定义")]
    public int MeterDegrees
    {
      get { return meterDegrees; }
      set
      {
        if (value > 360 || value <= 0)
          return;
        meterDegrees = value;
        Refresh();
      }
    }
    private decimal minValue = 0;
    /// <summary>
    /// Gets or sets the minimum value.
    /// </summary>
    /// <value>The minimum value.</value>
    [Description("最小值,<MaxValue"), Category("自定义")]
    public decimal MinValue
    {
      get { return minValue; }
      set
      {
        if (value >= maxValue)
          return;
        minValue = value;
        Refresh();
      }
    }
    private decimal maxValue = 100;
    /// <summary>
    /// Gets or sets the maximum value.
    /// </summary>
    /// <value>The maximum value.</value>
    [Description("最大值,>MinValue"), Category("自定义")]
    public decimal MaxValue
    {
      get { return maxValue; }
      set
      {
        if (value <= minValue)
          return;
        maxValue = value;
        Refresh();
      }
    }
    /// <summary>
    /// 获取或设置控件显示的文字的字体。
    /// </summary>
    /// <value>The font.</value>
    /// <PermissionSet>
    ///  <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
    ///  <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
    ///  <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
    ///  <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
    /// </PermissionSet>
    [Description("刻度字体"), Category("自定义")]
    public override Font Font
    {
      get
      {
        return base.Font;
      }
      set
      {
        base.Font = value;
        Refresh();
      }
    }
    private decimal m_value = 0;
    /// <summary>
    /// Gets or sets the value.
    /// </summary>
    /// <value>The value.</value>
    [Description("值,>=MinValue并且<=MaxValue"), Category("自定义")]
    public decimal Value
    {
      get { return m_value; }
      set
      {
        if (value < minValue || value > maxValue)
          return;
        m_value = value;
        Refresh();
      }
    }
    private MeterTextLocation textLocation = MeterTextLocation.None;
    /// <summary>
    /// Gets or sets the text location.
    /// </summary>
    /// <value>The text location.</value>
    [Description("值和固定文字显示位置"), Category("自定义")]
    public MeterTextLocation TextLocation
    {
      get { return textLocation; }
      set
      {
        textLocation = value;
        Refresh();
      }
    }
    private string fixedText;
    /// <summary>
    /// Gets or sets the fixed text.
    /// </summary>
    /// <value>The fixed text.</value>
    [Description("固定文字"), Category("自定义")]
    public string FixedText
    {
      get { return fixedText; }
      set
      {
        fixedText = value;
        Refresh();
      }
    }
    private Font textFont = DefaultFont;
    /// <summary>
    /// Gets or sets the text font.
    /// </summary>
    /// <value>The text font.</value>
    [Description("值和固定文字字体"), Category("自定义")]
    public Font TextFont
    {
      get { return textFont; }
      set
      {
        textFont = value;
        Refresh();
      }
    }
    private Color externalRoundColor = Color.FromArgb(255, 77, 59);
    /// <summary>
    /// Gets or sets the color of the external round.
    /// </summary>
    /// <value>The color of the external round.</value>
    [Description("外圆颜色"), Category("自定义")]
    public Color ExternalRoundColor
    {
      get { return externalRoundColor; }
      set
      {
        externalRoundColor = value;
        Refresh();
      }
    }
    private Color insideRoundColor = Color.FromArgb(255, 77, 59);
    /// <summary>
    /// Gets or sets the color of the inside round.
    /// </summary>
    /// <value>The color of the inside round.</value>
    [Description("内圆颜色"), Category("自定义")]
    public Color InsideRoundColor
    {
      get { return insideRoundColor; }
      set
      {
        insideRoundColor = value;
        Refresh();
      }
    }
    private Color boundaryLineColor = Color.FromArgb(255, 77, 59);
    /// <summary>
    /// Gets or sets the color of the boundary line.
    /// </summary>
    /// <value>The color of the boundary line.</value>
    [Description("边界线颜色"), Category("自定义")]
    public Color BoundaryLineColor
    {
      get { return boundaryLineColor; }
      set
      {
        boundaryLineColor = value;
        Refresh();
      }
    }
    private Color scaleColor = Color.FromArgb(255, 77, 59);
    /// <summary>
    /// Gets or sets the color of the scale.
    /// </summary>
    /// <value>The color of the scale.</value>
    [Description("刻度颜色"), Category("自定义")]
    public Color ScaleColor
    {
      get { return scaleColor; }
      set
      {
        scaleColor = value;
        Refresh();
      }
    }
    private Color scaleValueColor = Color.FromArgb(255, 77, 59);
    /// <summary>
    /// Gets or sets the color of the scale value.
    /// </summary>
    /// <value>The color of the scale value.</value>
    [Description("刻度值文字颜色"), Category("自定义")]
    public Color ScaleValueColor
    {
      get { return scaleValueColor; }
      set
      {
        scaleValueColor = value;
        Refresh();
      }
    }
    private Color pointerColor = Color.FromArgb(255, 77, 59);
    /// <summary>
    /// Gets or sets the color of the pointer.
    /// </summary>
    /// <value>The color of the pointer.</value>
    [Description("指针颜色"), Category("自定义")]
    public Color PointerColor
    {
      get { return pointerColor; }
      set
      {
        pointerColor = value;
        Refresh();
      }
    }
    private Color textColor = Color.FromArgb(255, 77, 59);
    /// <summary>
    /// Gets or sets the color of the text.
    /// </summary>
    /// <value>The color of the text.</value>
    [Description("值和固定文字颜色"), Category("自定义")]
    public Color TextColor
    {
      get { return textColor; }
      set
      {
        textColor = value;
        Refresh();
      }
    }
    Rectangle m_rectWorking;
    public UCMeter()
    {
      this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
      this.SetStyle(ControlStyles.DoubleBuffer, true);
      this.SetStyle(ControlStyles.ResizeRedraw, true);
      this.SetStyle(ControlStyles.Selectable, true);
      this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
      this.SetStyle(ControlStyles.UserPaint, true);
      this.SizeChanged += UCMeter1_SizeChanged;
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
      this.Size = new Size(350, 200);
    }
    void UCMeter1_SizeChanged(object sender, EventArgs e)
    {
      m_rectWorking = new Rectangle(10, 10, this.Width - 20, this.Height - 20);
    }
    protected override void OnPaint(PaintEventArgs e)
    {
      base.OnPaint(e);
      var g = e.Graphics;
      g.SetGDIHigh();
      //外圆
      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), 1), 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), 1), 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), 1), new PointF(intX, intY), new PointF(fltX1, fltY1));
        g.DrawLine(new Pen(new SolidBrush(boundaryLineColor), 1), new PointF(m_rectWorking.Right - (fltX1 - m_rectWorking.Left), fltY1), new PointF(m_rectWorking.Right - (intX - m_rectWorking.Left), 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 != MeterTextLocation.None)
      {
        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));
        }
      }
      //画指针
      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));
      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>
  /// Enum MeterTextLocation
  /// </summary>
  public enum MeterTextLocation
  {
    /// <summary>
    /// The none
    /// </summary>
    None,
    /// <summary>
    /// The top
    /// </summary>
    Top,
    /// <summary>
    /// The bottom
    /// </summary>
    Bottom
  }
}

 

最后的话

如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧

总结

以上所述是小编给大家介绍的c# Winform自定义控件-仪表盘功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

原文链接:https://www.cnblogs.com/bfyx/p/11457234.html

延伸 · 阅读

精彩推荐
  • C#C#强制类型转换小结

    C#强制类型转换小结

    任何一门编程语言均有相关数据类型。C#也不例外,不过转换过程要注意小类型能转换成大类型,但大类型一般不能转换成小类型,下面小编给大家详解C...

    Amedeo6352022-01-12
  • C#C#多线程之线程控制详解

    C#多线程之线程控制详解

    这篇文章主要为大家详细介绍了C#多线程之线程控制的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    云梦鸿4982022-01-19
  • C#C#文件目录操作方法汇总

    C#文件目录操作方法汇总

    本文主要列举出C#文件和目录操作的一些方法,包括创建、移动、遍历目录,读写文件等方法,有需要的小伙伴可以学习一下。...

    jerrylsxu7872021-11-19
  • C#经典排序算法之冒泡排序(Bubble sort)代码

    经典排序算法之冒泡排序(Bubble sort)代码

    这篇文章主要介绍了经典排序算法之冒泡排序(Bubble sort)代码的相关资料,非常不错具有参考借鉴价值,需要的朋友可以参考下...

    kkun3632021-11-23
  • C#C#与C++与互操作实例讲解

    C#与C++与互操作实例讲解

    在本篇文章里小编给大家整理了关于C#与C++与互操作实例以及相关内容,需要的朋友们可以学习下。...

    zhaotianff4912022-08-04
  • C#C#中系统时间和UNIX时间戳互相转换

    C#中系统时间和UNIX时间戳互相转换

    本文主要介绍C#中系统时间和UNIX时间戳相互转换的方法,大家可以直接拿去用,希望有用。...

    yourber7702021-11-22
  • C#C# 调用 JavaWebservice服务遇到的问题汇总

    C# 调用 JavaWebservice服务遇到的问题汇总

    本文给大家分享的是个人在使用C#调用 JavaWebservice服务遇到的几个问题的解决方法的汇总,给有类似需求的小伙伴们参考下吧。...

    C#教程网8082021-11-08
  • C#c# WinForm 窗体之间传值的几种方式(小结)

    c# WinForm 窗体之间传值的几种方式(小结)

    这篇文章主要介绍了WinForm 窗体之间传值的几种方式(小结),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    大白快跑86352022-02-28