Drawing shapes on a form

When painting a square or rectangle like this on a form, how can you round off the corners so they arent at perfect angles to mimick the look of XP Style?  and is there an easy way to also use code to draw a small shadow with the square as well?  Commented code is best if someone can show me an example of my questions.


Private inLabel1 As Boolean
    Private Sub Label1_MouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.MouseEnter
        Dim c1 As Color = Color.FromArgb(255, &HC1, &HD2, &HEE)
        Label1.BackColor = c1
        inLabel1 = True
    End Sub

    Private Sub Label1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Label1.Paint
        If inLabel1 Then
            Dim r As Rectangle = New Rectangle(New Point(0, 0), Label1.Size)
            r.Width = r.Width - 1
            r.Height = r.Height - 1
            Dim p1 As Pen = New Pen(Color.FromArgb(255, &HAC, &HA8, &H99), 1)
            e.Graphics.DrawRectangle(p1, r)
        End If
    End Sub

    Private Sub Label1_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.MouseLeave
        Label1.BackColor = Color.FromKnownColor(KnownColor.Control)
        inLabel1 = False
    End Sub
teamdadAsked:
Who is Participating?
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Here is one way to create a rounded rectangular label:

Imports System.Drawing.Drawing2D

Public Class Form1
    Inherits System.Windows.Forms.Form

#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 Label1 As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Label1 = New System.Windows.Forms.Label
        Me.SuspendLayout()
        '
        'Label1
        '
        Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label1.Location = New System.Drawing.Point(88, 80)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(120, 48)
        Me.Label1.TabIndex = 1
        Me.Label1.Text = "Rounded Label"
        Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(280, 222)
        Me.Controls.Add(Me.Label1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        roundOffLabel(Label1)
    End Sub

    Private Sub roundOffLabel(ByVal l As Label)
        Dim gp As New GraphicsPath
        gp.AddArc(New Rectangle(0, 0, 10, 10), 180, 90)
        gp.AddLine(10, 0, l.Width - 10 - 1, 0)
        gp.AddArc(New Rectangle(l.Width - 10 - 1, 0, 10, 10), 270, 90)
        gp.AddLine(l.Width - 1, 10, l.Width - 1, l.Height - 10 - 1)
        gp.AddArc(New Rectangle(l.Width - 10 - 1, l.Height - 10 - 1, 10, 10), 0, 90)
        gp.AddLine(l.Width - 10 - 1, l.Height - 1, 10, l.Height - 1)
        gp.AddArc(New Rectangle(0, l.Height - 10 - 1, 10, 10), 90, 90)
        gp.AddLine(0, l.Height - 10 - 1, 0, 10)
        l.Region = New Region(gp)
    End Sub

    Private Sub Label1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label1.MouseEnter
        Label1.Tag = Label1.BackColor
        Label1.BackColor = Color.Red
    End Sub

    Private Sub Label1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label1.MouseLeave
        Label1.BackColor = Label1.Tag
    End Sub

End Class
0
 
iboutchkineCommented:
You can create any shape of control you want. her eis an example Button looks like a star


Imports System.Drawing.Drawing2D
Imports System.Math

Public Class Form1
    Inherits System.Windows.Forms.Form

#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 Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.BackColor = System.Drawing.Color.FromArgb(CType(192, Byte), CType(255, Byte), CType(255, Byte))
        Me.Button1.Location = New System.Drawing.Point(8, 8)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(152, 136)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "I'm A Star!"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(168, 149)
        Me.Controls.Add(Me.Button1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Create an array of points defining the region.
        Dim points(9) As Point
        Dim point_types(9) As Byte
        Dim cx As Double = Button1.Width / 2
        Dim cy As Double = Button1.Height / 2
        Dim theta As Double = -PI / 2
        Dim dtheta As Double = 2 * PI / 10
        For i As Integer = 0 To 4
            points(2 * i) = New Point( _
                CInt(cx + Cos(theta) * cx * 0.9), _
                CInt(cy + Sin(theta) * cy * 0.9))
            theta += dtheta

            points(2 * i + 1) = New Point( _
                CInt(cx + Cos(theta) * cx * 0.5), _
                CInt(cy + Sin(theta) * cy * 0.5))
            theta += dtheta
        Next i

        ' Indicate all points are for lines (not Bezier, etc.).
        For i As Integer = 0 To 9
            point_types(i) = CByte(PathPointType.Line)
        Next i

        ' Create a Path using the points.
        Dim btn_path As New GraphicsPath(points, point_types)

        ' Make the region.
        Dim btn_region As New Region(btn_path)

        ' Set the button's Region.
        Button1.Region = btn_region
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MsgBox("Clicked!")
    End Sub
End Class
0
 
teamdadAuthor Commented:
I'm just trying to round off the corners so they arent at perfect angles to mimick the look of XP Style and maybe add an alpha shadow if it isn't too hard.  I don't want to draw strange controls like that.
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
Joe_GriffithCommented:
You can display XP style controls without drawing them yourself.  Everyone here is assuming that you want to draw them yourself to get some sort of XP looking control with out actually running on XP right?
0
 
adam_pedleyCommented:
Hey idle_mind

Just thought i would say that the roundofflabel function is brilliant, it even works with panels

Worked first time, just copy and paste
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Thanx...glad you found it useful.  =)

~IM
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.