How to set a form for a transparent background

I am trying to get my form in order to have transparent background in vb.net

Currently, in the New form, I installed

Me.SetStyle(ControlStyles.SupportsTransparentBackColor, true) 

But the shape looks like it has a gray color by default

Can anyone help?

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

EDIT: I tried the solution for the transparent key, but it does not work. I have a circular image with a black background. OnPaint I set the transparency key in the img pixel to 0,0, this leaves me with a circular image (which I want). It hides the black background, but I still remain with the gray rectangle by default in the form.

below is the code that 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 
+6
winforms
source share
4 answers

Use a TransparencyKey for a transparent shape.

eg.

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

Now run the form, you will see that there is a hole in button 1.

Thus, using this method, you can create an image of a mask in paint for which a part should be transparent and apply this image to the form and voila, the form is now transparent.

Edit: Sorry for the late reply.

Below is your code that matches 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 
+12
source share

Set the transparency property of the TransparencyKey form in the same way as the Background color property form.

+2
source share

You can use several methods.

  • Use TransparencyKey Forms
  • Override OnPaintBackground (WM_ERASEBKGND)
  • Override WndProc and process messages with paint (WM_NCPAINT, WM_PAINT, etc.)

I recommend redefining the window procedure to get optimal results.

+1
source share
 Me.Opacity = 0 

Be warned that:

  • This is for the whole form, and not just for the background. There are jobs to make some parts more difficult.
  • This is only psuedo-transparent where a snapshot of what is behind it is required. It is smart enough to know when you move the form, but not when you move other transparent objects on top of the form.
-2
source share

All Articles