hiimray-co-uk-www-posts/2020/12/TImageButton.cs
2020-12-23 03:42:40 +00:00

378 lines
14 KiB
C#

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace RyzStudio.Windows.Forms
{
public class TImageButton : System.Windows.Forms.Label
{
public enum TImageBoxState
{
Disabled = 0,
Normal,
Hover,
Active
}
protected bool isEnabled = true;
protected bool isHover = false;
protected Color foreColor = Color.Black;
protected string text;
protected Size textSize = new Size();
protected DockStyle textAlign = DockStyle.Fill;
protected TImageBoxState controlState = TImageBoxState.Normal;
public TImageButton()
{
this.AutoEllipsis = true;
this.AutoSize = false;
this.BackColor = Color.Transparent;
//this.BackgroundImage = Properties.Resources.circle;
this.BackgroundImageLayout = ImageLayout.Center;
this.BorderStyle = BorderStyle.None;
this.CausesValidation = true;
this.DoubleBuffered = true;
this.FlatStyle = FlatStyle.Flat;
this.ForeColor = Color.Black;
this.ImageAlign = ContentAlignment.MiddleCenter;
//this.Margin = new Padding(0);
//this.MaximumSize = new Size(135, 135);
//this.MinimumSize = this.MaximumSize;
//this.Padding = new Padding(0);
//this.Size = this.MaximumSize;
base.TextAlign = ContentAlignment.MiddleCenter;
this.TextAlign = DockStyle.Fill;
this.UseCompatibleTextRendering = false;
this.UseMnemonic = true;
this.DisabledForeColor = Color.Black;
this.ControlState = TImageBoxState.Normal;
}
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public override bool AllowDrop { get => base.AllowDrop; set => base.AllowDrop = value; }
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public new bool AutoEllipsis { get => base.AutoEllipsis; set => base.AutoEllipsis = value; }
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public override bool AutoSize { get => base.AutoSize; set => base.AutoSize = value; }
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public override Color BackColor { get => base.BackColor; set => base.BackColor = value; }
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public override Image BackgroundImage { get => base.BackgroundImage; set => base.BackgroundImage = value; }
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public override ImageLayout BackgroundImageLayout { get => base.BackgroundImageLayout; set => base.BackgroundImageLayout = value; }
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public new BorderStyle BorderStyle { get => base.BorderStyle; set => base.BorderStyle = value; }
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public new bool CausesValidation { get => base.CausesValidation; set => base.CausesValidation = value; }
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public override Cursor Cursor { get => base.Cursor; set => base.Cursor = value; }
//[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public new bool Enabled
{
get => isEnabled;
set
{
isEnabled = value;
this.ControlState = (isEnabled ? TImageBoxState.Normal : TImageBoxState.Disabled);
}
}
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public new FlatStyle FlatStyle { get => base.FlatStyle; set => base.FlatStyle = value; }
//[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
//public override Font Font { get => base.Font; set => base.Font = value; }
//[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public override Color ForeColor { get => foreColor; set => foreColor = value; }
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public new Image Image { get => base.Image; set => base.Image = value; }
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public new ContentAlignment ImageAlign { get => base.ImageAlign; set => base.ImageAlign = value; }
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public new string ImageKey { get => base.ImageKey; set => base.ImageKey = value; }
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public new int ImageIndex { get => base.ImageIndex; set => base.ImageIndex = value; }
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public new ImageList ImageList { get => base.ImageList; set => base.ImageList = value; }
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public new ImeMode ImeMode { get => base.ImeMode; set => base.ImeMode = value; }
//[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
//public new Padding Margin { get => base.Margin; set => base.Margin = value; }
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public override Size MaximumSize { get => base.MaximumSize; set => base.MaximumSize = value; }
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public override Size MinimumSize { get => base.MinimumSize; set => base.MinimumSize = value; }
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public new Padding Padding { get => base.Padding; set => base.Padding = value; }
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public override RightToLeft RightToLeft { get => base.RightToLeft; set => base.RightToLeft = value; }
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public new object Tag { get => base.Tag; set => base.Tag = value; }
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
//public new string Text { get => base.Text; set => base.Text = value; }
public new string Text { get => base.Text; set => base.Text = string.Empty; }
[Category("Appearance")]
public string Text2
{
get => text;
set
{
base.Text = string.Empty;
text = value ?? string.Empty;
textSize = TextRenderer.MeasureText(text, this.Font);
}
}
//[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
//public new ContentAlignment TextAlign { get => base.TextAlign; set => base.TextAlign = value; }
[Category("Appearance")]
public new DockStyle TextAlign { get => textAlign; set => textAlign = value; }
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public new bool UseCompatibleTextRendering { get => base.UseCompatibleTextRendering; set => base.UseCompatibleTextRendering = value; }
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public new bool UseMnemonic { get => base.UseMnemonic; set => base.UseMnemonic = value; }
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public new bool UseWaitCursor { get => base.UseWaitCursor; set => base.UseWaitCursor = value; }
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
Point pos = calcTextPosition();
TextRenderer.DrawText(e.Graphics, this.Text2, this.Font, pos, base.ForeColor);
}
protected override void OnClick(EventArgs e)
{
if (this.Enabled || this.AllowClickOnDisabled)
{
base.OnClick(e);
}
}
protected override void OnDoubleClick(EventArgs e)
{
if (this.Enabled || this.AllowClickOnDisabled)
{
base.OnDoubleClick(e);
}
}
protected override void OnFontChanged(EventArgs e)
{
base.OnFontChanged(e);
this.Text2 = this.Text2;
}
protected override void OnMouseClick(MouseEventArgs e)
{
if (!this.Enabled && !this.AllowClickOnDisabled) return;
if (e.Button != MouseButtons.Left) return;
base.OnMouseClick(e);
}
protected override void OnMouseDoubleClick(MouseEventArgs e)
{
if (!this.Enabled && !this.AllowClickOnDisabled) return;
if (e.Button != MouseButtons.Left) return;
base.OnMouseDoubleClick(e);
}
protected override void OnMouseDown(MouseEventArgs e)
{
if (e.Button != MouseButtons.Left) return;
this.ControlState = (this.Enabled ? TImageBoxState.Active : TImageBoxState.Disabled);
base.OnMouseDown(e);
}
protected override void OnMouseEnter(EventArgs e)
{
isHover = true;
this.ControlState = (this.Enabled ? (isHover ? TImageBoxState.Hover : TImageBoxState.Normal) : TImageBoxState.Disabled);
base.OnMouseEnter(e);
}
protected override void OnMouseLeave(EventArgs e)
{
isHover = false;
this.ControlState = (this.Enabled ? (isHover ? TImageBoxState.Hover : TImageBoxState.Normal) : TImageBoxState.Disabled);
base.OnMouseLeave(e);
}
protected override void OnMouseUp(MouseEventArgs e)
{
this.ControlState = (this.Enabled ? (isHover ? TImageBoxState.Hover : TImageBoxState.Normal) : TImageBoxState.Disabled);
base.OnMouseUp(e);
}
protected override void OnCreateControl()
{
base.OnCreateControl();
this.ControlState = this.ControlState;
this.AutoSize = false;
}
[Category("Appearance (Background)")]
public bool UseBackImage { get; set; } = true;
[Category("Appearance (Foreground)")]
public bool UseForeImage { get; set; } = false;
[Category("Appearance")]
public Color DisabledForeColor { get; set; }
[Category("Appearance")]
public int TextOffset { get; set; } = 0;
[Category("Behavior")]
public bool AllowClickOnDisabled { get; set; } = false;
[Category("Appearance (Foreground)")]
public Image NormalImage { get; set; } = null;
[Category("Appearance (Foreground)")]
public Image HoverImage { get; set; } = null;
[Category("Appearance (Foreground)")]
public Image ActiveImage { get; set; } = null;
[Category("Appearance (Foreground)")]
public Image DisabledImage { get; set; } = null;
[Category("Appearance (Background)")]
public Image NormalBackImage { get; set; } = null;
[Category("Appearance (Background)")]
public Image HoverBackImage { get; set; } = null;
[Category("Appearance (Background)")]
public Image ActiveBackImage { get; set; } = null;
[Category("Appearance (Background)")]
public Image DisabledBackImage { get; set; } = null;
protected TImageBoxState ControlState
{
get => controlState;
set
{
controlState = value;
switch (controlState)
{
case TImageBoxState.Disabled:
if (this.UseBackImage) this.BackgroundImage = this.DisabledBackImage;
if (this.UseForeImage) this.Image = this.DisabledImage;
base.ForeColor = this.DisabledForeColor;
break;
case TImageBoxState.Normal:
if (this.UseBackImage) this.BackgroundImage = this.NormalBackImage;
if (this.UseForeImage) this.Image = this.NormalImage;
base.ForeColor = this.ForeColor;
break;
case TImageBoxState.Hover:
if (this.UseBackImage) this.BackgroundImage = this.HoverBackImage;
if (this.UseForeImage) this.Image = this.HoverImage;
base.ForeColor = this.ForeColor;
break;
case TImageBoxState.Active:
if (this.UseBackImage) this.BackgroundImage = this.ActiveBackImage;
if (this.UseForeImage) this.Image = this.ActiveImage;
base.ForeColor = this.ForeColor;
break;
default: break;
}
}
}
protected Point calcTextPosition()
{
Point rs = new Point();
switch (this.TextAlign)
{
case DockStyle.Top:
rs.X = ((this.DisplayRectangle.Width - textSize.Width) / 2);
rs.Y = ((this.DisplayRectangle.Height / 2) - textSize.Height - this.TextOffset);
break;
case DockStyle.Bottom:
rs.X = ((this.DisplayRectangle.Width - textSize.Width) / 2);
rs.Y = ((this.DisplayRectangle.Height / 2) + this.TextOffset);
break;
case DockStyle.Left:
rs.X = ((this.DisplayRectangle.Width / 2) - textSize.Width - this.TextOffset);
rs.Y = ((this.DisplayRectangle.Height - textSize.Height) / 2);
break;
case DockStyle.Right:
rs.X = ((this.DisplayRectangle.Width / 2) + this.TextOffset);
rs.Y = ((this.DisplayRectangle.Height - textSize.Height) / 2);
break;
case DockStyle.Fill:
case DockStyle.None:
default:
rs.X = ((this.DisplayRectangle.Width - textSize.Width) / 2);
rs.Y = ((this.DisplayRectangle.Height - textSize.Height) / 2);
break;
}
return rs;
}
}
}