using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Mesnac.Controls.ChemicalWeighing { /// /// 红灯 /// [ToolboxBitmap(typeof(YellewLight), "Resources.AlarmLight.bmp")]//新添加的代码 public partial class RedLight : ChemicalWeighingControl { bool bNewPic = false; string _statusName; private bool _alarm; public string StatusName { get { return _statusName; } set { _statusName = value; } } public enum Statuses { ylNormal = 0, ylAlarm = 1, ylNormal2 = 2 } Statuses _status; string[] sImages; public RedLight() : base() { InitializeComponent(); Init(); ReloadStream(); } protected override void Init() { base.Init(); _imageStream = null; sImages = new string[3]; sImages[0] = "Mesnac.Controls.ChemicalWeighing.Resources.AlarmLightNewGray.png";// sImages[1] = "Mesnac.Controls.ChemicalWeighing.Resources.AlarmLightNewRed.png";// sImages[2] = "Mesnac.Controls.ChemicalWeighing.Resources.AlarmLightNewGray.png";// _status = Statuses.ylNormal; } public string AlarmName { get; set; } public bool Alarm { get => _alarm; set { _alarm = value; R(); } } private void ReloadStream() { //CloseStream(); int nIndex = 0; if (Alarm) { nIndex = 2; } else { nIndex = (int)_status; } _imageStream = _assembly.GetManifestResourceStream(sImages[nIndex]); } public Statuses Status { get { return _status; } set { if (bFirstCreated == true) { bFirstCreated = false; } //if (_status != value &&(this.Size.Width != 150 || this.Size.Height != 150) ) if (_status != value) { if (this.DesignMode == true) { bNewPic = true; } } if ((int)value < 0) { _status = (Statuses)0; } else if ((int)value > 2) { _status = (Statuses)2; } else { _status = value; } ReloadStream(); Reload(); } } private void Reload() { if (_imageStream != null) { Image img = Image.FromStream(_imageStream); if (bNewPic == true) { //this.Size = img.Size; bNewPic = false; } pictureBox1.Image = img; pictureBox1.Refresh(); } } protected override void OnPaint(PaintEventArgs e) { if (_imageStream != null) { Graphics g = e.Graphics; Image img = Image.FromStream(_imageStream); if (bNewPic == true) { //this.Size = img.Size; bNewPic = false; } g.DrawImage(img, 0, 0, this.Width, this.Height); } } private void PressureSwitch_Load(object sender, EventArgs e) { if (bFirstCreated == true && this.DesignMode == true) { bNewPic = true; bFirstCreated = false; Reload(); } } private void R() { ReloadStream(); Reload(); } } }