Click here to Skip to main content
Click here to Skip to main content

Round Buttons

By , 11 Jun 2003
 

Sample Image

Introduction

I wanted a button that looked exactly like normal buttons, but instead I wanted them circular. This class can be used like any other owner drawn control - simply include the header file, and declare your button controls as CRoundButton instead of CButton

First of all I make sure the buttons are circles (and not ellipses) and store the centre and radius of the button. Next I simply make the button owner drawn and draw it like every other owner drwn button, but instead of being able to use nice routines like Draw3dRect, I had to roll my own circle drawing routine which would draw each pixel with the correct colour dependant on the point on the circle I was drawing.

I will not include the full source in this page - it is available for download here. The owner draw part is simple and follows along the lines of any other owner drawn button. The circle drawing routine is a standard algorithm, with the only modification in calculating the pixel colour. Given two colours crBright and crDark, and an angle relative to the x-axis, the colour for a pixel can be calculated using the following.

COLORREF GetColour(double dAngle, COLORREF crBright, COLORREF crDark)
{
#define Rad2Deg            180.0/3.1415 
#define LIGHT_SOURCE_ANGLE  -2.356    // -2.356 radians = -135 degrees, 
                                      // i.e. From the top left of the screen

    ASSERT(dAngle > -3.1416 && dAngle < 3.1416);
    double dAngleDifference = LIGHT_SOURCE_ANGLE - dAngle;

    if (dAngleDifference < -3.1415) 
        dAngleDifference = 6.293 + dAngleDifference;
    else if (dAngleDifference > 3.1415) 
        dAngleDifference = 6.293 - dAngleDifference;

    double Weight = 0.5*(cos(dAngleDifference)+1.0);

    BYTE Red   = (BYTE) (Weight*GetRValue(crBright) + 
                        (1.0-Weight)*GetRValue(crDark));
    BYTE Green = (BYTE) (Weight*GetGValue(crBright) + 
                        (1.0-Weight)*GetGValue(crDark));
    BYTE Blue  = (BYTE) (Weight*GetBValue(crBright) + 
                        (1.0-Weight)*GetBValue(crDark));

    return RGB(Red, Green, Blue);
}

This is a simple linear interpolation between the two colours based on the cosine of the angle between the light source and the point. Angles are measured from the +ve x-axis (i.e. (1,0) = 0 degrees, (0,1) = 90 degrees ), but remember: positive y points down!

Update

Tom Kalmijn kindly added routines that post-process the button image to smooth out the jagged edges. His method uses a nearest-neighbours algorithm to interpolate missing pixels. It's not particularly fast but it does increase smoothing.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Chris Maunder
Founder CodeProject
Canada Canada
Member
Chris is the Co-founder, Administrator, Architect, Chief Editor and Shameless Hack who wrote and runs The Code Project. He's been programming since 1988 while pretending to be, in various guises, an astrophysicist, mathematician, physicist, hydrologist, geomorphologist, defence intelligence researcher and then, when all that got a bit rough on the nerves, a web developer. He is a Microsoft Visual C++ MVP both globally and for Canada locally.
 
His programming experience includes C/C++, C#, SQL, MFC, ASP, ASP.NET, and far, far too much FORTRAN. He has worked on PocketPCs, AIX mainframes, Sun workstations, and a CRAY YMP C90 behemoth but finds notebooks take up less desk space.
 
He dodges, he weaves, and he never gets enough sleep. He is kind to small animals.
 
Chris was born and bred in Australia but splits his time between Toronto and Melbourne, depending on the weather. For relaxation he is into road cycling, snowboarding, rock climbing, and storm chasing.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralExcellent article!memberDrABELL16 Sep '09 - 12:39 
Hi Chris,
 
Very nice article, indeed! Well written and practical.
 
As I was always saying to my students: desktop application could be just as good as its GUI. Actually, it could be much worse, but never better Smile | :)
 
Best regards,
 
Alex
GeneralA nice articlememberDerek Bartram22 Mar '08 - 10:53 
I have implemented a similar functioned button, however designed for office 2007 style interfaces (and WPF, Windows Presentation Foundation).
 
http://www.codeproject.com/KB/WPF/roundbutton.aspx[^]
Questionhow to implement this class to see the buttonsmembertoutounesan_bg14 Aug '07 - 11:12 
Hi,

I don't know how to use this class and to declare it in the aim to use it.
I need it for a project.

 
Please, help me.
QuestionHow to add button dynamic in VB6.0?membermyphuong2442 Sep '05 - 19:32 
I have a database with a table "Deparment" it have tow fields(ID,NAME). In this table have 7 records.
In this form when design I drop a button in toolbox into the form with Name:bt1, and index=0.
I want,when this programming runtime, it read from database(Table Deparment) with 7 records, at time in the form have 7 buttons with caption is field name "NAME" which it created arrays. I have done it but now I don't remember, Help me?
Thank you very much.
GeneralSome explanations,please...memberDudi Avramov12 Dec '04 - 4:49 
Hi Chris,
I've came across with your sample about round buttons, and you did great job.
I need an explanation,please, about the function DrawCircle which uses lError to decrement XOffset.
Could you,please, explain the following code in that function:
 
//Advance the error term and the constant X axis step
lError += lYoffset++;
 
//Check to see if error term has overflowed
if ((lError += lYoffset) >= 0)
lError -= --lXoffset * 2;
 
What is the principle to draw circle without using the circle equation x^2+y^2=r^2?
 
Thanks,
Dudi

GeneralRe: Some explanations,please...memberDudi Avramov13 Dec '04 - 0:14 
Did you use Bresenham algorithm?
QuestionCan anyone show me some good ebook about design controlmemberhoangbao6 Nov '04 - 2:58 
Dear everyone , I'm studying VC++ MFC , but now i'm on basic level , i'm very interested in customed control . i'd like to know more about this aspect , to understand the code that some experts wrote here , and will be able to write some .
Thanks a lot .

 
Hoang Bao
QuestionHow to insert this in view classmembermymauve2119 Mar '04 - 1:10 
I tried to draw this button in my project directly inserting it in the view but it gives assertion error .

commenting the following line obviously removes this error but the button drawn is not circular one infact it draws only a grey rectangle.
 
ASSERT(dAngle > -3.1416 && dAngle < 3.1416);
 
What am i missing ???
 

AnswerRe: How to insert this in view classmembermaruku8 Nov '04 - 18:58 
I have this same problem as well. GetClientRect in presubclass window returns a CRect with no dimensions. Have you found a solution since March? Smile | :)
AnswerRe: How to insert this in view classmembertoucherzxb25 Nov '07 - 20:09 
I have the same question when I want to add the Buttons dynamic,the error occurs when ASSERT(dAngle > -3.1416 && dAngle < 3.1416);
somebody help me!Smile | :)

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 12 Jun 2003
Article Copyright 1999 by Chris Maunder
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid