Skip Navigation Links.
How to Create a Spiral Shaped Form
Language(s):VB.Net
Category(s):Winforms

One of the new features of VB.Net is the ability to create odd shaped forms. This example creates a spiral shaped form.

'VB.Net Example - Spiral Shaped Form. 
'
'Jon Vote 07/2002
'
'Idioma Software Inc. 
'www.idioma-software.com
'jon@idioma-software.com 
'
'This example may be freely used in code 
'as long as credit is given to the author. 
'No publication is authorized. 
'
'Code is presented as is. 
'User assumes all responsibility

'1) Start a new VB.Net project. Form1 is created by default. 
'2) Press F7 to bring up the code window. 
'3) Replace all (some? no all!) of the code in the code window 
'   with the following:
'
'VB.Net Example: Spiral Shaped Form
'
'Jon Vote 07/2002
'
'Idioma Software Inc. 
'www.idioma-software.com
'jon@idioma-software.com 
'
'Code is presented as is. 
'User assumes all responsibility
'
Public Class Form1

    Inherits System.Windows.Forms.Form
    Private rgnOriginal As ResolveEventArgs

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
    Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem6 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem7 As System.Windows.Forms.MenuItem
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.MainMenu1 = New System.Windows.Forms.MainMenu()
        Me.MenuItem1 = New System.Windows.Forms.MenuItem()
        Me.MenuItem2 = New System.Windows.Forms.MenuItem()
        Me.MenuItem3 = New System.Windows.Forms.MenuItem()
        Me.MenuItem6 = New System.Windows.Forms.MenuItem()
        Me.MenuItem7 = New System.Windows.Forms.MenuItem()
        '
        'MainMenu1
        '
        Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1})
        '
        'MenuItem1
        '
        Me.MenuItem1.Index = 0
        Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem2, Me.MenuItem3, Me.MenuItem6, Me.MenuItem7})
        Me.MenuItem1.Text = "Form"
        '
        'MenuItem2
        '
        Me.MenuItem2.Index = 0
        Me.MenuItem2.Text = "Normal"
        '
        'MenuItem3
        '
        Me.MenuItem3.Index = 1
        Me.MenuItem3.Text = "Spiral"
        '
        'MenuItem6
        '
        Me.MenuItem6.Index = 2
        Me.MenuItem6.Text = "-"
        '
        'MenuItem7
        '
        Me.MenuItem7.Index = 3
        Me.MenuItem7.Text = "Close"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(716, 433)
        Me.Menu = Me.MainMenu1
        Me.Name = "Form1"
        Me.Text = "Form1"

    End Sub

#End Region
    Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
        NormalForm()
    End Sub

    Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click
        SpiralForm()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Text = "This code sample was brought to you by the folks at Idioma Software! - www.idioma-software.com "
    End Sub

    Private Sub MenuItem5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'frmAbout.DefInstance.Show()
    End Sub

    Private Sub MenuItem7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem7.Click
        Me.Close()
    End Sub

    Private Sub Form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
        End
    End Sub

    Private Sub SpiralForm()
        Dim oGraphicsPath As New System.Drawing.Drawing2D.GraphicsPath()
        Dim sngMaxRadius As Single
        Dim sngStep As Single
        Dim recHeader As New System.Drawing.Rectangle()

        sngMaxRadius = 50
        sngStep = 0.1
        DrawSpiral(oGraphicsPath, sngMaxRadius, sngStep)
        oGraphicsPath.CloseFigure()

        recHeader.X = 0
        recHeader.Y = 0
        recHeader.Height = 50
        recHeader.Width = Me.Width
        oGraphicsPath.AddRectangle(recHeader)

        Me.Region = New Region(oGraphicsPath)
    End Sub

    Private Sub DrawSpiral(ByRef oGraphicsPath As System.Drawing.Drawing2D.GraphicsPath, ByVal sngMaxRadius As Single, ByVal sngStep As Single)
        Dim dblAngle As Double
        Dim x1 As Double
        Dim x2 As Double
        Dim y1 As Double
        Dim y2 As Double
        Dim dblTheta As Double
        Dim r As Integer
        Dim sngXCenter As Single
        Dim sngYCenter As Single
        Dim ptFrom As System.Drawing.Point
        Dim ptTo As System.Drawing.Point

        sngXCenter = Me.Width / 2
        sngYCenter = Me.Height / 2
        x1 = sngXCenter
        y1 = sngYCenter
        For dblAngle = 0 To (5 * 360) Step sngStep

            'Increment the radius
            r = dblAngle / 360 * sngMaxRadius

            'Convert to radians
            dblTheta = dblAngle * (2 * 3.14 / 360)
            x2 = r * System.Math.Cos(dblTheta) + sngXCenter
            y2 = r * System.Math.Sin(dblTheta) + sngYCenter
            ptFrom.X = x1
            ptFrom.Y = y1
            ptTo.X = x2
            ptTo.Y = y2

            oGraphicsPath.AddLine(ptFrom, ptTo)
            'Debug.WriteLine(dblTheta & ", ", x2 & "," & y2)
            x1 = x2
            y1 = y2

        Next dblAngle
    End Sub

    Private Sub NormalForm()
        Dim recForm As New System.Drawing.Rectangle()
        Dim oGraphicsPath As New System.Drawing.Drawing2D.GraphicsPath()
        recForm.X = 0
        recForm.Y = 0
        recForm.Width = Me.Width
        recForm.Height = Me.Height
        oGraphicsPath.AddRectangle(recForm)
        Me.Region = New Region(oGraphicsPath)
    End Sub

End Class

This article has been viewed 6298 times.
The examples on this page are presented "as is". They may be used in code as long as credit is given to the original author. Contents of this page may not be reproduced or published in any other manner what so ever without written permission from Idioma Software Inc.