|
|
Title | Make a shaped form by setting its region in Visual Basic .NET |
Description | This example shows how to make a shaped form by setting its region in Visual Basic .NET. |
Keywords | forms, region, set form region, shaped form, TransparencyKey, Visual Basic .NET, VB.NET |
Categories | Controls, Software Engineering |
|
|
In theory you can set a form's TransparencyKey property to make parts of the form disappear. In practice, this feature has gotten buggy in recent versions of .NET so it doesn't always work consistently on every computer.
An alternative is to create a region that defines the form's shape and then set the form's Region property to the region.
The example program uses the following code to give its form a star shape.
|
|
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles MyBase.Load
' Make points to define a polygon for the form.
Dim pts(0 To 9) As PointF
Dim cx As Single = Me.ClientSize.Width * 0.5
Dim cy As Single = Me.ClientSize.Height * 0.5
Dim r1 As Single = Me.ClientSize.Height * 0.45
Dim r2 As Single = Me.ClientSize.Height * 0.25
Dim theta As Single = -PI / 2
Dim dtheta As Single = 2 * PI / 10
For i As Integer = 0 To 9 Step 2
pts(i) = New PointF( _
cx + r1 * Cos(theta), _
cy + r1 * Sin(theta))
theta += dtheta
pts(i + 1) = New PointF( _
cx + r2 * Cos(theta), _
cy + r2 * Sin(theta))
theta += dtheta
Next i
' Use the polygon to define a GraphicsPath.
Dim path As New GraphicsPath()
path.AddPolygon(pts)
' Make a region from the path.
Dim form_region As New Region(path)
' Restrict the form to the region.
Me.Region = form_region
End Sub
|
|
|
|
|
|