using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Text; using System.Windows.Forms; namespace ControlLib.inh { /// /// 图片按钮 /// public partial class ImageButton : PictureBox { private Image m_imgUp = null; private Image m_imgOver = null; private Image m_imgDown = null; private Image m_imgDisable = null; private Bitmap m_grayDisable = null; private string m_strUp = string.Empty; private string m_strOver = string.Empty; private string m_strDown = string.Empty; private string m_strDisable = string.Empty; public void Init(string strUp) { Init(strUp, string.Empty, string.Empty, string.Empty); } public void Init(string strUp, string strOver) { Init(strUp, strOver, string.Empty, string.Empty); } public void Init(string strUp, string strOver, string strDown) { Init(strUp, strOver, strDown,string.Empty); } public void Init(string strUp, string strOver, string strDown, string strDisable) { m_strUp = strUp; m_strOver = strOver; m_strDown = strDown; m_strDisable = strDisable; ChangeState(m_strUp, ref m_imgUp); } private void ImageButton_MouseEnter(object sender, EventArgs e) { if (m_LockStated) return; ChangeState(m_strOver, ref m_imgOver); } private void ImageButton_MouseLeave(object sender, EventArgs e) { if (m_LockStated) return; ChangeState(m_strUp, ref m_imgUp); } private void ImageButton_MouseDown(object sender, EventArgs e) { if (m_LockStated) return; ChangeState(m_strDown, ref m_imgDown); } private void ChangeState(string strSource, ref Image imgSource) { if (strSource == string.Empty) return; if (imgSource == null) imgSource = Image.FromFile(strSource); this.Image = imgSource; //CommonLib.PubUtil.WriteLog("***************************************************************"); } protected override void Dispose(bool disposing) { if(m_imgUp!=null) m_imgUp.Dispose(); if(m_imgOver!=null) m_imgOver.Dispose(); if(m_imgDown!=null) m_imgDown.Dispose(); if(m_imgDisable!=null) m_imgDisable.Dispose(); if(m_grayDisable!=null) m_grayDisable.Dispose(); m_imgUp = m_imgOver = m_imgDown = m_imgDisable = m_grayDisable = null; base.Dispose(disposing); } //是否处在锁定状态 private bool m_LockStated = false; public void LockState(bool up, bool over) { LockState(up, over, false, false); } public void LockState(bool up, bool over, bool down,bool disable) { m_LockStated = true; if(up) ChangeState(m_strUp, ref m_imgUp); if (over) ChangeState(m_strOver, ref m_imgOver); if (down) ChangeState(m_strDown, ref m_imgDown); if (disable) ChangeState(m_strDisable, ref m_imgDisable); } /// /// 释放锁,切换到默认状态 /// public void ReleaseLock() { m_LockStated = false; ChangeState(m_strUp, ref m_imgUp); } //public override bool Enabled无法覆盖 private void ImageButton_EnabledChanged(object sender, EventArgs e) { if (m_LockStated) return; if (this.Enabled) { ChangeState(m_strUp, ref m_imgUp); } else { ChangeState(m_strDisable, ref m_imgDisable); if (m_imgDisable == null) { if (m_grayDisable == null && m_imgUp != null) { m_grayDisable = new Bitmap(m_imgUp.Width, m_imgUp.Height); Bitmap bitTmp = (Bitmap)m_imgUp; Color pixel; for (int x = 0; x < m_grayDisable.Width; x++) { for (int y = 0; y < m_grayDisable.Height; y++) { pixel = bitTmp.GetPixel(x, y); int r = pixel.R; int g = pixel.G; int b = pixel.B; int m = ((int)(0.7 * r) + (int)(0.2 * g) + (int)(0.1 * b)); m_grayDisable.SetPixel(x, y, Color.FromArgb(m, m, m)); } } } this.Image = m_grayDisable; } } } public ImageButton() { //设置双缓冲 this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint,true); this.MouseDown += new MouseEventHandler(ImageButton_MouseDown); this.MouseEnter += new EventHandler(ImageButton_MouseEnter); this.MouseLeave += new EventHandler(ImageButton_MouseLeave); this.EnabledChanged += new EventHandler(ImageButton_EnabledChanged); } /// /// 附带的参数 /// private object m_Source = null; public object Source { get { return m_Source; } set { m_Source = value; } } public object[] SourceValue = null; } }