Round your corners

 
Many people face a problem when it comes to drawing the controls with rounded corners. So I got down and dirty with some simple drawing and came up with this. All you have to do is to call this Sub Procedure with the parameters passed to it and you are done. Enjoy!
 
   1:    Public Sub DrawRoundedRectangle(ByVal objGraphics As Graphics, _
   2:                                 ByVal m_intxAxis As Integer, _
   3:                                 ByVal m_intyAxis As Integer, _
   4:                                 ByVal m_intWidth As Integer, _
   5:                                 ByVal m_intHeight As Integer, _
   6:                                 ByVal m_diameter As Integer, _
   7:                                 ByVal g As Graphics)
   8:   
   9:   
  10:          Dim path As New GraphicsPath()
  11:          'Dim g As Graphics
  12:          Dim BaseRect As New RectangleF(m_intxAxis, m_intyAxis, m_intWidth,
  13:                                        m_intHeight)
  14:          Dim ArcRect As New RectangleF(BaseRect.Location,
  15:                                    New SizeF(m_diameter, m_diameter))
  16:          'top left Arc
  17:          path.AddArc(ArcRect, 180, 90)
  18:          path.AddLine(m_intxAxis + CInt(m_diameter / 2),
  19:                               m_intyAxis,
  20:                               m_intxAxis + m_intWidth - CInt(m_diameter / 2),
  21:                               m_intyAxis)
  22:   
  23:          ' top right arc
  24:          ArcRect.X = BaseRect.Right - m_diameter
  25:          path.AddArc(ArcRect, 270, 90)
  26:          path.AddLine(m_intxAxis + m_intWidth,
  27:                               m_intyAxis + CInt(m_diameter / 2),
  28:                               m_intxAxis + m_intWidth,
  29:                               m_intyAxis + m_intHeight - CInt(m_diameter / 2))
  30:   
  31:          ' bottom right arc
  32:          ArcRect.Y = BaseRect.Bottom - m_diameter
  33:          path.AddArc(ArcRect, 0, 90)
  34:          path.AddLine(m_intxAxis + CInt(m_diameter / 2),
  35:                               m_intyAxis + m_intHeight,
  36:                               m_intxAxis + m_intWidth - CInt(m_diameter / 2),
  37:                               m_intyAxis + m_intHeight)
  38:   
  39:          ' bottom left arc
  40:          ArcRect.X = BaseRect.Left
  41:          path.AddArc(ArcRect, 90, 90)
  42:          path.AddLine(
  43:                               m_intxAxis, m_intyAxis + CInt(m_diameter / 2),
  44:                               m_intxAxis,
  45:                               m_intyAxis + m_intHeight - CInt(m_diameter / 2))
  46:          g.CompositingQuality = CompositingQuality.HighQuality
  47:          g.SmoothingMode = SmoothingMode.AntiAlias
  48:          g.DrawPath(Pens.Black, path)
  49:      End Sub

Comments

Post a Comment

No spam please :)

Popular posts from this blog

A Good Day

My Little Wife

Fallen - Based on a true story (almost)