Click here to Skip to main content
6,629,885 members and growing! (21,978 online)
Email Password   helpLost your password?
Desktop Development » Button Controls » General     Intermediate

Custom Button with Color and Shape

By Alan Zhao

Another simple custom button control with color and shape.
C#.NET 1.0, .NET 1.1, Win2K, WinXP, Win2003, Dev
Posted:28 Apr 2004
Views:125,412
Bookmarked:58 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
25 votes for this article.
Popularity: 4.92 Rating: 3.52 out of 5
4 votes, 16.0%
1
3 votes, 12.0%
2
4 votes, 16.0%
3
8 votes, 32.0%
4
6 votes, 24.0%
5

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.

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

About the Author

Alan Zhao


Member
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.


Occupation: Web Developer
Company: www.LightspeedSigns.com
Location: United States United States

Other popular Button Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 19 of 19 (Total in Forum: 19) (Refresh)FirstPrevNext
GeneralEmbedding in another project PinmemberTony Reynolds22:40 20 Jul '08  
GeneralRe: Embedding in another project PinmemberTony Reynolds23:46 21 Jul '08  
GeneralSlow click event handling? [modified] Pinmemberscoroop0:11 13 Nov '07  
GeneralRe: Slow click event handling? Pinmembervoiceofthemany3:39 15 Oct '09  
GeneralUsuage Pinmembersamsmithnz15:24 26 May '07  
GeneralRe: Usuage PinmemberAlan Zhao8:01 29 May '07  
GeneralRe: Usuage PinmemberSunshine Always1:06 11 Sep '07  
QuestionSmall Problem [modified] Pinmembersumit siddheshwar19:54 8 Aug '06  
Generalneed Help to Novice Pinmemberavantaram12:01 4 Aug '06  
GeneralVB.Net? Pinmemberam_binu0:36 18 Jul '06  
GeneralNice Component PinmemberMahesh Sapre23:54 1 Jun '06  
GeneralDefaulting to Rectangle Pinmemberhoule9:14 13 Dec '05  
GeneralRe: Defaulting to Rectangle Pinmemberhoule9:22 13 Dec '05  
GeneralMissing file : ColorButtonDesigner? Pinmemberchinkuanyeh7:04 19 Dec '04  
GeneralHi Alan Pinmemberjjason_1815:54 14 Jul '04  
GeneralBack color? PinmemberKenneta12:04 27 May '04  
GeneralRe: Back color? PinsussTal pasi16:50 9 Sep '05  
GeneralShortcut keys? PinmemberJohn N21:20 18 May '04  
Generalslight issue with hover colors not releasing PinmemberMartyK20072:25 10 Sep '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 28 Apr 2004
Editor: Smitha Vijayan
Copyright 2004 by Alan Zhao
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project