|

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.
| You must Sign In to use this message board. |
|
| | Msgs 1 to 10 of 10 (Total in Forum: 10) (Refresh) | FirstPrevNext |
|
 |
|
|
To make the code suitable to any type of rectangle, replace
INT offsetX = (right-left)*percentageRounded/100; INT offsetY = (bottom-top)*percentageRounded/100;
with
INT minDef = min((right-left), (bottom-top));
INT offsetX = minDef*percentageRounded/100; INT offsetY = minDef*percentageRounded/100;
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Nice article and very common sense approach to solving problem.
However, I notice in your article the image is tiled ... Is there a way that you can achieve the rounded rect effect and without having an image that is smaller than the GraphicsPath tiled or having an image that is larger than the GraphicsPath chopped to the right or left ?
Thanks for all of your GDI plus submissions...
Mike
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
For Example Bitmap* bitmap = new Bitmap(nWidth,nHeight);
error C2660: 'new' : function does not take 3 parameters
Pen class has same error, but code is right.
I updated platform SDK(08-2001) and I used to GDI+, but I don't know how to use "new" keyword about drawing class(Bitmap, Pen..etc)
please help me..
If you email to me... kurt6@dreamwiz.com
thank you...
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Doesn't work that way, to use GDI+ I've not found a way to create them using 'new'. I believe this is intentional, GDI+ controls the lifetime of objects.
Christian
No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002
During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I solved that problem...
#ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__;
// #define new DEBUG_NEW <--- This line is comment...
#endif
Release mode is OK..but Debug mode occurs problem because of "new" operator. so I typed in comment symbol("//").
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
|
Use ::new
SoftechSoftware Davide Calabro' davide_calabro@yahoo.com http://www.softechsoftware.it
|
| Sign In·View Thread·PermaLink | 5.00/5 (2 votes) |
|
|
|
 |
|
|
I'm surprised no one has caught this! Well I guess you wouldn't see it unless you drew the rounded rect path using a pen using Graphics.DrawPath(). Here's the problem (I dunno if the image will display as it doesn't work in Preview):
<img src = "http://www.aephid.com/posted/rounded-rect-bad-lines.jpg"></img> Click here to see image
The solution is simple. Just change the last line of your function from:
path->AddLine(left + offsetX, top, right - offsetX/2, top);
to
path->CloseFigure();
Cheers,
swine
Check out Aephid Photokeeper, the powerful digital photo album solution at www.aephid.com.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
To avoid having to return a pointer, one could rewrite your function as:
void MakeRoundRect(Point topLeft, Point bottomRight, INT percentageRounded, GraphicsPath& path) { 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;
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); }
Check out Aephid Photokeeper, the powerful digital photo album solution at www.aephid.com.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
It's funny, the email I got was all screwed up so I could only see the bottom four lines or so, but I knew what you were going to say before you said it. I dunno why it was obvious to me as soon as I saw the header, and yet not when I wrote it.
Thanks.
Christian
We're just observing the seasonal migration from VB to VC. Most of these birds will be killed by predators or will die of hunger. Only the best will survive - Tomasz Sowinski 29-07-2002 ( on the number of newbie posters in the VC forum )
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Now worries . I've also figured out a way to get around the stupid Clone() mechanism and implemented C++ copy constructors, assignment operators, and comparison stuff. Stupid that gdiplus was written as to deny C++ exists (or perhaps it was done this way for the benefit of .net?).
Cheers,
swine
Check out Aephid Photokeeper, the powerful digital photo album solution at www.aephid.com.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
General News Question Answer Joke Rant Admin
|