Visual Studio .NET 2002.NET 1.0Windows 2003.NET 1.1Windows 2000Windows XPIntermediateDevVisual StudioWindows.NETC#
Custom Button with Color and Shape






3.25/5 (32 votes)
Apr 29, 2004

264791

9753
Another simple custom button control with color and shape.
Introduction
This is my first C# custom control. Please download the demo program and have a click.
Features
Users are able to define the:
- Color
- Border color
- Normal color A
- Normal color B
- Hover color A
- Hover color B
- GradientStyle
- Horizontal
- Vertical
- ForwardDiagonal
- BackwardDiagonal
- ButtonStyle
- Rectangle
- Ellipse
- SmoothingQuality
- None(crispy)
- HighSpeed
- AntiAlias
- HighQuality
You can actually feel the clicks with its flat style!
Key Techniques Used in OnPaint()
SmoothingMode
//
// set SmoothingMode
//
switch (_SmoothingQuality)
{
case SmoothingQualities.None:
e.Graphics.SmoothingMode = SmoothingMode.Default;
break;
case SmoothingQualities.HighSpeed:
e.Graphics.SmoothingMode = SmoothingMode.HighSpeed;
break;
case SmoothingQualities.AntiAlias:
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
break;
case SmoothingQualities.HighQuality:
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
break;
}
LinearGradientMode
//
// mode declaration
//
LinearGradientMode mode;
//
// set LinearGradientMode
//
switch (_GradientStyle)
{
case GradientStyles.Horizontal:
mode = LinearGradientMode.Horizontal;
break;
case GradientStyles.Vertical:
mode = LinearGradientMode.Vertical;
break;
case GradientStyles.ForwardDiagonal:
mode = LinearGradientMode.ForwardDiagonal;
break;
case GradientStyles.BackwardDiagonal:
mode = LinearGradientMode.BackwardDiagonal;
break;
default:
mode = LinearGradientMode.Vertical;
break;
}
LinearGradientBrush
//
// brush declaration
//
LinearGradientBrush brush;
switch (_State)
{
case _States.Normal:
brush = new LinearGradientBrush(newRect,
_NormalColorA, _NormalColorB, mode);
switch (_ButtonStyle)
{
case ButtonStyles.Rectangle:
e.Graphics.FillRectangle(brush, newRect);
e.Graphics.DrawRectangle(new
Pen(_NormalBorderColor, 1), newRect);
break;
case ButtonStyles.Ellipse:
e.Graphics.FillEllipse(brush, newRect);
e.Graphics.DrawEllipse(new
Pen(_NormalBorderColor, 1), newRect);
break;
}
e.Graphics.DrawString(this.Text, base.Font,
new SolidBrush(base.ForeColor), textX, textY);
break;
case _States.MouseOver:
brush = new LinearGradientBrush(newRect,
_HoverColorA, _HoverColorB, mode);
switch (_ButtonStyle)
{
case ButtonStyles.Rectangle:
e.Graphics.FillRectangle(brush, newRect);
e.Graphics.DrawRectangle(new
Pen(_HoverBorderColor, 1), newRect);
break;
case ButtonStyles.Ellipse:
e.Graphics.FillEllipse(brush, newRect);
e.Graphics.DrawEllipse(new
Pen(_HoverBorderColor, 1), newRect);
break;
}
e.Graphics.DrawString(this.Text, base.Font,
new SolidBrush(base.ForeColor), textX, textY);
break;
case _States.Clicked:
brush = new LinearGradientBrush(newRect,
_HoverColorA, _HoverColorB, mode);
switch (_ButtonStyle)
{
case ButtonStyles.Rectangle:
e.Graphics.FillRectangle(brush, newRect);
e.Graphics.DrawRectangle(new
Pen(_HoverBorderColor, 2), newRect);
break;
case ButtonStyles.Ellipse:
e.Graphics.FillEllipse(brush, newRect);
e.Graphics.DrawEllipse(new
Pen(_HoverBorderColor, 2), newRect);
break;
}
e.Graphics.DrawString(this.Text, base.Font,
new SolidBrush(base.ForeColor),
textX + 1, textY + 1);
break;
}
My Other Control Project
Feedbacks
Please vote for this article.
And email me or leave your messages if you have:
- bug reports
- code improvements
- any comments or suggestions.