Rectangle curved edges

J

John Wood

Do anyone know an easy way to draw rectangles with curved edges? I couldn't
see anything in Graphics that would provide that.

Thanks in advance.
 
B

Bob Powell [MVP]

The GDI+ FAQ has answers to this and many other questions

--
Bob Powell [MVP]
Visual C#, System.Drawing

The Image Transition Library wraps up and LED style instrumentation is
available in the June of Well Formed for C# or VB programmers
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://bobpowelldotnet.blogspot.com/atom.xml
 
J

John Wood

For anyone who's interested, I converted that code to C#:

public GraphicsPath GetGraphicsPath(int x, int y, int width, int height,
int radius)
{
GraphicsPath gp = new GraphicsPath();
gp.AddLine(x + radius, y, x + width - radius, y);
gp.AddArc(x + width - radius, y, radius, radius, 270, 90);
gp.AddLine(x + width, y + radius, x + width, y + height - radius);
gp.AddArc(x + width - radius, y + height - radius, radius, radius, 0,
90);
gp.AddLine(x + width - radius, y + height, x + radius, y + height);
gp.AddArc(x, y + height - radius, radius, radius, 90, 90);
gp.AddLine(x, y + height - radius, x, y + radius);
gp.AddArc(x, y, radius, radius, 180, 90);
gp.CloseFigure();
return gp;
}

public void DrawRoundRect(Graphics g, Pen p, int x, int y, int width, int
height, int radius)
{
GraphicsPath gp = GetGraphicsPath(x, y, width, height, radius);
g.DrawPath(p, gp);
gp.Dispose();
}

public void FillRoundRect(Graphics g, Brush b, int x, int y, int width,
int height, int radius)
{
GraphicsPath gp = GetGraphicsPath(x, y, width, height, radius);
g.FillPath(b, gp);
gp.Dispose();
}
 
J

John Wood

Just thought I'd let you know that I *LOVE* the new halo effect sample. Will
look very nice in my splash screen!

Thanks.
 
H

Herfried K. Wagner [MVP]

* "John Wood said:
Do anyone know an easy way to draw rectangles with curved edges? I couldn't
see anything in Graphics that would provide that.

P/invoke on 'RoundRect':

\\\
Private Declare Function RoundRect Lib "gdi32.dll" ( _
ByVal hdc As IntPtr, _
ByVal X1 As Int32, _
ByVal Y1 As Int32, _
ByVal X2 As Int32, _
ByVal Y2 As Int32, _
ByVal X3 As Int32, _
ByVal Y3 As Int32 _
) As Int32
///
 
B

Bob Powell [MVP]

That was too simple :))

--
Bob Powell [MVP]
Visual C#, System.Drawing

The Image Transition Library wraps up and LED style instrumentation is
available in the June of Well Formed for C# or VB programmers
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://bobpowelldotnet.blogspot.com/atom.xml
 
T

Tim Wilson

I would think so. And I would also assume that, since a Graphics object is
stateless, other things such as line style, color, thickness, etc would also
not be easily accessible without having to SelectObject/DeleteObject
everything into the underlying device context.

IntPtr hDC = e.Graphics.GetHdc();
// Create objects, select them in, select them out, and then delete them.
e.Graphics.ReleaseHdc(hDC);

But for a simple rounded rect the P/Invoke to RoundRect is good.

--
Tim Wilson
..Net Compact Framework MVP

John Wood said:
I assume that would ignore the smoothing mode and stuff though...
Wouldn't it?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top