Click here to Skip to main content
6,597,576 members and growing! (19,031 online)
Email Password   helpLost your password?
Multimedia » GDI+ » General     Intermediate

GDI+ RoundedRect

By Christian Graus

Providing a RoundedRect function for GDI+
VC6, VC7, Windows, GDI+, Dev
Posted:15 Feb 2002
Views:62,577
Bookmarked:17 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
17 votes for this article.
Popularity: 3.98 Rating: 3.23 out of 5
1 vote, 12.5%
1
1 vote, 12.5%
2
2 votes, 25.0%
3
1 vote, 12.5%
4
3 votes, 37.5%
5

Sample Image - Screenshot.jpg

Introduction

Although GDI+ is a very exciting extension to GDI, for some reason, the RoundedRect function is not provided. Although it is obviously possible to use GDI functions with GDI+, this does not give us the ability to use new features such as texturing and alpha transparency with our rounded rectangles. Luckily, GDI+ offers the GraphicsPath class, which we can use to create any shape and then draw using any brush. I've provided a function which can be added to any project, and used as shown to draw a rounded rect, and provided a demo application that allows setting the rounding percentage via a slider. It's quick and dirty ( it's late here ), but it is adequate to show off what the function does. The function is as follows:

GraphicsPath* MakeRoundRect(Point topLeft, Point bottomRight, INT percentageRounded)
{
          ASSERT (percentageRounded >= 1 && percentageRounded <= 100);

          INT left  = min(topLeft.X, bottomRight.X);
          INT right = max(topLeft.X, bottomRight.X);

          INT top    = min(topLeft.Y, bottomRight.Y);
          INT bottom = max(topLeft.Y, bottomRight.Y);

          INT offsetX = (right-left)*percentageRounded/100; 
          INT offsetY = (bottom-top)*percentageRounded/100;

          GraphicsPath pt;
          GraphicsPath * path = pt.Clone();

          path->AddArc(right-offsetX, top, offsetX, offsetY, 270, 90);

          path->AddArc(right-offsetX, bottom-offsetY, offsetX, offsetY, 0, 90);

          path->AddArc(left, bottom - offsetY, offsetX, offsetY, 90, 90);

          path->AddArc(left, top, offsetX, offsetY, 180, 90);

          path->AddLine(left + offsetX, top, right - offsetX/2, top);

          return path;
}

The interesting thing to note is that since I first wrote this code, the ability to call new for GraphicsPath has been removed in some cunning manner ( try it and see ). Instead I must make a local object, and call it's 'Clone' method. It's entirely possible I am just missing something at this late hour, but it seems in line with other classes in GDI+, that you need to create new objects via pointers using GDI+ methods instead of new, and I presume this is because the other .NET languages do not offer pointers.

That's about all there is to it. This code has one flaw - it is poor design that the function creates an object, and the caller must delete it. However, GDI+ kindly does not allow us to return the path as an object, only as a pointer. I'm guessing this has something to do with GDI+ returning every class as a reference, but I am not sure. For some fun, I recommend you play with other things you can add to this path – the Warp method looks like it could especially be a lot of fun.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Christian Graus


Member
Programming computers ( self taught ) since about 1984 when I bought my first Apple ][. Was working on a GUI library to interface Win32 to Python, and writing graphics filters in my spare time, and then building n-tiered apps using asp, atl and asp.net in my job at Dytech. After 4 years there, I've started working from home, at first for Code Project and now for a vet telemedicine company. I owned part of a company that sells client education software in the vet market, but we sold that and now I work for the new owners.
Occupation: Software Developer (Senior)
Location: Australia Australia

Other popular GDI+ articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 10 of 10 (Total in Forum: 10) (Refresh)FirstPrevNext
GeneralSmall fix PinmemberTonyTop11:17 1 Apr '05  
Generalconfining image to area... Pinmembermliss19:03 20 Sep '03  
GeneralHow to use "new" keyword in VC++6.0 PinsussEnde20:05 27 Oct '02  
GeneralRe: How to use "new" keyword in VC++6.0 PinmemberChristian Graus20:09 27 Oct '02  
GeneralRe: How to use "new" keyword in VC++6.0 Pinmemberende621:31 27 Oct '02  
GeneralRe: How to use "new" keyword in VC++6.0 PinmemberDavide Calabro5:29 12 Jun '03  
GeneralLast Line Pokes Out PinmemberSwinefeaster23:14 2 Aug '02  
GeneralAvoid returning a pointer PinmemberSwinefeaster2:26 2 Aug '02  
GeneralRe: Avoid returning a pointer PinmemberChristian Graus2:43 2 Aug '02  
GeneralRe: Avoid returning a pointer PinmemberSwinefeaster2:46 2 Aug '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 15 Feb 2002
Editor: Chris Maunder
Copyright 2002 by Christian Graus
Everything else Copyright © CodeProject, 1999-2009
Web16 | Advertise on the Code Project