using System.Drawing; using System.Reflection; using System.Windows.Forms; namespace Mesnac.Controls.ChemicalWeighing { [ToolboxBitmap(typeof(SwithDMLight), "Resources.AlarmLight.bmp")]//新添加的代码 public partial class SwithDMLight : ChemicalWeighingControl { private string[] sImages = new string[3]; private bool _running; private bool _alarm; public SwithDMLight() { InitializeComponent(); _assembly = Assembly.GetExecutingAssembly(); sImages[0] = "Mesnac.Controls.ChemicalWeighing.Resources.AlarmLightNewGray.png";// sImages[1] = "Mesnac.Controls.ChemicalWeighing.Resources.AlarmLightNewGreen.png";// sImages[2] = "Mesnac.Controls.ChemicalWeighing.Resources.AlarmLightNewRed.png";// } public string RunningName { get; set; } public string AlarmName { get; set; } public bool Running { get => _running; set { _running = value; R(); } } public bool Alarm { get => _alarm; set { _alarm = value; R(); } } private void Reload() { if ( _imageStream != null ) { Image img = Image.FromStream( _imageStream); pictureBox1.Image = img; pictureBox1.Refresh(); } } private void ReloadStream() { int defaultIndex = 0; //如果出现报警 就去判断是直还是斜 if (Alarm) { defaultIndex = 2; } else { if (Running) { defaultIndex = 1; } } _imageStream = _assembly.GetManifestResourceStream(sImages[defaultIndex]); } private void R() { ReloadStream(); Reload(); } } }