Drawing a round-rectangle shaped button

I have a DialogBar on the frame of a Doc/View app. I'm using Visual C++ 5.0. I have a few buttons that I wish to draw in a round-rectangle shape, something like:

but obviously cleaner.
I'm relatively new to MFC GUI programming but I have been looking around, and I suppose the answer lies in one/some/a-combination of these keywords: owner-draw, MeasureItem, PresubClassWindow, DrawItem, CRgn::CreateRoundRectRgn...

Also, I'm not sure I understand the last two parameters to CRgn::CreateRoundRectRgn- can you explain?

Please be as specific as possible in your answer, as I have spent a considerable time on this problem. A code snippet would definitely be preferable. A sample would be great.

Thanx a lot!
Murali Devarakonda4WeekStartup.guruAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tommy HuiEngineerCommented:
Check out the buttons section at http://www.codeguru.com. There are classes to create circular buttons and triangular buttons, adapting those classes to rounded rectangles should be easy.
Owner drawn buttons are fairly simple.  Rectangular ones are easiest becaure you can use DrawFrameControl to draw them.

For other straight-edged shapes, you can use DrawEdge.

For a round rectangle, you need to do more of the drawing yourself (using RoundRect) after setting the pen and brush colours.  If you want a 3D raised look (like standard button), then even that won't help and you'll need to draw the individual parts of the round rect in different colours (arcs and lines).  If you want an Apple-look (I think they have round rects) then the round rect call would beok .. maybe with another round rect drawn first in black for a shadow.

BTW: The test of the button is drawn using DrawState.

If you want some code for doing owner drawn buttons, let me know.

Murali Devarakonda4WeekStartup.guruAuthor Commented:
Thank you for your response. Even before I posted my question, I had already looked at the CodeGuru samples (hence my reference to "PresubClassWindow, DrawItem, CRgn::CreateRoundRectRgn..." etc.

It's not obvious from the samples - at least not for someone new to Advanced GUI stuff in MFC- how I can reshape a rectangular button to round-rect. There seems to be an awful lot of code required (with a thorough understanding of the concepts behind it).

I expected it to be a straighforward task, if not trivial.
If that's not the case, I must say this is another reason MFC sucks because it's not very well designed, and not truly object-oriented. Why couldn't the CButton class (or it's derived) have methods to resize, reshape, etc.? Just wondering...

Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Murali Devarakonda4WeekStartup.guruAuthor Commented:

I already have a standard rectangular button on my DialogBar. All I want to do is to give it semi-circular edges, keeping all the other properties intact. Of course, I'll be painting a bitmap onto it, which works so far ;-)

If you have a sample, please send it to Murali-Krishna@Cheerful.com

Windows supports some standard button shapes .. a rectangular push-button, a check box, a option(radio) button.  Other non-standard shapes are drawn by overriding the DrawItem function, in which case you can draw them anyway you want.  That seems fairly flexible to me.

Once you've done an ownerdrawn button once (or borrowed someone elses ownerdrawn button code) then you just change it to draw as you want using any of the good old GDI drawing functions.  GDI includes DrawFrameControl, DrawState, DrawEdge, Draw3DRect and DrawFocusRect to help you.

Here is a DrawItem override that should do what you want (roughly .. I haven't compiled it .. treat it as pseudo-code if you like).

#define ROUND

void CRoundRectButton::DrawBorders(CDC* pDC, CRect& rect,UINT state) {
#ifdef ROUND
      // black border, white inside
      CPen edgepen(PS_SOLID,0,RGB(0,0,0));
      CPen shadowpen(PS_SOLID,0,RGB(128,128,128));
      CPen facebrush(RGB(255,255,255));
      CPen shadowbrush(RGB(128,128,128));
      CPen* pOldPen = pDC->SelectObject(&shadowpen);
      CBrush* pOldBrush = pDC->SelectObject(&shadowbrush);
      // allow for shadow
      // setup roundness
      CPoint roundness;
      roundness.x = roundness.y = rect.Height()/2;
      // draw shadow first
      if (state & ODS_SELECTED) {
            // draw shadow at topleft
      } else {
            // draw shadow at bottom right
      // now draw the button over the top of the shadow
      // shrink interior so content doesn't leak
      // set up flags for button style
      if (state & ODS_SELECTED) flags |= DFCS_PUSHED;
      if (state & ODS_DISABLED) flags |= DFCS_INACTIVE;
      // draw the button
      // shrink intereior
      // offset down and right when pressed
      if (state & ODS_SELECTED) rect.OffsetRect(1,1);

void CRoundRectButton::DrawText(CDC* pDC, const CRect& rect, LPCTSTR text, UINT state) {
      // Draw the text transparently
      int nBkModeOld = pDC->SetBkMode(TRANSPARENT);
      COLORREF textcol = ::GetSysColor(COLOR_BTNTEXT);
      COLORREF oldTextColor = pDC->SetTextColor(textcol);
      // ensure it is centred in the button rect
      CSize textSizeClient = pDC->GetTextExtent(text,strlen(text));
      int x = rect.left+(rect.Width()-textSizeClient.cx)/2;
      int y = rect.top+(rect.Height()-textSizeClient.cy)/2;
            CPoint(x,y), rect.Size(), text,

void CRoundRectButton::Draw(CDC* pDC, const CRect& inrect, UINT state) {
      CRect rect = inrect;
      CPoint textpoint = rect.TopLeft();
      CSize  textsize =  rect.Size();
      CRect textrect(textpoint,textsize);

void CRoundRectButton::PreSubclassWindow() {
      // ensure we are owner drawn
      SetButtonStyle(GetButtonStyle() | BS_OWNERDRAW);

void CRoundRectButton::DrawItem(LPDRAWITEMSTRUCT lpDIS) {
      // call Draw to do the work
      CDC* pDC = CDC::FromHandle(lpDIS->hDC);
      CRect rectClient = lpDIS->rcItem;

Change the DrawBorder function to draw the button differently (eg, change colours, roundness etc)

Also you can change the DrawText function so it uses DrawState to draw your bitmap (an icon is better though).

I also have code for drawing icons and/or text on a button, if you want.

Murali Devarakonda4WeekStartup.guruAuthor Commented:

Thanks for the sample. It sure looks like something I can start with. And yes, I would love to have ANY samples involving owner-draw buttons, bitmaps, icons, whatever.

Especially, "code for drawing icons and/or text on a button", although I've been able to draw a simple bitmap (it doesn't scale to the button size though) on a button. I suppose I need to do BitBlt or StretchBlt. I've re-read Prosise's Chapters (2,5)on Drawing, and Controls. Also, chapter 12: on Bitmaps and Regions. It's fun, but a whole lot of stuff to digest before I KNOW how to do this- or at least in the best way possible.

Murali Devarakonda4WeekStartup.guruAuthor Commented:

I think you need to lock this question again yourself (I've unlocked it) for me to give you any points.


I appreciate your response, but the system doesn't allow me to divide the points. Thanks for your help.
You can ask Linda to do a point split for you.  Send her mail at Linda@experts-exchange.com (make sure you tell here the Q.xxxx number) and/or post a message in the Experts Exchange Customer Service topic area.

If you don't have any response in a day or two, then I'll come back here and answer.  But it would be nice if you could share the points appropriately.

I've dug up an (old) version of my code for icon and/or text buttons and send it to you.  Pelase see your inbox.


Please post an answer so you can receive points for all the work you've done.  I'll post another question to give thui the points MuraliKrishna would like him to receive.

Linda Gardner
Customer Service @ Experts Exchange
Thank you


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.