views:

1192

answers:

3

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

A: 
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.
Joel Coehoorn
+1  A: 

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
Sachin
+1  A: 

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.

David Anderson