Solved

Drawing a round-rectangle shaped button

Posted on 1998-05-18
12
4,239 Views
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!
0
Comment
Question by:Murali Devarakonda
[X]
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
12 Comments
 
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.
0
 
LVL 10

Expert Comment

by:RONSLOW
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.

0
 
LVL 1

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

0
Industry Leaders: 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!

 
LVL 1

Author Comment

by:Murali Devarakonda
ID: 1312474
RONSLOW,

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

Thanx!
0
 
LVL 10

Expert Comment

by:RONSLOW
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).
...

0
 
LVL 10

Expert Comment

by:RONSLOW
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
      rect.DeflateRect(1,1);
      // setup roundness
      CPoint roundness;
      roundness.x = roundness.y = rect.Height()/2;
      // draw shadow first
      if (state & ODS_SELECTED) {
            // draw shadow at topleft
            rect.OffsetRect(0,0);
            pDC->RoundRect(rect,roundness);
            rect.OffsetRect(1,1);
      } else {
            // draw shadow at bottom right
            rect.OffsetRect(1,1);
            pDC->RoundRect(rect,roundness);
            rect.OffsetRect(-1,-1);
      }
      // now draw the button over the top of the shadow
      pDC->SelectObject(&facebrush);
      pDC->SelectObject(&edgepen);
      pDC->RoundRect(rect,roundness);
      pDC->SelectObject(pOldPen);
      pDC->SelectObject(pOldBrush);
      // shrink interior so content doesn't leak
      rect.DeflateRect(1,1);
#else
      // set up flags for button style
      DWORD flags = DFCS_ADJUSTRECT|DFCS_BUTTONPUSH;
      if (state & ODS_SELECTED) flags |= DFCS_PUSHED;
      if (state & ODS_DISABLED) flags |= DFCS_INACTIVE;
      // draw the button
      pDC->DrawFrameControl(rect,DFC_BUTTON,flags);
      // shrink intereior
      rect.DeflateRect(1,1);
      // offset down and right when pressed
      if (state & ODS_SELECTED) rect.OffsetRect(1,1);
#endif
}

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;
      pDC->DrawState(
            CPoint(x,y), rect.Size(), text,
            (state & ODS_DISABLED?DSS_DISABLED:DSS_NORMAL), true, 0, (HBRUSH)NULL
            );
      pDC->SetTextColor(oldTextColor);
      pDC->SetBkMode(nBkModeOld);
}


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

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);
      ASSERT_VALID(pDC);
      CRect rectClient = lpDIS->rcItem;
      Draw(pDC,rectClient,lpDIS->itemState);
}

0
 
LVL 10

Expert Comment

by:RONSLOW
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.

0
 
LVL 1

Author Comment

by:Murali Devarakonda
ID: 1312478
RONSLOW,

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.

Thanks
0
 
LVL 1

Author Comment

by:Murali Devarakonda
ID: 1312479
RONSLOW,

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

Thui,

I appreciate your response, but the system doesn't allow me to divide the points. Thanks for your help.
0
 
LVL 10

Expert Comment

by:RONSLOW
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.

0
 
LVL 7

Expert Comment

by:linda101698
ID: 1312481
RONSLOW,

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

Accepted Solution

by:
RONSLOW earned 100 total points
ID: 1312482
Thank you

0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Suggested Courses

623 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