2 Attachment(s)
How to draw a polygon over a form's controls
Variations on this question crop up repeatedly on the VB.Net forum. To save answering it over and over again, here's my solution:
Attachment 119675
The yellow polygon is actually on a separate Form (OverlayForm) from the form with the controls (Form1). You can still click the controls on Form1 except where they are covered by the polygon. You can move and resize Form1 because the code keeps the overlay in the same size and position. Form1 is the Owner of OverlayForm, which makes sure that no other windows can come between them and that they minimize and close together. In other words, the overlay is stuck firmly onto Form1.
It's a good idea to make the OverlayForm double buffered so it won't flicker when you resize the form. You could do this by adding a form (Form2) to your project and setting its DoubleBuffered to True in the Designer. But it only takes a few lines to declare a double-buffered Form2 in code (see the attachment). Here's the code for Form1:
Code:
Private WithEvents OverlayForm As New Form2
Private vertices() As PointF
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
vertices = GetPolygon() 'see below for the GetPolygon function.
With OverlayForm
Using gp As New Drawing2D.GraphicsPath
gp.AddPolygon(vertices)
.Region = New Region(gp) 'Make the overlay transparent except the polygon.
End Using
.FormBorderStyle = Windows.Forms.FormBorderStyle.None
.Opacity = 0.35
.ShowInTaskbar = False
.Show(Me) 'The Me argument makes this form Owner of OverlayForm
End With
End Sub
'Keep the OverlayForm fixed to this form in the XY direction:
Private Sub Form1_Move_etc(sender As System.Object, e As System.EventArgs) Handles Me.Shown, Me.SizeChanged, Me.Move
OverlayForm.Bounds = Me.Bounds
End Sub
Private Sub OverlayForm_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles OverlayForm.Paint
e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
e.Graphics.FillPolygon(Brushes.Yellow, vertices)
Using pn As New Pen(Color.BlueViolet, 2)
e.Graphics.DrawPolygon(pn, vertices)
End Using
End Sub
Notes:
1. The Region property of OverlayForm makes everything transparent except the polygon. You could do something similar by setting FormBorderStyle.None and TransparencyKey=BackgroundColor instead, but that may prevent you from clicking on Form1's controls.
2. The polygon is made partly transparent by setting the Opacity of the OverlayForm.
3. You could define your own polygon by setting values for the vertices array, or you could define your own GetPolygon function. For the purposes of illustration, I made the sawtooth polygon illustrated above with this function:
Code:
Private Function GetPolygon() As PointF()
Dim vList As New List(Of PointF)
Dim gp1 As New Drawing2D.GraphicsPath
gp1.AddEllipse(40, 85, 300, 200)
gp1.Flatten(Nothing, 5)
Dim gp2 As New Drawing2D.GraphicsPath
gp2.AddEllipse(60, 120, 250, 140)
gp2.Flatten(Nothing, 5)
For i As Integer = 0 To gp1.PointCount - 1
vList.Add(gp2.PathPoints(i))
vList.Add(gp1.PathPoints(i))
Next
Return vList.ToArray
End Function
You can download the complete code for both forms from the link below. To try it out, start by putting controls on Form1 to match those in the picture, or however you like. Then copy the code to the Form1 code.
BB
Attachment 119677