6

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
1
  • I have added the code below to get the exact thing you are looking for. If anything extra is needed please ad a comment and I will update the same Feb 9, 2009 at 13:32

4 Answers 4

12

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
1
  • While this maybe an old post, top google result.. A shorten example that may work for some people is simply this TransparencyKey = Me.BackColor.. But make sure your forms backcolor is not in the image your using or any other controls on the form.. Still set your form backcolor to have a unqiue color
    – Angry 84
    Jul 17, 2016 at 7:28
2

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

1

There are a few methods you could use.

  • Use the forms TransparencyKey
  • Override OnPaintBackground (WM_ERASEBKGND)
  • Override WndProc and handle the paint messages (WM_NCPAINT, WM_PAINT, etc)

I recommend overriding the window procedure to get optimal results.

-2
Me.Opacity = 0

Be warned that:

  1. This is for the entire form, rather than just the background. There are work-arounds to make certain parts more opague.
  2. It's only psuedo-transparency where it takes a snapshot of what's behind it. It's smart enough to know when you move the form, but not when you move other transparent objects on top of the form.
1
  • This will also hide all control elements on the form.
    – Ahmad
    Dec 4, 2014 at 5:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.