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

用处及效果

准备工作

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

(0)

相关推荐

  • C#实现用户自定义控件中嵌入自己的图标

    本文实例讲述了C#实现用户自定义控件中嵌入自己的图标.分享给大家供大家参考,具体如下: 下面给出一下具体的步骤. 1. 新建一个用户控件 2. 向资源文件是添加一张图片,图片格式可以是bm,ico 大小最好是 16 * 16 啦! 3. 选中用户控件图标,单击"右键>属性"把 "生成操作 的属性值改为:嵌入的资源(Action Resource) 4. 第四步: [ToolboxBitmap(typeof(CutPitureNew_WPF), "CutPitu

  • C#自定义控件VS用户控件

    C#中自定义控件VS用户控件大比拼 1 自定义控件与用户控件区别 WinForm中, 用户控件(User Control):继承自 UserControl,主要用于开发 Container 控件,Container控件可以添加其他Controls控件 自定义控件(Custom Control):继承自 Control,主要用于开发windows控件的最基本的类,比如 Text,Button 控件 2 要开发自己的控件的几种方法[1] 复合控件(Composite Controls):将现有的各种

  • C#自定义控件添加右键菜单的方法

    C#自定义控件添加右键菜单非常简单,主要用到控件,像control定义右键菜单,用items.add()叠加右键菜单内容,用click事件处理函数. 1.control是要定义右键菜单的控件. private void control_MouseDown(object sender, MouseEventArgs e) {    if (e.Button == MouseButtons.Right)    {        ContextMenu menu = new rightClickMen

  • C#自定义控件实现TextBox禁止粘贴的方法

    本文实例讲述了C#自定义控件实现TextBox禁止粘贴的方法.分享给大家供大家参考,具体如下: 开发环境:Visual Studio .net 2005 + Windows XP sp2 professional 新建->项目->Windows控件库: 新建一个类,继承自TextBox类,具体源代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawi

  • C#设计模式之Template模板方法模式实现ASP.NET自定义控件 密码强度检测功能

    本文实例讲述了C#设计模式之Template模板方法模式实现ASP.NET自定义控件 密码强度检测功能.分享给大家供大家参考,具体如下: 一.理论定义 模板方法模式 预先定义实现了一些基本属性和方法,需要重新计算的部分,通过子类去重写 或  增加新方法来实现. 二.应用举例 需求描述: ASP.NET自定义控件有很多通用的属性和事件, 通过继承System.Web.UI.WebControls.WebControl类,可以实现自定义控件. WebControl拥有控件基本的方法和事件,让我们定义

  • 解析C#自定义控件的制作与使用实例的详解

    上篇:控件制作本例是制作一个简单的自定义控件,然后用一个简单的测试程序,对于初学者来说,本例子比较简单,只能起到抛石引玉的效果.我也是在学习当中,今后会将自己所学的逐步写出来和大家交流共享.第一步:新建一个控件库项目:myControl 第二步:从工具箱里面拖动1个PictureBox.1个Button.6个Lable控件到用户界面上,布局如下: 如上图,设置pictureBox的Name为picBox,背景为白色,Button的Name为btnOpen,另外靠左的三个Lable的Text属性分

  • 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 用处及效果 准备工作

  • Winform自定义控件在界面拖动、滚动鼠标时闪烁的解决方法

    环境说明: 项目中有一个基类窗体BaseForm,有一个自定义控件TextBoxBase,两个控件都做了一些独特常规的封装和重写,在TextBoxBase中有一点重绘的下划线,发现在窗体运行之后,在窗体上滚动鼠标滚轮,会导致TextBoxBase下划线闪烁. 解决办法: 重写两个控件的CreateParams方法. BaseForm中添加: protected override CreateParams CreateParams { get { CreateParams cp = base.Cr

  • WinForm自定义控件应用实例

    C#的WinForm有一些控件具备自绘的功能,这就意味着你可以对这些控件进行自绘,可以起到意想不到的视觉效果.本文所述的以下控件就是通过一些简单的控件转变过来的.具体示例如下: 1.横向选项卡重绘: 这里的"横向"对话框其实是通过一个TabControl进行"方向旋转".重绘控件项等操作进行实现的.步骤如下: ①.Alignment:用于控制选项卡的方向(设置为Left). ②.SizeMode:用于调整每个选项卡,默认是Normal(非自绘模式),此处应该设置为F

  • C#中Winform 实现Ajax效果自定义按钮

    技术看点 WinForm自定义控件的使用 自定义控件gif动画的播放 需求及效果 又来一波 C# GDI自定义控件show .这个控件已经使用几年了,最近找出来重构一下.原来是没有边框的,那么导致导航的功能不是很突出.本来想加个效果:在执行单击时显示Loading动画,在执行完单击事件后恢复原样.这就是网页里见到的局部刷新,Ajax常用的场景.需求来自几年前一个智能储物柜项目,人机界面有个美工设计好的效果图,为了省事和通用,需要一个透明的按钮来实现导航的任务.就是控件只是设计时可见,运行时不可见

  • JS百度地图搜索悬浮窗功能

    这个需求的效果类似下面的截图,主要还是利用百度地图中自定义控件的功能,挺简单的.文档地址在这 http://lbsyun.baidu.com/index.php?title=jspopular 效果图: 代码 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <me

  • 开源一个微信小程序仪表盘组件过程解析

    前言 最近开发了一个小程序动态仪表盘组件,并以第三方小程序组件的形式发布到npm,任意小程序项目都可以安装这个模块,从而获得仪表盘功能. 组件功能目前还非常简单,先来预览一下效果: 感兴趣的直接看源码: https://github.com/tower1229/weapp-plugin-dashboard 下面是踩坑过程. 如何开发微信小程序自定义组件 官方提供了一个CLI工具专门用于开发小程序自定义组件,首先全局安装这个工具: npm install -g @wechat-miniprogra

  • XAML: 自定义控件中事件处理的最佳实践方法

    在开发 XAML(WPF/UWP) 应用程序中,有时候,我们需要创建自定义控件 (Custom Control) 来满足实际需求.而在自定义控件中,我们一般会用到一些原生的控件(如 Button.TextBox 等)来辅助以完成自定义控件的功能. 自定义控件并不像用户控件 (User Control) 一样,使用 Code-Behind(UI 与逻辑在一起)技术.相反,它通过把 UI 与逻辑分离而将两者解耦.因此,创建一个自定义控件会产生两个文件,一个是 Generic.xaml,在它里面定义其

  • Winform 实现进度条弹窗和任务控制

    最近要给一个 Winform 项目添加功能,需要一个能显示进度条的弹窗,还要求能够中止任务,所以就做了一个,在此做个记录总结.虽然用的是比较老的 Winform 技术,不过其中的原理都是相通的. 一.弹窗前台 首先提供一个 Winform 控件居中的小技巧: 将控件放在 TableLayoutPanel 容器中,然后将控件的 Anchor 属性设置为 None,这样控件就能在容器中居中了: 将容器的 Anchor 属性设置为 Top, Left, Right,这样容器就能随着窗口左右拉伸了: 最

  • Python集成C#实现界面操作下载文件功能的全过程

    目录 〇.写在前面 一.这个功能是怎么样的 二.WinForm 端功能实现 1. 界面设计 2. 方法定义 三.Python 端功能实现 四.运行效果 五.存在问题 总结 〇.写在前面 你想的没错,Python 和 C# 其实都可以单独实现我们要实现的功能,这里笔者只是抱着实验及学习的态度去解决问题. 我是一个 C# 程序员,目前在学习 Python,对于 Python 得天独厚的胶水语言特性自然是必须要领略一番,于是就有了本文. 学会了 Python 调用 C# 的话,就能做很多想到和想不到的

  • ASP.NET MVC中HtmlHelper控件7个大类中各个控件使用详解

    HtmlHelper类在命令System.Web.Mvc.Html之中,主要由7个静态类组成,它们分别是FormExtensions类,InputExtensions类,LinkExtensions类,SelectExtensions类,TextExtensions类,ValidationExtensions类,RenderPartialExtensions类. 为了方便开发者使用HtmlHelper控件,在视图ViewPage类中设置了一个属性Html它就是HtmlHelper类型. 一.Fo

随机推荐