using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Drawing2D; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Rs.Controls { public enum EButtonIcon { None, EStop, Reset, Start, Stop } public partial class RsButton : ControlBase { private EButtonIcon _ButtonType; public EButtonIcon ButtonType { get { return _ButtonType; } set { _ButtonType = value; } } public RsButton() { InitializeComponent(); this.TabStop = false; lblTips.Paint += lblTips_Paint; this.lbl.MouseEnter += lbl_MouseEnter; this.lbl.MouseLeave += lbl_MouseLeave; } #region 字段属性 private bool enabledMouseEffect = false; [Description("是否启用鼠标效果"), Category("自定义")] public bool EnabledMouseEffect { get { return enabledMouseEffect; } set { enabledMouseEffect = value; } } /// /// 是否显示角标 /// /// true if this instance is show tips; otherwise, false. [Description("是否显示角标"), Category("自定义")] public bool IsShowTips { get { return this.lblTips.Visible; } set { this.lblTips.Visible = value; } } /// /// 角标文字 /// /// The tips text. [Description("角标文字"), Category("自定义")] public string TipsText { get { return this.lblTips.Text; } set { this.lblTips.Text = value; } } /// /// The BTN back color /// private Color _btnBackColor = Color.White; /// /// 按钮背景色 /// /// The color of the BTN back. [Description("按钮背景色"), Category("自定义")] public Color BtnBackColor { get { return _btnBackColor; } set { _btnBackColor = value; this.BackColor = value; } } /// /// The BTN fore color /// private Color _btnForeColor = Color.White; /// /// 按钮字体颜色 /// /// The color of the BTN fore. [Description("按钮字体颜色"), Category("自定义")] public virtual Color BtnForeColor { get { return _btnForeColor; } set { _btnForeColor = value; this.lbl.ForeColor = value; } } /// /// The BTN font /// private Font _btnFont = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); /// /// 按钮字体 /// /// The BTN font. [Description("按钮字体"), Category("自定义")] public Font BtnFont { get { return _btnFont; } set { _btnFont = value; this.lbl.Font = value; } } /// /// 按钮点击事件 /// [Description("按钮点击事件"), Category("自定义")] public event EventHandler BtnClick; /// /// 按钮按下事件 /// [Description("按钮按下事件"), Category("自定义")] public event MouseEventHandler BtnMouseDown; /// /// 按钮抬起事件 /// [Description("按钮抬起事件"), Category("自定义")] public event MouseEventHandler BtnMouseUp; /// /// The BTN text /// private string _btnText; /// /// 按钮文字 /// /// The BTN text. [Description("按钮文字"), Category("自定义")] public virtual string BtnText { get { return _btnText; } set { _btnText = value; lbl.Text = value; } } /// /// The m tips color /// private Color m_tipsColor = Color.FromArgb(232, 30, 99); /// /// 角标颜色 /// /// The color of the tips. [Description("角标颜色"), Category("自定义")] public Color TipsColor { get { return m_tipsColor; } set { m_tipsColor = value; } } [Description("鼠标效果生效时发生,需要和MouseEffected同时使用,否则无效"), Category("自定义")] public event EventHandler MouseEffecting; [Description("鼠标效果结束时发生,需要和MouseEffecting同时使用,否则无效"), Category("自定义")] public event EventHandler MouseEffected; #endregion Color m_cacheColor = Color.Empty; void lbl_MouseLeave(object sender, EventArgs e) { if (enabledMouseEffect) { if (MouseEffecting != null && MouseEffected != null) { MouseEffected(this, e); } else { if (m_cacheColor != Color.Empty) { this.FillColor = m_cacheColor; m_cacheColor = Color.Empty; } } } } void lbl_MouseEnter(object sender, EventArgs e) { if (enabledMouseEffect) { if (MouseEffecting != null && MouseEffected != null) { MouseEffecting(this, e); } else { if (FillColor != Color.Empty && FillColor != null) { m_cacheColor = this.FillColor; this.FillColor = this.FillColor.ChangeColor(-0.2f); } } } } /// /// Handles the Paint event of the lblTips control. /// /// The source of the event. /// The instance containing the event data. void lblTips_Paint(object sender, PaintEventArgs e) { e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; //使绘图质量最高,即消除锯齿 e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; e.Graphics.CompositingQuality = CompositingQuality.HighQuality; e.Graphics.FillEllipse(new SolidBrush(m_tipsColor), new Rectangle(0, 0, lblTips.Width - 1, lblTips.Height - 1)); System.Drawing.SizeF sizeEnd = e.Graphics.MeasureString(TipsText, lblTips.Font); e.Graphics.DrawString(TipsText, lblTips.Font, new SolidBrush(lblTips.ForeColor), new PointF((lblTips.Width - sizeEnd.Width) / 2, (lblTips.Height - sizeEnd.Height) / 2 + 1)); } /// /// Handles the MouseDown event of the lbl control. /// /// The source of the event. /// The instance containing the event data. private void lbl_MouseDown(object sender, MouseEventArgs e) { if (BtnMouseDown != null) { BtnMouseDown(this, e); } } private void lbl_MouseUp(object sender, MouseEventArgs e) { if (BtnMouseUp != null) { BtnMouseUp(this, null); } } private void lbl_MouseClick(object sender, EventArgs e) { if (this.BtnClick != null) BtnClick(this, e); } private void label1_Paint(object sender, PaintEventArgs e) { StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Center; FontFamily ff = IconFontHelper.PFCC.Families[0]; Font font = new Font(ff, 40); switch (ButtonType) { case EButtonIcon.None: break; case EButtonIcon.EStop: e.Graphics.DrawString("\ue605", font, new SolidBrush(this.ForeColor), label1.ClientRectangle, sf); break; case EButtonIcon.Reset: e.Graphics.DrawString("\ue600", font, new SolidBrush(this.ForeColor), label1.ClientRectangle, sf); break; case EButtonIcon.Start: e.Graphics.DrawString("\ue6b0", font, new SolidBrush(this.ForeColor), label1.ClientRectangle, sf); break; case EButtonIcon.Stop: e.Graphics.DrawString("\ue629", font, new SolidBrush(this.ForeColor), label1.ClientRectangle, sf); break; default: break; } } } }