SunnyUI/SunnyUI/Controls/UIButton.cs

348 lines
9.6 KiB
C#
Raw Normal View History

2020-05-11 21:11:29 +08:00
/******************************************************************************
* SunnyUI
* CopyRight (C) 2012-2020 ShenYongHua().
* QQ群56829229 QQ17612584 EMailSunnyUI@qq.com
*
* Blog: https://www.cnblogs.com/yhuse
* Gitee: https://gitee.com/yhuse/SunnyUI
* GitHub: https://github.com/yhuse/SunnyUI
*
* SunnyUI.dll can be used for free under the GPL-3.0 license.
* If you use this code, please keep this note.
* 使
******************************************************************************
* : UIButton.cs
* :
* : V2.2
* : 2020-01-01
*
* 2020-01-01: V2.2.0
* 2020-04-25: V2.2.4
******************************************************************************/
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
2020-05-11 21:11:29 +08:00
using System.Windows.Forms;
// ReSharper disable All
#pragma warning disable 1591
namespace Sunny.UI
{
[DefaultEvent("Click")]
[DefaultProperty("Text")]
[ToolboxItem(true)]
public class UIButton : UIControl, IButtonControl
2020-05-11 21:11:29 +08:00
{
public UIButton()
{
TabStop = true;
Width = 100;
Height = 35;
Cursor = Cursors.Hand;
foreHoverColor = UIStyles.Blue.ButtonForeHoverColor;
forePressColor = UIStyles.Blue.ButtonForePressColor;
rectHoverColor = UIStyles.Blue.RectHoverColor;
rectPressColor = UIStyles.Blue.RectPressColor;
fillHoverColor = UIStyles.Blue.ButtonFillHoverColor;
fillPressColor = UIStyles.Blue.ButtonFillPressColor;
}
2020-06-01 22:52:50 +08:00
private bool isClick;
public void PerformClick()
{
if (isClick) return;
if (CanSelect && Enabled)
{
isClick = true;
OnClick(EventArgs.Empty);
isClick = false;
}
}
protected override void OnClick(EventArgs e)
{
Focus();
base.OnClick(e);
}
2020-05-11 21:11:29 +08:00
private bool showTips = false;
[Description("是否显示角标"), Category("自定义")]
[DefaultValue(false)]
public bool ShowTips
{
get
{
return showTips;
}
set
{
if (showTips != value)
{
showTips = value;
Invalidate();
}
}
}
private string tipsText = "";
[Description("角标文字"), Category("自定义")]
[DefaultValue("")]
public string TipsText
{
get { return tipsText; }
set
{
if (tipsText != value)
{
tipsText = value;
if (ShowTips)
{
Invalidate();
}
}
}
}
private Font tipsFont = new Font("Microsoft Sans Serif", 9);
[Description("角标文字字体"), Category("自定义")]
[DefaultValue(typeof(Font), "Microsoft Sans Serif, 9pt")]
public Font TipsFont
{
get { return tipsFont; }
set
{
if (!tipsFont.Equals(value))
{
tipsFont = value;
Invalidate();
}
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (Enabled && ShowTips && !string.IsNullOrEmpty(TipsText))
{
e.Graphics.SetHighQuality();
SizeF sf = e.Graphics.MeasureString(TipsText, TipsFont);
float sfMax = Math.Max(sf.Width, sf.Height);
float x = Width - 1 - 2 - sfMax;
float y = 1 + 1;
e.Graphics.FillEllipse(UIColor.Red, x, y, sfMax, sfMax);
e.Graphics.DrawString(TipsText, TipsFont, Color.White, x + sfMax / 2.0f - sf.Width / 2.0f, y + sfMax / 2.0f - sf.Height / 2.0f);
2020-05-11 21:11:29 +08:00
}
if (Focused && ShowFocusLine)
{
Rectangle rect = new Rectangle(2, 2, Width - 5, Height - 5);
var path = rect.CreateRoundedRectanglePath(Radius);
using (Pen pn = new Pen(ForeColor))
{
pn.DashStyle = DashStyle.Dot;
e.Graphics.DrawPath(pn, path);
}
path.Dispose();
}
2020-05-11 21:11:29 +08:00
}
private bool selected;
/// <summary>
/// 是否选中
/// </summary>
[DefaultValue(false)]
public bool Selected
{
get => selected;
set
{
if (selected != value)
{
selected = value;
Invalidate();
}
}
}
public override void SetStyleColor(UIBaseStyle uiColor)
{
base.SetStyleColor(uiColor);
if (uiColor.IsCustom()) return;
fillHoverColor = uiColor.ButtonFillHoverColor;
rectHoverColor = uiColor.RectHoverColor;
foreHoverColor = uiColor.ButtonForeHoverColor;
fillPressColor = uiColor.ButtonFillPressColor;
rectPressColor = uiColor.RectPressColor;
forePressColor = uiColor.ButtonForePressColor;
Invalidate();
}
/// <summary>
/// 填充颜色,当值为背景色或透明色或空值则不填充
/// </summary>
[Description("填充颜色"), Category("自定义")]
[DefaultValue(typeof(Color), "80, 160, 255")]
public Color FillColor
{
get => fillColor;
set => SetFillColor(value);
}
/// <summary>
/// 边框颜色
/// </summary>
[Description("边框颜色"), Category("自定义")]
[DefaultValue(typeof(Color), "80, 160, 255")]
public Color RectColor
{
get => rectColor;
set => SetRectColor(value);
}
/// <summary>
/// 字体颜色
/// </summary>
[Description("字体颜色"), Category("自定义")]
[DefaultValue(typeof(Color), "White")]
public override Color ForeColor
{
get => foreColor;
set => SetForeColor(value);
}
[DefaultValue(typeof(Color), "244, 244, 244")]
public Color FillDisableColor
{
get => fillDisableColor;
set => SetFillDisableColor(value);
}
[DefaultValue(typeof(Color), "173, 178, 181")]
public Color RectDisableColor
{
get => rectDisableColor;
set => SetRectDisableColor(value);
}
[DefaultValue(typeof(Color), "109, 109, 103")]
public Color ForeDisableColor
{
get => foreDisableColor;
set => SetForeDisableColor(value);
}
[DefaultValue(typeof(Color), "111, 168, 255")]
public Color FillHoverColor
{
get => fillHoverColor;
set => SetFillHoveColor(value);
}
[DefaultValue(typeof(Color), "74, 131, 229")]
public Color FillPressColor
{
get => fillPressColor;
set => SetFillPressColor(value);
}
[DefaultValue(typeof(Color), "White")]
public Color ForeHoverColor
{
get => foreHoverColor;
set => SetForeHoveColor(value);
}
[DefaultValue(typeof(Color), "White")]
public Color ForePressColor
{
get => forePressColor;
set => SetForePressColor(value);
}
[DefaultValue(typeof(Color), "111, 168, 255")]
public Color RectHoverColor
{
get => rectHoverColor;
set => SetRectHoveColor(value);
}
[DefaultValue(typeof(Color), "74, 131, 229")]
public Color RectPressColor
{
get => rectPressColor;
set => SetRectPressColor(value);
}
[DefaultValue(typeof(Color), "64, 105, 225")]
public Color SelectedColor
{
get => selectedColor;
set => SetSelectedColor(value);
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
IsPress = true;
Invalidate();
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
IsPress = false;
Invalidate();
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
IsPress = false;
IsHover = false;
Invalidate();
}
protected override void OnMouseEnter(EventArgs e)
{
base.OnMouseEnter(e);
IsHover = true;
Invalidate();
}
public void NotifyDefault(bool value)
{
}
[DefaultValue(DialogResult.None)]
public DialogResult DialogResult { get; set; } = DialogResult.None;
protected override void OnKeyDown(KeyEventArgs e)
{
if (Focused && e.KeyCode == Keys.Space)
{
PerformClick();
}
base.OnKeyDown(e);
}
[DefaultValue(false)]
public bool ShowFocusLine { get; set; }
2020-05-11 21:11:29 +08:00
}
}