You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

308 lines
9.8 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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; }
}
/// <summary>
/// 是否显示角标
/// </summary>
/// <value><c>true</c> if this instance is show tips; otherwise, <c>false</c>.</value>
[Description("是否显示角标"), Category("自定义")]
public bool IsShowTips
{
get
{
return this.lblTips.Visible;
}
set
{
this.lblTips.Visible = value;
}
}
/// <summary>
/// 角标文字
/// </summary>
/// <value>The tips text.</value>
[Description("角标文字"), Category("自定义")]
public string TipsText
{
get
{
return this.lblTips.Text;
}
set
{
this.lblTips.Text = value;
}
}
/// <summary>
/// The BTN back color
/// </summary>
private Color _btnBackColor = Color.White;
/// <summary>
/// 按钮背景色
/// </summary>
/// <value>The color of the BTN back.</value>
[Description("按钮背景色"), Category("自定义")]
public Color BtnBackColor
{
get { return _btnBackColor; }
set
{
_btnBackColor = value;
this.BackColor = value;
}
}
/// <summary>
/// The BTN fore color
/// </summary>
private Color _btnForeColor = Color.White;
/// <summary>
/// 按钮字体颜色
/// </summary>
/// <value>The color of the BTN fore.</value>
[Description("按钮字体颜色"), Category("自定义")]
public virtual Color BtnForeColor
{
get { return _btnForeColor; }
set
{
_btnForeColor = value;
this.lbl.ForeColor = value;
}
}
/// <summary>
/// The BTN font
/// </summary>
private Font _btnFont = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
/// <summary>
/// 按钮字体
/// </summary>
/// <value>The BTN font.</value>
[Description("按钮字体"), Category("自定义")]
public Font BtnFont
{
get { return _btnFont; }
set
{
_btnFont = value;
this.lbl.Font = value;
}
}
/// <summary>
/// 按钮点击事件
/// </summary>
[Description("按钮点击事件"), Category("自定义")]
public event EventHandler BtnClick;
/// <summary>
/// 按钮按下事件
/// </summary>
[Description("按钮按下事件"), Category("自定义")]
public event MouseEventHandler BtnMouseDown;
/// <summary>
/// 按钮抬起事件
/// </summary>
[Description("按钮抬起事件"), Category("自定义")]
public event MouseEventHandler BtnMouseUp;
/// <summary>
/// The BTN text
/// </summary>
private string _btnText;
/// <summary>
/// 按钮文字
/// </summary>
/// <value>The BTN text.</value>
[Description("按钮文字"), Category("自定义")]
public virtual string BtnText
{
get { return _btnText; }
set
{
_btnText = value;
lbl.Text = value;
}
}
/// <summary>
/// The m tips color
/// </summary>
private Color m_tipsColor = Color.FromArgb(232, 30, 99);
/// <summary>
/// 角标颜色
/// </summary>
/// <value>The color of the tips.</value>
[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);
}
}
}
}
/// <summary>
/// Handles the Paint event of the lblTips control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="PaintEventArgs" /> instance containing the event data.</param>
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));
}
/// <summary>
/// Handles the MouseDown event of the lbl control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="MouseEventArgs" /> instance containing the event data.</param>
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;
}
}
}
}