What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleSet a control's Region to shape the control in VB .NET
Keywordsregion, window region, shape, shaped control
CategoriesVB.NET, Graphics
Set the control's Region propery to a System.Drawing.Region object. Creating the Region is the only trick.

This example makes a polygonal region. It first creates an array of Points to define a star-shaped polygon. It creates an array of Bytes that indicates that each point is part of a line, not a Bezier curve or something else. It uses the Points and Bytes to define a GraphicsPath object and uses the GraphicsPath to define the Region. Finally, it sets the Button1 control's Region property to this 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
Note that the Region property is inherited from the Control class so IntelliSense doesn't list it. You need to type it yourself.
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.