using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace ColorButton
{
public class ColorButton : System.Windows.Forms.Control
{
private enum _States
{
Normal,
MouseOver,
Clicked
}
public enum ButtonStyles
{
Rectangle,
Ellipse
}
// default values
private bool _Active = true;
private _States _State = _States.Normal;
private ButtonStyles _ButtonStyle = ButtonStyles.Rectangle;
private int handle = 0;
private Color _NormalBorderColor = Color.Green;
private Color _NormalColorA = Color.Green;
private Color _NormalColorB = Color.Honeydew;
private Color _HoverBorderColor = Color.MediumVioletRed;
private Color _HoverColorA = Color.MediumVioletRed;
private Color _HoverColorB = Color.White;
public ColorButton(int ihandle)
{
// initiate button size and font
base.Size = new Size(200, 40);
base.Font = new Font("Verdana", 10, FontStyle.Bold);
this.handle = ihandle;
}
public int getHandle()
{
return this.handle;
}
public ButtonStyles ButtonStyle
{
get
{
return _ButtonStyle;
}
set
{
_ButtonStyle = value;
this.Invalidate();
}
}
public Color NormalBorderColor
{
get
{
return _NormalBorderColor;
}
set
{
_NormalBorderColor = value;
this.Invalidate();
}
}
public Color HoverBorderColor
{
get
{
return _HoverBorderColor;
}
set
{
_HoverBorderColor = value;
this.Invalidate();
}
}
public Color NormalColorA
{
get
{
return _NormalColorA;
}
set
{
_NormalColorA = value;
this.Invalidate();
}
}
public Color NormalColorB
{
get
{
return _NormalColorB;
}
set
{
_NormalColorB = value;
this.Invalidate();
}
}
public Color HoverColorA
{
get
{
return _HoverColorA;
}
set
{
_HoverColorA = value;
this.Invalidate();
}
}
public Color HoverColorB
{
get
{
return _HoverColorB;
}
set
{
_HoverColorB = value;
this.Invalidate();
}
}
public bool Active
{
get
{
return _Active;
}
set
{
_Active = value;
this.Invalidate();
}
}
// to make sure the control is invalidated(repainted) when the text is changed
public override string Text
{
get
{
return base.Text;
}
set
{
base.Text = value;
this.Invalidate();
}
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
SolidBrush brush;
SizeF textSize = e.Graphics.MeasureString(this.Text, base.Font);
int textX = (int)(base.Size.Width / 2) - (int)(textSize.Width / 2);
int textY = (int)(base.Size.Height / 2) - (int)(textSize.Height / 2);
Rectangle newRect = new Rectangle(ClientRectangle.X + 1, ClientRectangle.Y + 1,
ClientRectangle.Width - 3, ClientRectangle.Height - 3);
if (_Active)
{
brush = new SolidBrush(_NormalColorA);
switch (_ButtonStyle)
{
case ButtonStyles.Rectangle:
e.Graphics.FillRectangle(brush, newRect);
e.Graphics.DrawRectangle(new Pen(_NormalBorderColor), newRect);
break;
case ButtonStyles.Ellipse:
e.Graphics.FillEllipse(brush, newRect);
e.Graphics.DrawEllipse(new Pen(_NormalBorderColor), newRect);
break;
}
e.Graphics.DrawString(this.Text, base.Font, new SolidBrush(base.ForeColor), textX, textY);
}
else
{
brush = new SolidBrush(_NormalColorA);
switch (_ButtonStyle)
{
case ButtonStyles.Rectangle:
e.Graphics.FillRectangle(brush, newRect);
e.Graphics.DrawRectangle(new Pen(_NormalBorderColor), newRect);
break;
case ButtonStyles.Ellipse:
e.Graphics.FillEllipse(brush, newRect);
e.Graphics.DrawEllipse(new Pen(_NormalBorderColor), newRect);
break;
}
e.Graphics.DrawString(this.Text, base.Font, new SolidBrush(_NormalColorA), textX, textY);
}
}
protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
{
if (_Active)
{
_State = _States.MouseOver;
this.Invalidate();
base.OnMouseUp(e);
}
}
protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
{
if (_Active)
{
_State = _States.Clicked;
this.Invalidate();
base.OnMouseDown(e);
}
}
protected override void OnClick(System.EventArgs e)
{
// prevent click when button is inactive
if (_Active)
{
if (_State == _States.Clicked)
{
base.OnClick(e);
}
}
}
}
}