Round borders

Use this board to post your code snippets - tips and tricks
8 posts Page 1 of 1
Contributors
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4389
Joined: Tue Aug 04, 2009 1:47 am

Round borders
CodenStuff
Easy code to create rounded borders around controls to make them look a bit nicer.

Manual code:
Code: Select all
    Public Sub RoundBorder(ByVal G As Graphics, ByVal BorderColour As Color, ByVal X As Single, ByVal Y As Single, ByVal Width As Single, ByVal Height As Single, ByVal Radius As Single, ByVal Thickness As Single)
        Dim GfXPath As GraphicsPath = New GraphicsPath()
        Dim Border As New Pen(BorderColour, Thickness)
        GfXPath.AddLine(X + Radius, Y, X + Width - (Radius * 2), Y)
        GfXPath.AddArc(X + Width - (Radius * 2), Y, Radius * 2, Radius * 2, 270, 90)
        GfXPath.AddLine(X + Width, Y + Radius, X + Width, Y + Height - (Radius * 2))
        GfXPath.AddArc(X + Width - (Radius * 2), Y + Height - (Radius * 2), Radius * 2, Radius * 2, 0, 90)
        GfXPath.AddLine(X + Width - (Radius * 2), Y + Height, X + Radius, Y + Height)
        GfXPath.AddArc(X, Y + Height - (Radius * 2), Radius * 2, Radius * 2, 90, 90)
        GfXPath.AddLine(X, Y + Height - (Radius * 2), X, Y + Radius)
        GfXPath.AddArc(X, Y, Radius * 2, Radius * 2, 180, 90)
        GfXPath.CloseFigure()
        G.SmoothingMode = SmoothingMode.AntiAlias
        G.DrawPath(Border, GfXPath)
        GfXPath.Dispose()
    End Sub
To use just add the following in to your forms paint event:
Code: Select all
RoundBorder(e.Graphics, Color.Red, Control.Location.X, Control.Location.Y, Control.Width, Control.Height, 5, 3)
Break down
e.Graphics = Graphics instance
Color.Red = The colour of the border
Control.Location.X = Your control horizontal coordinates
Control.Location.Y = Your control vertical coordinates
Control.Width = Your control width
Control.Height = Your control height
5 = Radius of the curve
3 = Border thickness in pixels



Auto Code:
Code: Select all
    Public Sub AutoRoundBorder(ByVal G As Graphics, ByVal BorderColour As Color, ByVal Who As Control, ByVal Radius As Single, ByVal Thickness As Single)
        Dim GfXPath As GraphicsPath = New GraphicsPath()
        Dim Border As New Pen(BorderColour, Thickness)
        Dim X = Who.Location.X - Radius
        Dim Y = Who.Location.Y - Radius
        Dim Width = Who.Width + (Radius * 2) : Dim height = Who.Height + (Radius * 2)
        GfXPath.AddLine(X + Radius, Y, X + Width - (Radius * 2), Y)
        GfXPath.AddArc(X + Width - (Radius * 2), Y, Radius * 2, Radius * 2, 270, 90)
        GfXPath.AddLine(X + Width, Y + Radius, X + Width, Y + height - (Radius * 2))
        GfXPath.AddArc(X + Width - (Radius * 2), Y + height - (Radius * 2), Radius * 2, Radius * 2, 0, 90)
        GfXPath.AddLine(X + Width - (Radius * 2), Y + height, X + Radius, Y + height)
        GfXPath.AddArc(X, Y + height - (Radius * 2), Radius * 2, Radius * 2, 90, 90)
        GfXPath.AddLine(X, Y + height - (Radius * 2), X, Y + Radius)
        GfXPath.AddArc(X, Y, Radius * 2, Radius * 2, 180, 90)
        GfXPath.CloseFigure()
        G.SmoothingMode = SmoothingMode.AntiAlias
        G.DrawPath(Border, GfXPath)
        GfXPath.Dispose()
    End Sub
Easier code without much fuss and again in the form paint event:
Code: Select all
AutoRoundBorder(e.Graphics, Color.Red, ControlName, 5, 3)
Break down
e.Graphics = Graphics instance
Color.Red = The colour of the border
ControlName = The name of your control
5 = Radius of the curve
3 = Border thickness in pixels


Can be used to make a nice UI cooll;
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
Filip
Coding Guru
Coding Guru
Posts: 833
Joined: Wed Jan 05, 2011 3:59 pm

Re: Round borders
Filip
And that's why you use CSS:
Code: Select all
border-radius: 5px; 
CodenStuff wrote:
Nope, it's just your sick and dirty mind. You sick twisted warped little pervo :D
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4389
Joined: Tue Aug 04, 2009 1:47 am

Re: Round borders
CodenStuff
This is VB..you Imbecile :lol:
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
Filip
Coding Guru
Coding Guru
Posts: 833
Joined: Wed Jan 05, 2011 3:59 pm

Re: Round borders
Filip
Overly complicated language...
CodenStuff wrote:
Nope, it's just your sick and dirty mind. You sick twisted warped little pervo :D
User avatar
jtlusco
VIP - Donator
VIP - Donator
Posts: 138
Joined: Mon Jul 12, 2010 11:28 pm

Re: Round borders
jtlusco
Yes but you can do more with VB then you can with CSS
Have you been scripted today
Image
Filip
Coding Guru
Coding Guru
Posts: 833
Joined: Wed Jan 05, 2011 3:59 pm

Re: Round borders
Filip
But if you combine it with JS/PHP/Myself, you can do amazing stuff.. The main reason I quit views because of you wanted to distribute app, everyone needs windows with dot net framework..

than*
CodenStuff wrote:
Nope, it's just your sick and dirty mind. You sick twisted warped little pervo :D
User avatar
Codex
Coding God
Coding God
Posts: 2028
Joined: Wed Mar 31, 2010 5:50 pm

Re: Round borders
Codex
Think of CSS as VB.NET designer's properties window... now zoom out, add html, which is the "designer" view of .NET (where you add buttons etc.). Zoom out even more, add php and javascript to see how amazing and awesome it is.

OT: sorry for hijacking your awesome quick snip :P #CodenStuff
We shall let the revolution begin.. the revolution for freedom, freedom against censorship. We shall fight in the fields and in the streets, we shall fight in the hills; we shall never surrender
User avatar
mikethedj4
VIP - Site Partner
VIP - Site Partner
Posts: 2592
Joined: Thu Mar 25, 2010 4:36 am

Re: Round borders
mikethedj4
Filip wrote:
And that's why you use CSS:
Code: Select all
border-radius: 5px; 
lmao; :lol: Ahhh that was funny :lol: lmao;
jtlusco wrote:
Yes but you can do more with VB then you can with CSS
I'd have to disagree with you there, because VB is platform dependent.
8 posts Page 1 of 1
Return to “Quick Snips”