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
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
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
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) +
BYTE Green = (BYTE) (Weight*GetGValue(crBright) +
BYTE Blue = (BYTE) (Weight*GetBValue(crBright) +
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!
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
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.