How to draw this region: round corners rectangle?

  • Thread starter Thread starter Alberto Silva - MVP
  • Start date Start date
A

Alberto Silva - MVP

Hi,
I would like to draw a region with a round corners rectangle format, either
filled or empty... does someone has any sample on it?

Thanks,
Alberto Silva
 
Hi Alex,
It work just fine!
If we could only change the pen's width in cf...

To those who may be interested on the VB version, I used the
http://www.KamalPatel.net webservice to get it. The result is below.

Thanks,
Alberto Silva
Public Shared Sub DrawRoundedRectangle(ByVal g As Graphics, ByVal p As Pen,
ByVal backColor As Color, ByVal rc As Rectangle, ByVal size As Size)

Dim points() As Point = New Point(8) {}

'prepare points for poligon

points(0).X = rc.Left + size.Width / 2

points(0).Y = rc.Top + 1

points(1).X = rc.Right - size.Width / 2

points(1).Y = rc.Top + 1

points(2).X = rc.Right

points(2).Y = rc.Top + size.Height / 2

points(3).X = rc.Right

points(3).Y = rc.Bottom - size.Height / 2

points(4).X = rc.Right - size.Width / 2

points(4).Y = rc.Bottom

points(5).X = rc.Left + size.Width / 2

points(5).Y = rc.Bottom

points(6).X = rc.Left + 1

points(6).Y = rc.Bottom - size.Height / 2

points(7).X = rc.Left + 1

points(7).Y = rc.Top + size.Height / 2

'prepare brush for background

Dim fillBrush As Brush = New SolidBrush(backColor)

'draw side lines and circles in the corners

g.DrawLine(p, rc.Left + size.Width / 2, rc.Top, rc.Right - size.Width / 2,
rc.Top)

g.FillEllipse(fillBrush, rc.Right - size.Width, rc.Top, size.Width,
size.Height)

g.DrawEllipse(p, rc.Right - size.Width, rc.Top, size.Width, size.Height)

g.DrawLine(p, rc.Right, rc.Top + size.Height / 2, rc.Right, rc.Bottom -
size.Height / 2)

g.FillEllipse(fillBrush, rc.Right - size.Width, rc.Bottom - size.Height,
size.Width, size.Height)

g.DrawEllipse(p, rc.Right - size.Width, rc.Bottom - size.Height, size.Width,
size.Height)

g.DrawLine(p, rc.Right - size.Width / 2, rc.Bottom, rc.Left + size.Width /
2, rc.Bottom)

g.FillEllipse(fillBrush, rc.Left, rc.Bottom - size.Height, size.Width,
size.Height)

g.DrawEllipse(p, rc.Left, rc.Bottom - size.Height, size.Width, size.Height)

g.DrawLine(p, rc.Left, rc.Bottom - size.Height / 2, rc.Left, rc.Top +
size.Height / 2)

g.FillEllipse(fillBrush, rc.Left, rc.Top, size.Width, size.Height)

g.DrawEllipse(p, rc.Left, rc.Top, size.Width, size.Height)

'fill the background and remove the internal arcs

g.FillPolygon(fillBrush, points)

'dispose the brush

fillBrush.Dispose()

'----------------------------------------------------------------

' Converted from C# to VB .NET using CSharpToVBConverter(1.2).

' Developed by: Kamal Patel (http://www.KamalPatel.net)

'----------------------------------------------------------------

End Sub
 
Just a not, in the VB.net translated code, instead of..

Dim points() As Point = New Point(8)

use

Dim points() As Point = New Point(7)

or you'll get an odd corner... the reason is because of differences in the
base element of arrays between C# and vb.net.

Alberto Silva
 
Back
Top