using Bro.Common.Factory; using Bro.Common.Helper; using Bro.Common.Interface; using System; using System.Drawing; using System.Reflection; using System.Windows.Forms; namespace Bro.UI.Config { public partial class CtrlDeviceState : UserControl { public event Action OnDeviceCheckedChanged; private IDevice device = null; public IDevice Device { get => device; set { if (value != null) { device = value; lblName.Text = device.Name; device.OnDeviceStateChanged += Device_OnDeviceStateChanged; Device_OnDeviceStateChanged(device, device.CurrentState); CheckDeviceRunCtrlEnabled(); chkDevice.CheckedChanged -= ChkDevice_CheckedChanged; chkDevice.CheckedChanged += ChkDevice_CheckedChanged; } } } public bool IsChecked { get => chkDevice.Checked; set { chkDevice.CheckedChanged -= ChkDevice_CheckedChanged; chkDevice.Checked = value; chkDevice.CheckedChanged += ChkDevice_CheckedChanged; } } private void ChkDevice_CheckedChanged(object sender, EventArgs e) { OnDeviceCheckedChanged?.Invoke(Device.Id, chkDevice.Checked); } private void CheckDeviceRunCtrlEnabled() { string typeCode = Device.GetType().GetCustomAttribute()?.TypeCode; if (!string.IsNullOrWhiteSpace(typeCode)) { chkDevice.Enabled = UIFactory.IsDeviceCtrlExisted(typeCode, EnumHelper.DeviceAttributeType.RunCtrl); } else { chkDevice.Enabled = false; } //if (chkDevice.Enabled) //{ // chkDevice.Checked = true; //} } private void Device_OnDeviceStateChanged(IDevice device, EnumHelper.DeviceState deviceState) { //Graphics g = plState.CreateGraphics(); //g.Clear(SystemColors.Control); //g.FillEllipse(new SolidBrush(deviceState.GetEnumSelectedColor()), plState.ClientRectangle); plState.Refresh(); } public CtrlDeviceState() { InitializeComponent(); } public CtrlDeviceState(IDevice _device) { InitializeComponent(); Device = _device; plState.Paint += PlState_Paint; } private void PlState_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; g.Clear(SystemColors.Control); g.FillEllipse(new SolidBrush(Device.CurrentState.GetEnumSelectedColor()), plState.ClientRectangle); } } }