Click here to Skip to main content
15,880,725 members
Articles / Programming Languages / C#
Article

Custom Button with Color and Shape

Rate me:
Please Sign up or sign in to vote.
3.25/5 (32 votes)
28 Apr 2004 262.1K   9.7K   80   23
Another simple custom button control with color and shape.

Image 1

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

C#
//
// 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

C#
//
// mode declaration
//
LinearGradientMode mode;
C#
//
// 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

C#
//
// brush declaration
//
LinearGradientBrush brush;
C#
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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer www.LightspeedSigns.com
United States United States
Hi, my name is Alan Zhao. I live in Glens Falls, NY. Say hi to me if you see me one day.

also visit me at www.LinkGone.com - Make your long URLs easy to read, remember and reuse.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Nathan Ferreira22-Aug-14 4:32
Nathan Ferreira22-Aug-14 4:32 
GeneralKeep up with good job man Pin
Nipuna S Perera6-Aug-14 0:00
professionalNipuna S Perera6-Aug-14 0:00 
QuestionHere is something interesting regarding shaped buttons Pin
murathankocan3-Feb-13 22:38
murathankocan3-Feb-13 22:38 
GeneralMy vote of 1 Pin
Long Nguyen (xyz)7-Aug-12 20:18
Long Nguyen (xyz)7-Aug-12 20:18 
GeneralEmbedding in another project Pin
Tony Reynolds20-Jul-08 21:40
Tony Reynolds20-Jul-08 21:40 
GeneralRe: Embedding in another project Pin
Tony Reynolds21-Jul-08 22:46
Tony Reynolds21-Jul-08 22:46 
GeneralSlow click event handling? [modified] Pin
scoroop12-Nov-07 23:11
scoroop12-Nov-07 23:11 
GeneralRe: Slow click event handling? Pin
voiceofthemany15-Oct-09 2:39
voiceofthemany15-Oct-09 2:39 
GeneralUsuage Pin
samsmithnz26-May-07 14:24
samsmithnz26-May-07 14:24 
GeneralRe: Usuage Pin
Alan Zhao29-May-07 7:01
Alan Zhao29-May-07 7:01 
GeneralRe: Usuage Pin
Sunshine Always11-Sep-07 0:06
Sunshine Always11-Sep-07 0:06 
QuestionSmall Problem [modified] Pin
sumit siddheshwar8-Aug-06 18:54
sumit siddheshwar8-Aug-06 18:54 
Generalneed Help to Novice Pin
avantaram4-Aug-06 11:01
avantaram4-Aug-06 11:01 
QuestionVB.Net? Pin
am_binu17-Jul-06 23:36
am_binu17-Jul-06 23:36 
GeneralNice Component Pin
Mahesh Sapre1-Jun-06 22:54
Mahesh Sapre1-Jun-06 22:54 
GeneralDefaulting to Rectangle Pin
tp_h13-Dec-05 8:14
tp_h13-Dec-05 8:14 
GeneralRe: Defaulting to Rectangle Pin
tp_h13-Dec-05 8:22
tp_h13-Dec-05 8:22 
QuestionMissing file : ColorButtonDesigner? Pin
chinkuanyeh19-Dec-04 6:04
chinkuanyeh19-Dec-04 6:04 
GeneralHi Alan Pin
jjason_1814-Jul-04 14:54
jjason_1814-Jul-04 14:54 
QuestionBack color? Pin
Kenneta27-May-04 11:04
Kenneta27-May-04 11:04 
AnswerRe: Back color? Pin
Tal Pasi9-Sep-05 15:50
Tal Pasi9-Sep-05 15:50 
QuestionShortcut keys? Pin
John N18-May-04 20:20
John N18-May-04 20:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.