ucLamp.cs 1.99 KB
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MyControl
{
    public partial class ucLamp : UserControl
    {
        public ucLamp()
        {
            InitializeComponent();
            
        }
        private int _display;
        /// <summary>
        /// 显示的值
        /// </summary>
        public int display
        {
            get
            {
                return _display;
            }
            set
            {
                if (value >= 0 && value <= Int16.MaxValue)
                {
                    if (_display != value)
                    {
                        _display = value;
                        Lampdisplay(_display);
                    }
                }
            }
        }
        /// <summary>
        /// 显示函数
        /// </summary>
        /// <param name="value"></param>
        public void Lampdisplay(int value)
        {
            bool[] display = int2bools(value).Reverse().ToArray();
            //绿灯  运行+空闲
            picGreen.Visible = (display[0] == true || display[1] == true);
            //蓝灯  通讯正常+计划保养
            picBlue.Visible = (display[2] == true || display[3] == true);
            //红灯 设备异常
            picRed.Visible = (display[4] == true);
            //黄灯  调机+警告
            picYellow.Visible = (display[5] == true || display[6]);

        }

        /// <summary>
        /// 16进制int转bool数组
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        private bool[] int2bools(int s)
        {
            char[] strs = Convert.ToString(s, 2).PadLeft(16, '0').ToCharArray();
            bool[] re = new bool[16];
            for (int i = 0; i < re.Length; i++)
            {
                re[i] = strs[i] == '1';
            }
            return re;
        }
    }
}