Click here to Skip to main content
Licence CPOL
First Posted 26 Nov 1999
Views 314,343
Bookmarked 102 times

Round Buttons

By | 11 Jun 2003 | Article
A class that turns rectangular buttons into round buttons.

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
The Code Project
Canada Canada

Member

Follow on Twitter Follow on Twitter
Google+
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. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralExcellent article! PinmemberDrABELL12:39 16 Sep '09  
GeneralA nice article PinmemberDerek Bartram10:53 22 Mar '08  
Questionhow to implement this class to see the buttons Pinmembertoutounesan_bg11:12 14 Aug '07  
QuestionHow to add button dynamic in VB6.0? Pinmembermyphuong24419:32 2 Sep '05  
GeneralSome explanations,please... PinmemberDudi Avramov4:49 12 Dec '04  
GeneralRe: Some explanations,please... PinmemberDudi Avramov0:14 13 Dec '04  
QuestionCan anyone show me some good ebook about design control Pinmemberhoangbao2:58 6 Nov '04  
QuestionHow to insert this in view class Pinmembermymauve211:10 19 Mar '04  
AnswerRe: How to insert this in view class Pinmembermaruku18:58 8 Nov '04  
AnswerRe: How to insert this in view class Pinmembertoucherzxb20:09 25 Nov '07  
QuestionUnderline the shortcut character? PinmemberAAntix10:15 7 Nov '03  
Questionhow to create command button through code PinsussAnonymous4:30 30 Sep '03  
QuestionHow To Create a Round Shape Control ? PinmemberVikrant Vikrant21:58 29 Jun '03  
GeneralNot the most important thing in the world Pinmembertkalmijn21:48 9 Jun '03  
GeneralRe: Not the most important thing in the world PinadminChris Maunder4:57 10 Jun '03  
GeneralWhere can we declare Pinmemberaimenkawaz11:53 5 Oct '02  
GeneralRound Button Java PinmemberEric Minor5:38 26 Jun '02  
GeneralRe: Round Button Java PinsussSebuliba Peter19:48 1 Jan '03  
Question#How to implement a round button in C#? PinmemberAnonymous6:42 24 Jun '02  
GeneralKewl... PinmemberHockeyDude14:35 2 Apr '02  
GeneralRGN qustion Pinmemberkuzi5:53 6 Mar '02  
GeneralThe size of the circle PinmemberHuda4:34 2 Sep '01  
GeneralCool but... PinmemberEarl Rex Arao-arao23:11 10 May '01  
QuestionHow to add a bitmap to the round buttons? PinsussBernd Giesen22:52 8 Jun '00  
Questionreal round button ?!? Pinsusstuvok20:55 18 Mar '00  

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

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

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