Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Drawing shapes on a form

Posted on 2004-10-22
6
Medium Priority
?
659 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
Comment
Question by:teamdad
6 Comments
 
LVL 28

Expert Comment

by:iboutchkine
ID: 12384286
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
ID: 12384381
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 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 12384587
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 7

Expert Comment

by:Joe_Griffith
ID: 12385064
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
ID: 12650943
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 86

Expert Comment

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

~IM
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Suggested Courses
Course of the Month11 days, 17 hours left to enroll

916 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