1

I'm trying to draw a semi-transparent layer over my entire form when certain events occur, with a prompt in front of this semi-transparent layer (similar to the effect in Windows Vista/7 with UAC enabled)

At first I was trying to use a custom semi-transparent panel, but this didn't seem to work at all. As such I'm now trying to use a child form within the form using the opacity property on the form. However, when this semi-transparent form is added to the controls of the main form and displayed, it loses it's transparency.

I've got a hacky work around at the moment where the semi-transparent layer isn't actually a component within the main form, and the transparency works then, but obviously this isn't ideal and could potentially cause various issues further down the line.

Also, to further cimplicate things, the form which I want to contain the form cannot be made into an Mdi Parent (if this would help at all) as it itself is contained within a tab control of multiple forms.

Basically anything that could help me get a semi-transparent "dark out" layer over the rest of my form with a prompt on top would be ideal, I'm not too concerned about how it's achieved.

Thanks.

Sam
  • 23
  • 1
  • 4

1 Answers1

0

You could create a borderless form with a semi transparent background:

Private Sub button1_Click(sender As Object, e As EventArgs)
    Dim f As New Form2()
    f.WindowState = FormWindowState.Normal
    f.StartPosition = FormStartPosition.Manual
    f.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
    f.TransparencyKey = Color.Magenta
    f.Bounds = Me.Bounds
    f.ShowDialog()
End Sub

In Form2's paint event, you can set it to be semi-transparent by using a hatch brush:

Private Sub Form2_Paint(sender As Object, e As PaintEventArgs)
    Dim hb = New HatchBrush(HatchStyle.Percent50, Me.TransparencyKey)
    e.Graphics.FillRectangle(hb, Me.DisplayRectangle)
End Sub
John Koerner
  • 37,428
  • 8
  • 84
  • 134