ArticleDirectory
This article from http://www.codeproject.com/KB/buttons/RoundButton_csharp.aspx
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
Introduction
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.
Overview
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
Textstartpoint
Point 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 wordButton
ByRoundbutton
InInitializecomponents ()
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 # IsControl
Class. 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 extendButton
Class. To create a ListBox having checkboxes and showing images instead of text, you wowould extendCheckedlistbox
Class. 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 extendUsercontrol
Class. You can create a composite control also usingUsercontrol
.
Basic Philosophy
The basic philosophy to create a round button is simple. ExtendButton
Class, havePaint
Handler, draw your ellipse in the handler. There are other intricacies, however. If you are having your ownPaint
Handler, you will not be calling the defaultPaint
Handler, which means more responsibilities:
- 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.
- You wowould affect other properties related to drawing like: text, image and backgroundimage. If you are not calling the default
Paint
Handler, 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 defaultPaint
Handler. 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
��Colorbutton4
Modified to take in pen and brush arguments. Needed for hover-coloring. I added handlersMouseenter
AndMouseleave
To 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. Button
ToAdvbutton. roundbutton
.
Known issues
These are known issues:
- It disregards
Textalign
Property
- It disregards
Imagealign
Property
- Does not give a perfect 3D look, consequently, the property
Flatstyle
Is 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 propertiesImagealign
AndTextalign
Are currently ignored. The image is currently drawn at the center and text coordinates depends on the propertyTextstartpoint
.
References
License
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