Drawing Round-Edged Rectangle Using VB.NET






3.56/5 (26 votes)
Apr 30, 2004

138293

2859
This code can be used to draw Round Edged Rectangles using VB.NET
Introduction
This is a Article explains about drawing a Round-edge Rectangle using VB.Net. It will help beginners to learn 'How to make use of GDI+ in VB.NET
The input parameters are Xaxis, Yaxis, Width, Height and Diameter. Based on the diameter, the calculations are made. For an example I have put the color of the rectangle as Black.
If user want to change they can chage the color.
Hope it will be helpful.
Public Sub DrawRoundedRectangle(ByVal objGraphics As Graphics, _
ByVal m_intxAxis As Integer, _
ByVal m_intyAxis As Integer, _
ByVal m_intWidth As Integer, _
ByVal m_intHeight As Integer, _
ByVal m_diameter As Integer)
'Dim g As Graphics
Dim BaseRect As New RectangleF(m_intxAxis, m_intyAxis, m_intWidth,
m_intHeight)
Dim ArcRect As New RectangleF(BaseRect.Location,
New SizeF(m_diameter, m_diameter))
'top left Arc
objGraphics.DrawArc(Pens.Black, ArcRect, 180, 90)
objGraphics.DrawLine(Pens.Black, m_intxAxis + CInt(m_diameter / 2),
m_intyAxis,
m_intxAxis + m_intWidth - CInt(m_diameter / 2),
m_intyAxis)
' top right arc
ArcRect.X = BaseRect.Right - m_diameter
objGraphics.DrawArc(Pens.Black, ArcRect, 270, 90)
objGraphics.DrawLine(Pens.Black, m_intxAxis + m_intWidth,
m_intyAxis + CInt(m_diameter / 2),
m_intxAxis + m_intWidth,
m_intyAxis + m_intHeight - CInt(m_diameter / 2))
' bottom right arc
ArcRect.Y = BaseRect.Bottom - m_diameter
objGraphics.DrawArc(Pens.Black, ArcRect, 0, 90)
objGraphics.DrawLine(Pens.Black, m_intxAxis + CInt(m_diameter / 2),
m_intyAxis + m_intHeight,
m_intxAxis + m_intWidth - CInt(m_diameter / 2),
m_intyAxis + m_intHeight)
' bottom left arc
ArcRect.X = BaseRect.Left
objGraphics.DrawArc(Pens.Black, ArcRect, 90, 90)
objGraphics.DrawLine(Pens.Black,
m_intxAxis, m_intyAxis + CInt(m_diameter / 2),
m_intxAxis,
m_intyAxis + m_intHeight - CInt(m_diameter / 2))
End Sub
You can call this method from anywhere by just giving the required values to the input parameters.You can chnage the input parameters like
Private Sub DrawRoundedRectangle(ByVal rectf as RectangleF, _
ByVal m_diameter As Integer)
and from RectangleF
object You can get the values of xAxis, yAxis, width and Height of the rectangles.