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

 更新时间:2020年6月25日 10:36  点击:1758

前提

入行已经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

用处及效果

准备工作

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

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

开始

添加一个类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;

重绘

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
   }

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

// ***********************************************************************
// 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自定义控件-仪表盘功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对猪先飞网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

[!--infotagslink--]

相关文章

  • C#实现简单的登录界面

    我们在使用C#做项目的时候,基本上都需要制作登录界面,那么今天我们就来一步步看看,如果简单的实现登录界面呢,本文给出2个例子,由简入难,希望大家能够喜欢。...2020-06-25
  • 浅谈C# 字段和属性

    这篇文章主要介绍了C# 字段和属性的的相关资料,文中示例代码非常详细,供大家参考和学习,感兴趣的朋友可以了解下...2020-11-03
  • C#中截取字符串的的基本方法详解

    这篇文章主要介绍了C#中截取字符串的的基本方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-03
  • C#实现简单的Http请求实例

    这篇文章主要介绍了C#实现简单的Http请求的方法,以实例形式较为详细的分析了C#实现Http请求的具体方法,需要的朋友可以参考下...2020-06-25
  • C#连接SQL数据库和查询数据功能的操作技巧

    本文给大家分享C#连接SQL数据库和查询数据功能的操作技巧,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友参考下吧...2021-05-17
  • C#中new的几种用法详解

    本文主要介绍了C#中new的几种用法,具有很好的参考价值,下面跟着小编一起来看下吧...2020-06-25
  • 使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序)

    这篇文章主要介绍了使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • C#开发Windows窗体应用程序的简单操作步骤

    这篇文章主要介绍了C#开发Windows窗体应用程序的简单操作步骤,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-12
  • C#从数据库读取图片并保存的两种方法

    这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
  • C#和JavaScript实现交互的方法

    最近做一个小项目不可避免的需要前端脚本与后台进行交互。由于是在asp.net中实现,故问题演化成asp.net中jiavascript与后台c#如何进行交互。...2020-06-25
  • C++调用C#的DLL程序实现方法

    本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
  • 轻松学习C#的基础入门

    轻松学习C#的基础入门,了解C#最基本的知识点,C#是一种简洁的,类型安全的一种完全面向对象的开发语言,是Microsoft专门基于.NET Framework平台开发的而量身定做的高级程序设计语言,需要的朋友可以参考下...2020-06-25
  • C#变量命名规则小结

    本文主要介绍了C#变量命名规则小结,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-09
  • C#绘制曲线图的方法

    这篇文章主要介绍了C#绘制曲线图的方法,以完整实例形式较为详细的分析了C#进行曲线绘制的具体步骤与相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • C# 中如何取绝对值函数

    本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
  • c#自带缓存使用方法 c#移除清理缓存

    这篇文章主要介绍了c#自带缓存使用方法,包括获取数据缓存、设置数据缓存、移除指定数据缓存等方法,需要的朋友可以参考下...2020-06-25
  • c#中(&&,||)与(&,|)的区别详解

    这篇文章主要介绍了c#中(&&,||)与(&,|)的区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
  • 经典实例讲解C#递归算法

    这篇文章主要用实例讲解C#递归算法的概念以及用法,文中代码非常详细,帮助大家更好的参考和学习,感兴趣的朋友可以了解下...2020-06-25
  • C#学习笔记- 随机函数Random()的用法详解

    下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • C#中list用法实例

    这篇文章主要介绍了C#中list用法,结合实例形式分析了C#中list排序、运算、转换等常见操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25