Round button in C #

Source: Internet
Author: User
    • Step 1
    • Step 2
    • Step 3

This article from


This sample code uses strates how to create a custom control, specifically a round button in C # With Advanced color effects.


    • Download source files-7 KB
    • Download Demo project-8 KB


The button presented in this example has been developed in stages. I shall walk you through the stages one by one, so that you can create a custom control yourself easily. round Buttons are specific cases of elliptic buttons.


My motivation for this article started with me trying to imitate the round button of Windows Media Player. I wanted to create a custom control in C # And round button is a control developers popularly try to implement when implementing custom controls. I searched codeproject and Google, there was none, and I decided to create one myself.

Custom Controls are easier to create in C # than in MFC, thanks to FCL. there was an article by Chris Maunder on how to create an elliptic button Using MFC. the code in this sample looks simpler because of some FCL functions that come free.

What's special about this button

The button I developed has the following properties:

    • It is elliptical/round
    • Hovercolor�� Color of the button when you hover over it
    • TextstartpointPoint at which we start drawing Text of the button. Coordinates are relative to upper-hand left corner of the bounding rectangle of the button.
    • Colorgradient�� This parameter allows you to shade the button. indicates how sharp a color transition you want.
    • Colorstepgradient�� Indicates how many every pixels you want color change.
    • Fadeout�� Indicates if you want darker color outside or inside. indicate true for darker color along the rim, and false for lighter.
Why shocould you use round buttons?

Why wocould you create a round button at all? First, if you want nice UI. second, if a regular rectangular button is not as good as one, for example, that looks like a cigar. windows Media Player �s play button mimics today �s round buttons on multimedia player stations and conveys the idea more accurately than a rectangular one.

It is easier to create a round button in C #, And You wowould like to use. net to create it if you want it to seamlessly integrate with other UI development environments like VB. net. in order to use it, you d need an operating system with. NET framework installed. in order to replace buttons in existing projects with this new one, you want to replace the wordButtonByRoundbuttonInInitializecomponents ()Function in your forms and add a reference to the DLL hosting code for this button.

C # Background

The mother of all controls in C # IsControlClass. it has all the features that you need want out of a control and msdn describes the features well. if you are authoring a new control and your control is going to be similar to a standard existing control, you will do well to extend that class. in our example, we extendButtonClass. To create a ListBox having checkboxes and showing images instead of text, you wowould extendCheckedlistboxClass. If you want to create a control from scratch that has no behavioral reseance ance to any controls that you know of, you want to extendUsercontrolClass. You can create a composite control also usingUsercontrol.

Basic Philosophy

The basic philosophy to create a round button is simple. ExtendButtonClass, havePaintHandler, draw your ellipse in the handler. There are other intricacies, however. If you are having your ownPaintHandler, you will not be calling the defaultPaintHandler, which means more responsibilities:

    1. You will have to set the region for the button so that if the user clicks at a point which lies outside the bounding circle but inside the bounding rectangle, that does not translate to a click.
    2. You wowould affect other properties related to drawing like: text, image and backgroundimage. If you are not calling the defaultPaintHandler, you have to draw the text and images, aligned as the user wants them.
La code

Time to code. In the following, I describe briefly the steps in which I developed this button.

Step 1

Simple round button. events als done. behaviorally similar to a rectangular button. we create one pen and one brush. the pen, to draw the boundary ellipse/circle; the brush: To fill interior of the ellipse.

Collapse | Copy code
Constructor: {_ pen = New Pen (_ color); _ brush = New Solidbrush (color. fromknowncolor (knowncolor. Control); _ brushinside = New Solidbrush (_ color );} //   Onpaint�� Protected   Override   Void   Onpaint (Painteventargs PE) {graphics G = PE. graphics; G. fillrectangle (_ brush, 0 , 0 , Clientsize. Width, clientsize. Height); G. drawellipse (_ pen, 0 , 0 , Clientsize. Width, clientsize. Height); G. fillellipse (_ brushinside, 0 , 0 , Clientsize. Width, clientsize. Height );}

Additionally, you want to expose a color property that the end user can populate in design mode.

Step 2

The implementation in step 1 has a bug: If you click at a point which lies outside the bounding circle but inside the bounding rectangle, that gets interpreted as a click.

To fix that, we add the following code inOnpaint To set the window region correctly:

Collapse | Copy code
Graphicspath Path =NewGraphicspath (); Path. addellipse (0,0, Clientsize. Width, clientsize. Height );This. Region =NewRegion (PATH );
Step 3

Rest of the Code shocould be easy to interpret. I have developed functions, step by step, to make the coloring code more appealing and fixing problems because we are avoiding the call to the defaultPaintHandler. I called the function where I do the painting inside the button inOnpaint()AsColorbutton, And developed different versionsColorbutton ().

Brief overview of the functions:

    • Colorbutton1�� Flat coloring of the button
    • Colorbutton2�� Fills color with color gradient. Color gets darker towards the righthand-bottom corner.
    • Colorbutton3-Fills color with color gradient. Color gets darker towards the center. Respects the Image property set by the user
    • Colorbutton4��Adds fade-in/out property. Color gets lighter (fade out) or darker (fade in) towards the center.
    • Colorbutton5��Colorbutton4Modified to take in pen and brush arguments. Needed for hover-coloring. I added handlersMouseenterAndMouseleaveTo color the button differently on mouse hover. draws a focus rectangle when the button has focus.
Using the button

In a fresh project, go to the toolbox, Add/Remove items, browse and point to the DLL for the button. In an existing project, change textSystems. Windows. Forms. ButtonToAdvbutton. roundbutton.

Known issues

These are known issues:

    • It disregardsTextalignProperty
    • It disregardsImagealignProperty
    • Does not give a perfect 3D look, consequently, the propertyFlatstyleIs ignored
Other considerations

Note that we have taken care:

    • Windows accessibility requirements
    • Localization issues. text written in a right-to-left language will appear fine.
Future improvements

The propertiesImagealignAndTextalignAre currently ignored. The image is currently drawn at the center and text coordinates depends on the propertyTextstartpoint.

    • The round button-, by Chris Maunder


This article has no explicit license attached to it but may contain in 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.