Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Drawing a round-rectangle shaped button

Posted on 1998-05-18
Medium Priority
Last Modified: 2013-11-20
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!
Question by:Murali Devarakonda
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 15

Expert Comment

by:Tommy Hui
ID: 1312471
Check out the buttons section at There are classes to create circular buttons and triangular buttons, adapting those classes to rounded rectangles should be easy.
LVL 10

Expert Comment

ID: 1312472
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.


Author Comment

by:Murali Devarakonda
ID: 1312473
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...

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

by:Murali Devarakonda
ID: 1312474

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

LVL 10

Expert Comment

ID: 1312475
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).

LVL 10

Expert Comment

ID: 1312476
#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();
      int y =;
            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;

LVL 10

Expert Comment

ID: 1312477
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.


Author Comment

by:Murali Devarakonda
ID: 1312478

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.


Author Comment

by:Murali Devarakonda
ID: 1312479

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.
LVL 10

Expert Comment

ID: 1312480
You can ask Linda to do a point split for you.  Send her mail at (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.


Expert Comment

ID: 1312481

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
LVL 10

Accepted Solution

RONSLOW earned 300 total points
ID: 1312482
Thank you


Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question