using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Windows; using System.Text; using System.Windows.Forms; using System.Drawing; using System.IO; using DevExpress.XtraEditors; namespace ControlLib.com { /// /// 主界面模块按钮 /// public partial class ModuleButton : Label { //鼠标移开的图片 private Image m_imgUp = null; //鼠标进入的图片 private Image m_imgOver = null; //鼠标移开的图片文件名 private string m_strUp = string.Empty; //鼠标进入的图片文件名 private string m_strOver = string.Empty; //用来接收鼠标事件的空容器 private PictureBox pic = new PictureBox(); /// /// 构造函数 /// /// 控件宽度 /// 控件高度 public ModuleButton(int w, int h) { //设置双缓冲 this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true); this.Width = w; this.Height = h; this.ImageAlign = ContentAlignment.TopCenter; this.TextAlign = ContentAlignment.BottomCenter; this.BackColor = Color.White; pic.Width = 50; pic.Height = 50; pic.MouseEnter += new EventHandler(ImageButton_MouseEnter); pic.MouseLeave += new EventHandler(ImageButton_MouseLeave); pic.Top = 0; pic.BackColor = Color.Transparent; pic.Left = (w - pic.Width) / 2; pic.Cursor = Cursors.Hand; this.Controls.Add(pic); pic.BringToFront(); } /// /// 初始化 /// /// 鼠标移开的文件名 /// 鼠标进入的文件名 public void Init(string strUp, string strOver) { m_strUp = strUp; m_strOver = strOver; if (Visible) ChangeState(m_strUp, ref m_imgUp); } private void ChangeState(string strSource, ref Image imgSource) { if (strSource == string.Empty) return; if (imgSource == null) { if (File.Exists(strSource)) { imgSource = Image.FromFile(strSource); } else { XtraMessageBox.Show(strSource, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } } this.Image = imgSource; pic.Top = 0; pic.Width = this.Image.Width; pic.Height = this.Image.Height; pic.Left = (this.Width - pic.Width) / 2; } private void ImageButton_MouseEnter(object sender, EventArgs e) { ChangeState(m_strOver, ref m_imgOver); } private void ImageButton_MouseLeave(object sender, EventArgs e) { ChangeState(m_strUp, ref m_imgUp); } protected override void OnVisibleChanged(EventArgs e) { base.OnVisibleChanged(e); if (Visible) ChangeState(m_strUp, ref m_imgUp); } /// /// 输出给外面可以点击的空容器 /// public PictureBox Button { get { return pic; } } /// /// 附带参数 /// private object m_Source = null; public object Source { get { return m_Source; } set { m_Source = value; } } public object[] SourceValue = null; } }