Solved

Drawing shapes on a form

Posted on 2004-10-22
656 Views
Last Modified: 2010-04-24
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
0
Question by:teamdad
    6 Comments
     
    LVL 28

    Expert Comment

    by:iboutchkine
    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
     

    Author Comment

    by:teamdad
    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
     
    LVL 85

    Accepted Solution

    by:
    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
     
    LVL 7

    Expert Comment

    by:Joe_Griffith
    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
     
    LVL 4

    Expert Comment

    by:adam_pedley
    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
     
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Thanx...glad you found it useful.  =)

    ~IM
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    The Complete Ruby on Rails Developer Course

    Ruby on Rails is one of the most popular web development frameworks, and a useful tool used by both startups and more established companies to build strong graphic user interfaces, and responsive websites and apps.

    I'm currently working for a company where I have to upgrade over 50 VB6 programs to VB.NET 2008.  So far I'm about half way through, and I've learned quite a few tricks that drastically improve the performance of VB.NET apps. Because there are a…
    Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    875 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now