SunnyUI/SunnyUI/Controls/UIImageButton.cs

462 lines
13 KiB
C#
Raw Normal View History

2020-05-11 21:11:29 +08:00
/******************************************************************************
* SunnyUI
2022-01-02 12:32:50 +08:00
* CopyRight (C) 2012-2022 ShenYongHua().
2021-02-20 15:45:47 +08:00
* QQ群56829229 QQ17612584 EMailSunnyUI@QQ.Com
2020-05-11 21:11:29 +08:00
*
* 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.
* 使
******************************************************************************
* : UIImageButton.cs
* :
2022-01-05 21:57:47 +08:00
* : V3.1
2020-05-11 21:11:29 +08:00
* : 2020-01-01
*
* 2020-01-01: V2.2.0
* 2022-03-19: V3.1.1
2020-05-11 21:11:29 +08:00
******************************************************************************/
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace Sunny.UI
{
/// <summary>
/// 图像按钮
/// </summary>
2022-04-11 13:18:21 +08:00
public sealed class UIImageButton : PictureBox, IStyleInterface, IZoomScale
2020-05-11 21:11:29 +08:00
{
private bool IsPress;
private bool IsHover;
private Image imageDisabled;
private Image imagePress;
private Image imageHover;
private Image imageSelected;
private bool selected;
private string text;
private ContentAlignment textAlign = ContentAlignment.MiddleCenter;
private Color foreColor = UIFontColor.Primary;
2022-06-23 23:27:55 +08:00
/// <summary>
/// 禁止控件跟随窗体缩放
/// </summary>
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
2022-04-11 13:18:21 +08:00
public bool ZoomScaleDisabled { get; set; }
2022-06-23 23:27:55 +08:00
/// <summary>
/// 控件缩放前在其容器里的位置
/// </summary>
2022-04-11 13:18:21 +08:00
[Browsable(false)]
public Rectangle ZoomScaleRect { get; set; }
2022-06-23 23:27:55 +08:00
/// <summary>
/// 设置控件缩放比例
/// </summary>
/// <param name="scale">缩放比例</param>
2022-04-11 13:18:21 +08:00
public void SetZoomScale(float scale)
{
2022-04-11 13:18:21 +08:00
}
/// <summary>
/// 主题样式
/// </summary>
[DefaultValue(UIStyle.Blue), Description("主题样式"), Category("SunnyUI")]
public UIStyle Style
{
get => _style;
set => SetStyle(value);
}
/// <summary>
/// Tag字符串
/// </summary>
[DefaultValue(null)]
[Description("获取或设置包含有关控件的数据的对象字符串"), Category("SunnyUI")]
public string TagString { get; set; }
public void SetStyleColor(UIBaseStyle uiColor)
{
foreColor = uiColor.ImageButtonForeColor;
}
public void SetStyle(UIStyle style)
{
if (!style.IsCustom())
{
SetStyleColor(style.Colors());
Invalidate();
}
_style = style;
}
private UIStyle _style = UIStyle.Blue;
public bool IsScaled { get; private set; }
public void SetDPIScale()
{
if (!IsScaled)
{
this.SetDPIScaleFont();
IsScaled = true;
}
}
[Category("SunnyUI")]
2020-05-11 21:11:29 +08:00
[Description("按钮文字")]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override string Text
{
get => text;
set
{
if (text != value)
{
text = value;
Invalidate();
}
}
}
[Description("文字对齐方式"), Category("SunnyUI")]
2020-05-11 21:11:29 +08:00
[DefaultValue(ContentAlignment.MiddleCenter)]
public ContentAlignment TextAlign
{
get => textAlign;
set
{
textAlign = value;
Invalidate();
}
}
[Category("SunnyUI")]
2020-05-11 21:11:29 +08:00
[Description("文字字体")]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override Font Font
{
2020-06-27 16:53:32 +08:00
get => base.Font;
2020-05-11 21:11:29 +08:00
set
{
2020-06-27 16:53:32 +08:00
base.Font = value;
2020-05-11 21:11:29 +08:00
Invalidate();
}
}
[Category("SunnyUI")]
2020-05-11 21:11:29 +08:00
[Description("文字颜色")]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[DefaultValue(typeof(Color), "48, 48, 48")]
public override Color ForeColor
{
get => foreColor;
set
{
foreColor = value;
Invalidate();
}
}
[Browsable(false)]
[DefaultValue(typeof(Image), "null")]
[Description("初始化图片"), Category("SunnyUI")]
2020-05-11 21:11:29 +08:00
public new Image InitialImage { get; set; }
[Browsable(false)]
[DefaultValue(typeof(Image), "null")]
[Description("出错图片"), Category("SunnyUI")]
2020-05-11 21:11:29 +08:00
public new Image ErrorImage { get; set; }
/// <summary>
/// 构造函数
/// </summary>
public UIImageButton()
{
SetDefaultControlStyles();
SuspendLayout();
BorderStyle = BorderStyle.None;
ResumeLayout(false);
Width = 100;
Height = 35;
Version = UIGlobal.Version;
Cursor = Cursors.Hand;
base.Font = UIFontColor.Font();
2020-05-11 21:11:29 +08:00
}
/// <summary>
/// 自定义主题风格
/// </summary>
[DefaultValue(false)]
[Description("获取或设置可以自定义主题风格"), Category("SunnyUI")]
public bool StyleCustomMode { get; set; }
2020-05-11 21:11:29 +08:00
public string Version { get; }
/// <summary>
/// 鼠标移上图片
/// </summary>
[DefaultValue(typeof(Image), "null")]
[Description("鼠标移上图片"), Category("SunnyUI")]
2020-05-11 21:11:29 +08:00
public Image ImageHover
{
get => imageHover;
set
{
imageHover = value;
Invalidate();
}
}
/// <summary>
/// 鼠标按下图片
/// </summary>
[DefaultValue(typeof(Image), "null")]
[Description("鼠标按下图片"), Category("SunnyUI")]
2020-05-11 21:11:29 +08:00
public Image ImagePress
{
get => imagePress;
set
{
imagePress = value;
Invalidate();
}
}
/// <summary>
/// 不可用时图片
/// </summary>
[DefaultValue(typeof(Image), "null")]
[Description("不可用时图片"), Category("SunnyUI")]
2020-05-11 21:11:29 +08:00
public Image ImageDisabled
{
get => imageDisabled;
set
{
imageDisabled = value;
Invalidate();
}
}
/// <summary>
/// 选中时图片
2020-05-11 21:11:29 +08:00
/// </summary>
[DefaultValue(typeof(Image), "null")]
[Description("选中时图片"), Category("SunnyUI")]
2020-05-11 21:11:29 +08:00
public Image ImageSelected
{
get => imageSelected;
set
{
imageSelected = value;
Invalidate();
}
}
/// <summary>
/// 是否选中
/// </summary>
[DefaultValue(typeof(bool), "false")]
[Description("是否选中"), Category("SunnyUI")]
2020-05-11 21:11:29 +08:00
public bool Selected
{
get => selected;
set
{
if (selected != value)
{
selected = value;
Invalidate();
}
}
}
private void SetDefaultControlStyles()
{
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.DoubleBuffer, true);
UpdateStyles();
}
/// <summary>
2022-06-27 17:06:11 +08:00
/// 重载鼠标按下事件
2020-05-11 21:11:29 +08:00
/// </summary>
2022-06-27 17:06:11 +08:00
/// <param name="e">鼠标参数</param>
2020-05-11 21:11:29 +08:00
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
IsPress = true;
Invalidate();
}
/// <summary>
2022-06-27 17:06:11 +08:00
/// 重载鼠标抬起事件
2020-05-11 21:11:29 +08:00
/// </summary>
2022-06-27 17:06:11 +08:00
/// <param name="e">鼠标参数</param>
2020-05-11 21:11:29 +08:00
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
IsPress = false;
Invalidate();
}
/// <summary>
2022-06-27 17:06:11 +08:00
/// 重载鼠标进入事件
2020-05-11 21:11:29 +08:00
/// </summary>
2022-06-27 17:06:11 +08:00
/// <param name="e">鼠标参数</param>
2020-05-11 21:11:29 +08:00
protected override void OnMouseEnter(EventArgs e)
{
base.OnMouseEnter(e);
if (!DesignMode)
{
Cursor = Cursors.Hand;
}
IsHover = true;
Invalidate();
}
/// <summary>
2022-06-27 17:06:11 +08:00
/// 重载鼠标离开事件
2020-05-11 21:11:29 +08:00
/// </summary>
2022-06-27 17:06:11 +08:00
/// <param name="e">鼠标参数</param>
2020-05-11 21:11:29 +08:00
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
IsHover = false;
IsPress = false;
Invalidate();
}
private Point imageOffset;
2020-06-17 22:54:42 +08:00
[DefaultValue(typeof(Point), "0, 0")]
[Description("图片偏移位置"), Category("SunnyUI")]
2020-06-17 22:54:42 +08:00
public Point ImageOffset
{
get => imageOffset;
set
{
imageOffset = value;
Invalidate();
}
}
2020-05-11 21:11:29 +08:00
/// <summary>
/// 绘制按钮
/// </summary>
/// <param name="pe">pe</param>
protected override void OnPaint(PaintEventArgs pe)
{
Image img = Image;
if (!Enabled)
{
img = imageDisabled;
}
else
{
if (IsPress)
{
img = imagePress;
}
else if (IsHover)
{
img = imageHover;
}
if (Selected)
{
img = imageSelected;
}
}
if (img == null)
{
img = Image;
}
if (img != null)
{
if (SizeMode == PictureBoxSizeMode.Normal)
2020-06-17 22:54:42 +08:00
pe.Graphics.DrawImage(img, new Rectangle(ImageOffset.X, ImageOffset.Y, img.Width, img.Height));
2020-05-11 21:11:29 +08:00
if (SizeMode == PictureBoxSizeMode.StretchImage)
pe.Graphics.DrawImage(img, new Rectangle(0, 0, Width, Height));
if (SizeMode == PictureBoxSizeMode.AutoSize)
{
Width = img.Width;
Height = img.Height;
pe.Graphics.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height));
}
if (SizeMode == PictureBoxSizeMode.Zoom)
pe.Graphics.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height));
if (SizeMode == PictureBoxSizeMode.CenterImage)
pe.Graphics.DrawImage(img, new Rectangle((Width - img.Width) / 2, (Height - img.Height) / 2, img.Width, img.Height));
}
else
{
base.OnPaint(pe);
}
SizeF sf = pe.Graphics.MeasureString(Text, Font);
switch (TextAlign)
{
case ContentAlignment.TopLeft:
pe.Graphics.DrawString(text, Font, ForeColor, Padding.Left, Padding.Top);
break;
case ContentAlignment.TopCenter:
pe.Graphics.DrawString(text, Font, ForeColor, (Width - sf.Width) / 2, Padding.Top);
break;
case ContentAlignment.TopRight:
pe.Graphics.DrawString(text, Font, ForeColor, Width - Padding.Right - sf.Width, Padding.Top);
break;
case ContentAlignment.MiddleLeft:
pe.Graphics.DrawString(text, Font, ForeColor, Padding.Left, (Height - sf.Height) / 2);
break;
case ContentAlignment.MiddleCenter:
pe.Graphics.DrawString(text, Font, ForeColor, (Width - sf.Width) / 2, (Height - sf.Height) / 2);
break;
case ContentAlignment.MiddleRight:
pe.Graphics.DrawString(text, Font, ForeColor, Width - Padding.Right - sf.Width, (Height - sf.Height) / 2);
break;
case ContentAlignment.BottomLeft:
pe.Graphics.DrawString(text, Font, ForeColor, Padding.Left, Height - Padding.Bottom - sf.Height);
break;
case ContentAlignment.BottomCenter:
pe.Graphics.DrawString(text, Font, ForeColor, (Width - sf.Width) / 2, Height - Padding.Bottom - sf.Height);
break;
case ContentAlignment.BottomRight:
pe.Graphics.DrawString(text, Font, ForeColor, Width - Padding.Right - sf.Width, Height - Padding.Bottom - sf.Height);
break;
}
}
}
}