Making frame with GDI+

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

In one of my programs, I want to paint a frame around some controls via GDI+. The frame should have rounded corners
But as I'm really a newbie to programming, I just don't know how to do this via GDI+ and .net. Could anyone help me

Thank
Jame
 
Hi James

Try this :
Start code
******************************************
Private Function GetRoundedRect(ByVal BaseRect As RectangleF, ByVal
Radius As Single) As Region
' If corner radius is less than or equal to zero, return the
original rectangle
If Radius <= 0 Then Return New Region(BaseRect)
' If corner radius is greater than or equal to half the width or
height (whichever is shorter) then
' return a capsule instead of a lozenge.
If Radius >= (Math.Min(BaseRect.Width, BaseRect.Height) / 2.0) Then
Return GetCapsule(BaseRect)

Dim Diameter As Single = Radius + Radius
Dim ArcRect As New RectangleF(BaseRect.Location, New SizeF(Diameter,
Diameter))
Dim RR As New Drawing2D.GraphicsPath()
With RR
' top left arc
..AddArc(ArcRect, 180, 90)

' top right arc
ArcRect.X = BaseRect.Right - Diameter
..AddArc(ArcRect, 270, 90)

' bottom right arc
ArcRect.Y = BaseRect.Bottom - Diameter
..AddArc(ArcRect, 0, 90)

' bottom left arc
ArcRect.X = BaseRect.Left
..AddArc(ArcRect, 90, 90)

..CloseFigure()
End With

Return New Region(RR)
End Function

------------------------------------

Private Function GetCapsule(ByVal BaseRect As RectangleF) As Region
Dim Diameter As Single
Dim ArcRect As RectangleF
Dim RR As New Drawing2D.GraphicsPath()

With RR
Try
If BaseRect.Width > BaseRect.Height Then
' Return horizontal capsule
Diameter = BaseRect.Height
ArcRect = New RectangleF(BaseRect.Location, New
SizeF(Diameter, Diameter))
..AddArc(ArcRect, 90, 180)
ArcRect.X = BaseRect.Right - Diameter
..AddArc(ArcRect, 270, 180)

ElseIf BaseRect.Height > BaseRect.Width Then
' Return vertical capsule
Diameter = BaseRect.Width
ArcRect = New RectangleF(BaseRect.Location, New
SizeF(Diameter, Diameter))
..AddArc(ArcRect, 180, 180)
ArcRect.Y = BaseRect.Bottom - Diameter
..AddArc(ArcRect, 0, 180)

Else
' return circle
..AddEllipse(BaseRect)
End If

Catch e As Exception
..AddEllipse(BaseRect)
Finally
..CloseFigure()
End Try
End With

Return New Region(RR)
End Function
********************************************************
End code

Hope this helps.
Regards,
Anushi


James Steward said:
Hi,

In one of my programs, I want to paint a frame around some controls via
GDI+. The frame should have rounded corners.
But as I'm really a newbie to programming, I just don't know how to do
this via GDI+ and .net. Could anyone help me?
 
Thanks a lot! Just the right thing to start with
But there's one more thing I would need, I'm sorry. Could you post a code example about how to call these two (esp. the GetRoundedRect function) functions, so that a rounded rect of any size would be painted into my form??

Thanks a lo
james
 
Hi James
You would need to declare the parameters as RectangleF, SizeF and single.
To know how to do it, search msdn for rectangleF / SizeF . Here is the link
to online page, in case you need :
http://msdn.microsoft.com/library/d...stemDrawingRectangleFClassIntersectTopic2.asp
http://msdn.microsoft.com/library/d.../frlrfsystemdrawingrectanglefmemberstopic.asp
You'd then call the function as any other......... with the function name
followed by parameters.
Study for the System.Drawing classes and structures........ msdn contains
related code snippets :)
Regards.


James Steward said:
Thanks a lot! Just the right thing to start with!
But there's one more thing I would need, I'm sorry. Could you post a code
example about how to call these two (esp. the GetRoundedRect function)
functions, so that a rounded rect of any size would be painted into my
form???
 
Back
Top