Code for 2008 and 2010 versions of Vb.Net. Have you ever wanted an oval, triangle, a pentagon, a hex

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi ALL,
<edit> On 27th January, 2011<br/>
If you are using VB.Net 2005 or earlier then please see my 14th post in this thread.
Up to now it is the last post of mine in this thread.<br/>

</edit>
<br/>
I saw a post earlier on which reminded me about Extension Methods.<br/>
<br/>
This led me to create the following code which acts on the base class <span style="text-decoration:underline
CONTROL for ALL controls!!<br/>
<br/>
So??!! You might ask what does it do?<br/>
<br/>
Well using just one line of code you can transform any of your controls<br/>
into the shape you want within the bounds of the original shape of the control.<br/>
<br/>
So if you have a square control like a square button or picturebox, you will get a regular shape.<br/>
<br/>
If the dotted rectangle ( that the control is drawn out within ) is wider than it is tall<br/>
or vice-versa than the regular shape is stretched to fit this shape.<br/>
<br/>
Best explained with an example.<br/>
<br/>
From the PROJECT menu select <span style="text-decoration:underline
ADD MODULE , type in <span style="text-decoration:underline
ShapedControls.Vb in the <span style="text-decoration:underline
NAME box and click on <span style="text-decoration:underline OK.

Then <span style="text-decoration:underline PASTE this code in.>>


<pre lang="x-vbnet Option Strict On

Imports System.Runtime.CompilerServices

Module ShapedControls

Public Const Pi As Double = Math.PI
Public Const DegreesToRadians As Double = 180 / Pi

<Extension()> _
Public Sub Shape(ByVal ctrl As Control, Optional ByVal NumberOfSides As Integer = 3, Optional ByVal OffsetAngleInDegrees As Double = 0)

If NumberOfSides < 3 Then Throw New Exception("Number of sides can only be 3 or more.")

Dim MyAngle As Double = OffsetAngleInDegrees / DegreesToRadians

Dim radius1 As Integer = ctrl.Height 2
Dim radius2 As Integer = ctrl.Width 2
Dim xInt, yInt As Integer
Dim xDoub, yDoub As Double
Dim MyPath As New Drawing2D.GraphicsPath

For angle As Double = MyAngle To ((2 * Pi) + MyAngle) Step ((2 * Pi) / NumberOfSides)

xDoub = radius2 * Math.Cos(angle) + radius2
yDoub = radius1 * Math.Sin(angle) + radius1
xInt = CInt(Int(xDoub))
yInt = CInt(Int(yDoub))
MyPath.AddLine(New Point(xInt, yInt), New Point(xInt, yInt))

Next

MyPath.CloseFigure()
ctrl.Region = New Region(MyPath)
MyPath.Dispose()

End Sub

End Module[/code]

<div style="color:Black; background-color:White
<br/>
Have you done that?<br/>
<br/>
Good.<br/>
<br/>
Proceed to the next post.<br/>
<br/>
<br/>
Regards,<br/>
<br/>
John

View the full article
 
Back
Top