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

Drawing a round-rectangle shaped button

Posted on 1998-05-18
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
LVL 15

Expert Comment

by:Tommy Hui
ID: 1312471
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.
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...

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.


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 Murali-Krishna@Cheerful.com

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()-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;

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


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 100 total points
ID: 1312482
Thank you


Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

790 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