winform自定义控件---InstrumentControl仪表控件

InstrumentControl仪表控件

仪表控件一一-派生 于UserControl,用户控件
用作显示参数数据值,如电压、温度、频率等。
创建过程:
1.设置控件样式
2.属性扩展:
SplitCount分隔刻度数
MeterDegrees表盘跨度角度
MinValue 最小值
MaxValue最大值重写
Font (刻度字体)
Value 仪表值
TextLocation 值和固定文本位置
FixedText固定文本
TextFont 值、固定文本字体
ExternalRoundColor 外圆颜色
InsideSoundColor内圆颜色
BoundaryLineColor 两边界线颜色
ScaleColor刻度线颜色
ScaleValueColor刻度值文本颜色
PointerColor指针颜色
TextColor值、固定文本颜色
3.绘制画外圆弧、 内圆弧、边界线.刻度线、刻度值、值与固定文本.指针中心点、指针线

public partial class InstrumentControl : UserControl
    {
        public InstrumentControl()
        {
            InitializeComponent();
            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 += InstrumentControl_SizeChanged; ;
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
            this.Size = new Size(350, 200);
        }

        private void InstrumentControl_SizeChanged(object sender, EventArgs e)
        {
            m_rectWorking = new Rectangle(10, 10, this.Width - 20, this.Height - 20);
        }

        private int splitCount = 10;
        /// <summary>
        /// 刻度数量
        /// </summary>
        /// <value></value>
        [Description("分隔刻度数量,>1"), Category("自定义")]
        public int SplitCount
        {
            get { return splitCount; }
            set
            {
                if (value < 1)
                    return;
                splitCount = value;
                Refresh();
            }
        }
        private int meterDegrees = 150;
        /// <summary>
        /// 跨度角度
        /// </summary>
        /// <value></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>
        /// 最小值
        /// </summary>
        [Description("最小值,比最大值小"), Category("自定义")]
        public decimal MinValue
        {
            get { return minValue; }
            set
            {
                if (value >= maxValue)
                    return;
                minValue = value;
                Refresh();
            }
        }
        private decimal maxValue = 100;
        /// <summary>
        ///最大值
        /// </summary>
        [Description("最大值,比最小值大"), Category("自定义")]
        public decimal MaxValue
        {
            get { return maxValue; }
            set
            {
                if (value <= minValue)
                    return;
                maxValue = value;
                Refresh();
            }
        }
        /// <summary>
        /// 获取或设置控件显示的文字的字体。
        /// </summary>
        /// <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>
        /// 仪表值
        /// </summary>
        [Description("值,最大值与最小值之间的值"), 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>
        /// 文字位置
        /// </summary>
        /// <value>The text location.</value>
        [Description("值和固定文字显示位置"), Category("自定义")]
        public MeterTextLocation TextLocation
        {
            get { return textLocation; }
            set
            {
                textLocation = value;
                Refresh();
            }
        }
        private string fixedText;
        /// <summary>
        ///固定文字
        /// </summary>
        [Description("固定文字"), Category("自定义")]
        public string FixedText
        {
            get { return fixedText; }
            set
            {
                fixedText = value;
                Refresh();
            }
        }
        private Font textFont = DefaultFont;
        /// <summary>
        /// 固定文字字体
        /// </summary>
        [Description("值和固定文字字体"), Category("自定义")]
        public Font TextFont
        {
            get { return textFont; }
            set
            {
                textFont = value;
                Refresh();
            }
        }
        private Color externalRoundColor = Color.Red;
        /// <summary>
        /// 外边圆的颜色
        /// </summary>
        [Description("外圆颜色"), Category("自定义")]
        public Color ExternalRoundColor
        {
            get { return externalRoundColor; }
            set
            {
                externalRoundColor = value;
                Refresh();
            }
        }
        private Color insideRoundColor = Color.FromArgb(255, 77, 59);
        /// <summary>
        ///内圆颜色
        /// </summary>
        /// <value></value>
        [Description("内圆颜色"), Category("自定义")]
        public Color InsideRoundColor
        {
            get { return insideRoundColor; }
            set
            {
                insideRoundColor = value;
                Refresh();
            }
        }
        private Color boundaryLineColor = Color.FromArgb(255, 77, 59);
        /// <summary>
        /// 边界线颜色
        /// </summary>
        [Description("边界线颜色"), Category("自定义")]
        public Color BoundaryLineColor
        {
            get { return boundaryLineColor; }
            set
            {
                boundaryLineColor = value;
                Refresh();
            }
        }
        private Color scaleColor = Color.FromArgb(255, 77, 59);
        /// <summary>
        /// 刻度颜色
        /// </summary>
        [Description("刻度颜色"), Category("自定义")]
        public Color ScaleColor
        {
            get { return scaleColor; }
            set
            {
                scaleColor = value;
                Refresh();
            }
        }
        private Color scaleValueColor = Color.FromArgb(255, 77, 59);
        /// <summary>
        /// 刻度值文字颜色
        /// </summary>
        [Description("刻度值文字颜色"), Category("自定义")]
        public Color ScaleValueColor
        {
            get { return scaleValueColor; }
            set
            {
                scaleValueColor = value;
                Refresh();
            }
        }
        private Color pointerColor = Color.FromArgb(255, 77, 59);
        /// <summary>
        /// 指针颜色
        /// </summary>
        [Description("指针颜色"), Category("自定义")]
        public Color PointerColor
        {
            get { return pointerColor; }
            set
            {
                pointerColor = value;
                Refresh();
            }
        }
        private Color textColor = Color.FromArgb(255, 77, 59);
        /// <summary>
        /// 值和固定文字颜色
        /// </summary>
        [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.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            //外圆
            float fltStartAngle = -90 - (meterDegrees) / 2 + 360;//开始角度
            //矩形区域
            var r1 = new Rectangle(m_rectWorking.Location, new Size(m_rectWorking.Width, m_rectWorking.Width));
            //画圆弧
            g.DrawArc(new Pen(new SolidBrush(externalRoundColor), 3), r1, fltStartAngle, meterDegrees);

            //内圆
            var r2 = new Rectangle(m_rectWorking.Left + (m_rectWorking.Width - m_rectWorking.Width / 4) / 2, m_rectWorking.Top + (m_rectWorking.Width - m_rectWorking.Width / 4) / 2, m_rectWorking.Width / 4, m_rectWorking.Width / 4);
            //画内圆弧
            g.DrawArc(new Pen(new SolidBrush(insideRoundColor), 2), r2, fltStartAngle, meterDegrees);
            //边界线
            if (meterDegrees != 360)
            {
                float fltAngle = fltStartAngle - 180;
                //左上的点坐标
                float intY = (float)(m_rectWorking.Top + m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - m_rectWorking.Width / 8) * Math.Sin(Math.PI * (fltAngle / 180.00F))));
                float intX = (float)(m_rectWorking.Left + (m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - m_rectWorking.Width / 8) * Math.Cos(Math.PI * (fltAngle / 180.00F)))));
                //左下的点坐标
                float fltY1 = (float)(m_rectWorking.Top + m_rectWorking.Width / 2 - (m_rectWorking.Width / 8 * Math.Sin(Math.PI * (fltAngle / 180.00F))));
                float fltX1 = (float)(m_rectWorking.Left + (m_rectWorking.Width / 2 - (m_rectWorking.Width / 8 * Math.Cos(Math.PI * (fltAngle / 180.00F)))));
                //画左边界线
                g.DrawLine(new Pen(new SolidBrush(boundaryLineColor), 2), new PointF(intX, intY), new PointF(fltX1, fltY1));
                //右边两点的x坐标
                float fltx2 = m_rectWorking.Right - (fltX1 - m_rectWorking.Left);
                float intX2 = m_rectWorking.Right - (intX - m_rectWorking.Left);
                //画右边界线
                g.DrawLine(new Pen(new SolidBrush(boundaryLineColor), 2), new PointF(fltx2, fltY1), new PointF(intX2, intY));
            }
            //分割份数
            int _splitCount = splitCount * 2;
            float fltSplitValue = (float)meterDegrees / (float)_splitCount;
            for (int i = 0; i <= _splitCount; i++)
            {
                //刻度的起始角度 
                float fltAngle = (fltStartAngle + fltSplitValue * i - 180) % 360;
                //刻度线的起点坐标
                float fltY1 = (float)(m_rectWorking.Top + m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2) * Math.Sin(Math.PI * (fltAngle / 180.00F))));
                float fltX1 = (float)(m_rectWorking.Left + (m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2) * Math.Cos(Math.PI * (fltAngle / 180.00F)))));
                //结束点坐标
                float fltY2 = 0;
                float fltX2 = 0;
                if (i % 2 == 0)//长刻度
                {
                    fltY2 = (float)(m_rectWorking.Top + m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - 10) * Math.Sin(Math.PI * (fltAngle / 180.00F))));
                    fltX2 = (float)(m_rectWorking.Left + (m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - 10) * Math.Cos(Math.PI * (fltAngle / 180.00F)))));
                    if (!(meterDegrees == 360 && i == _splitCount))
                    {
                        decimal decValue = minValue + (maxValue - minValue) / _splitCount * i;
                        var txtSize = g.MeasureString(decValue.ToString("0.##"), this.Font);
                        float fltFY1 = (float)(m_rectWorking.Top - txtSize.Height / 2 + m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - 20) * Math.Sin(Math.PI * (fltAngle / 180.00F))));
                        float fltFX1 = (float)(m_rectWorking.Left - txtSize.Width / 2 + (m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - 20) * Math.Cos(Math.PI * (fltAngle / 180.00F)))));
                        //画刻度文本
                        g.DrawString(decValue.ToString("0.##"), Font, new SolidBrush(scaleValueColor), fltFX1, fltFY1);
                    }
                }
                else//短刻度
                {
                    fltY2 = (float)(m_rectWorking.Top + m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - 5) * Math.Sin(Math.PI * (fltAngle / 180.00F))));
                    fltX2 = (float)(m_rectWorking.Left + (m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - 5) * Math.Cos(Math.PI * (fltAngle / 180.00F)))));
                }
                //画刻度线
                g.DrawLine(new Pen(new SolidBrush(scaleColor), i % 2 == 0 ? 2 : 1), new PointF(fltX1, fltY1), new PointF(fltX2, fltY2));
            }
            //值文字和固定文字
            if (textLocation != 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>
        /// 值与文本显示位置
        /// </summary>
        public enum MeterTextLocation
        {
            /// <summary>
            ///无显示
            /// </summary>
            None,
            /// <summary>
            /// 上面
            /// </summary>
           Top
        }
    }


  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值