Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set a form to have a transparent background

I am struggling to get my form to have a transparent background in vb.net

Currently in the form New I set

Me.SetStyle(ControlStyles.SupportsTransparentBackColor, true) 

But still the form shows up as having the default grey background

Can anyone help??

EDIT: I need the controls on the form to be visible so I don't think setting the opacity to 0 will work

EDIT: I tried the transparency key solution but it doesn't work. I have a circular image with a black background. OnPaint I set the transparency key to the img pixel at 0,0, this then leaves me with circular image (which I want ) It hides the black background but I am still left with the default grey rectangle of the form.

below is the code I have -

Public Sub New()

    Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
    Me.BackColor = Color.Transparent
    ' This call is required by the Windows Form Designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
    Me.Timer1.Start()
End Sub

Private Sub frmWoll_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

    Dim img As Bitmap = CType(Me.BackgroundImage, Bitmap)

    img.MakeTransparent(img.GetPixel(2, 2))
    Me.TransparencyKey = img.GetPixel(2, 2)
End Sub
like image 243
Dean Avatar asked Feb 05 '09 21:02

Dean


People also ask

How do I create a transparent form?

You can also type “transparent” in the Form Color box to change the form background's transparency.

How do I add a transparent background?

Go to Select > Invert. On the right side of the screen, right-click on your image and select Add Alpha Channel. This will provide a transparent background.


2 Answers

Use TransparencyKey for transparent form.

eg.

TransparencyKey = Color.Red
Button1.BackColor = Color.Red

Now run the form you will find that the button1 has a hole in it.

So using this method you can create a mask image in paint for which part has to be transparent and apply that image to form and voila the form is now transparent.

Edit: Sorry for late reply.

Following is your code modified to suit your requirement

Public Sub New()

    Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
    Me.BackColor = Color.Transparent

    ' This call is required by the Windows Form Designer.
    InitializeComponent()
    ' Add any initialization after the InitializeComponent() call.
    Dim img As Bitmap = CType(Me.BackgroundImage, Bitmap)

    'img.MakeTransparent(img.GetPixel(2, 2))
    Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
    Me.TransparencyKey = img.GetPixel(2, 2)
End Sub
like image 161
Sachin Chavan Avatar answered Nov 15 '22 05:11

Sachin Chavan


Set Form's TransparencyKey color property same as form's Background color property

like image 32
Hiren H Patel Avatar answered Nov 15 '22 07:11

Hiren H Patel