Files
2026-07-10 15:25:05 +08:00

131 lines
3.7 KiB
C#

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 {
/// <summary>
/// 主界面模块按钮
/// </summary>
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();
/// <summary>
/// 构造函数
/// </summary>
/// <param name="w">控件宽度</param>
/// <param name="h">控件高度</param>
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();
}
/// <summary>
/// 初始化
/// </summary>
/// <param name="strUp">鼠标移开的文件名</param>
/// <param name="strOver">鼠标进入的文件名</param>
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);
}
/// <summary>
/// 输出给外面可以点击的空容器
/// </summary>
public PictureBox Button {
get { return pic; }
}
/// <summary>
/// 附带参数
/// </summary>
private object m_Source = null;
public object Source {
get { return m_Source; }
set { m_Source = value; }
}
public object[] SourceValue = null;
}
}