报警器

1 、效果

 

 2 代码编写

 /// <summary>
    /// 报警器  前台页面不用做任何设置
    /// </summary>
    public partial class UCAlarmControl : UserControl
    {
        public UCAlarmControl()
        {
            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.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
                      this.SizeChanged += UCAlarmControl_SizeChanged;
                      this.Size = new Size(50, 50);
                      timer = new Timer();
                      timer.Interval = 200;
                      timer.Tick += timer_Tick;
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            intColorIndex++;
            if (intColorIndex == AlamLightColor.Length)
                intColorIndex = 0;
            Refresh();
        }

        private void UCAlarmControl_SizeChanged(object sender, EventArgs e)
        {
            m_rectWorking = new Rectangle(this.Width/8, this.Height / 8, this.Width - this.Width/4, this.Height - this.Height / 4);
        }

        private Color[] alamLightColor =  { Color.Red };

        /// <summary>
        /// 报警灯的颜色
        /// </summary>
        /// <value>The color of the lamp.</value>
        [Description("灯颜色,当需要闪烁时,至少需要2个及以上颜色,不需要闪烁则至少需要1个颜色"), Category("自定义")]
        public Color[] AlamLightColor
        {
            get { return alamLightColor; }
            set
            {
                if (value == null || value.Length <= 0)
                    return;
                alamLightColor = value;
                Refresh();
            }
        }


        private Color alarmStandColor = Color.FromArgb(105, 105, 105);
        /// <summary>
        /// 灯座颜色
        /// </summary>
        /// <value></value>
        [Description("灯座颜色"), Category("自定义")]
        public Color AlarmStandColor
        {
            get { return alarmStandColor; }
            set {
                alarmStandColor = value;
                Refresh();
            }
        }

        private int twinkleSpeed = 0;

        /// <summary>
        /// 闪烁间隔时间(毫秒)
        /// </summary>
        [Description("闪烁间隔时间(毫秒),当为0时不闪烁"), Category("自定义")]
        public int TwinkleSpeed
        {
            get { return twinkleSpeed; }
            set
            {
                if (value < 0)
                    return;
                twinkleSpeed = value;
                if (value == 0 || AlamLightColor.Length <= 1)
                {
                    timer.Enabled = false;
                }
                else
                {
                    intColorIndex = 0;
                }
                Refresh();
            }
        }

        private bool isOn;
        /// <summary>
        /// 是否报警
        /// </summary>
        public bool IsOn
        {
            get { return isOn; }
            set { isOn = value;
                if (isOn&&twinkleSpeed>0)
                {
                    timer.Interval = twinkleSpeed;
                    timer.Enabled = true;
                }
                else
                {
                    timer.Enabled = false;
                    intColorIndex = 0;
                }
                Refresh();
            }
        }

        private string varName;
        /// <summary>
        ///报警状态参数名称
        /// </summary>
        public string VarName
        {
            get { return varName; }
            set { varName = value; }
        }


        /// <summary>
        ///计时器
        /// </summary>
        Timer timer;
        /// <summary>
        /// 颜色索引
        /// </summary>
        int intColorIndex = 0;
        /// <summary>
        /// 工作区
        /// </summary>
        Rectangle m_rectWorking;

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            var g = e.Graphics;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            Color c1 = AlamLightColor[intColorIndex];
            GraphicsPath path = new GraphicsPath();
            path.AddLine(new Point(m_rectWorking.Left, m_rectWorking.Bottom - this.Height / 4+2), new Point(m_rectWorking.Left, m_rectWorking.Top + m_rectWorking.Width));
            path.AddArc(new Rectangle(m_rectWorking.Left, m_rectWorking.Top, m_rectWorking.Width, m_rectWorking.Width), 180f, 180f);
            path.AddLine(new Point(m_rectWorking.Right, m_rectWorking.Top + m_rectWorking.Width), new Point(m_rectWorking.Right, m_rectWorking.Bottom - this.Height / 4+2));
            path.CloseAllFigures();
            g.FillPath(new SolidBrush(c1), path);

            //g.FillRectangle(new SolidBrush(AlarmStandColor), new Rectangle(this.Width/2-(this.Width/8)*3, this.Height- this.Height/4, this.Width -this.Width/4, this.Height/8));
            g.FillRectangle(new SolidBrush(AlarmStandColor), new Rectangle(0, m_rectWorking.Bottom - this.Height/4, this.Width, this.Height/4));

        }
    }

 

posted @ 2024-05-20 11:21  陌念  阅读(18)  评论(0)    收藏  举报