2

I'm in progress with my first application in visual basic, and I'm using the visual basic studio... I have created a form, with buttons - form has an background image with rounded corners and I can not set its color to "transparent" because the following error occurs:

Control does not support transparent background colors.

Now I have no idea what to do. I have read that I can set the transparency in the code, by adding the following lines:

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

But it doesn't seem to work... So what else can I do?

The whole code:

Public Class Form1

  Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    MsgBox("Test", 32, "Button Click")
  End Sub

  Private Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
    Close()
  End Sub

  Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    SetStyle(ControlStyles.SupportsTransparentBackColor, True)
    Me.BackColor = Color.Transparent
  End Sub

End Class
LarsTech
  • 80,625
  • 14
  • 153
  • 225
Scott
  • 5,991
  • 15
  • 35
  • 42

2 Answers2

4

For forms, try this:

Me.TransparencyKey = Me.BackColor

Since you have a background image, make sure to pick a BackColor that isn't found in the image.

LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • Doesn't work I think: `http://i.imgur.com/Afd67.png`. The lime color isn't found in the image for sure . – Scott Nov 21 '12 at 00:22
  • @Scott If you are referring to the "greenish" color at the bottom, that's not lime green, that's green. The colors have to match 100%. The transparency won't work well if it's an aliased color — only the matching color will be made transparent. – LarsTech Nov 21 '12 at 00:48
  • 1
    The simpler way is just to set `Me.TransparencyKey = BackColor` (with images you will also encounter ICC profiles and gamma that can affect the actual color displayed). I do have a feeling though, as OP mentioned rounded corners that he wants the form to be completely transparent so you can see f.x. the desktop behind the corners (op?). –  Nov 21 '12 at 00:55
  • @AbdiasSoftware, yes - it should be completely transparent. The `Me.TransparencyKey = Me.BackColor` solution doesn't work either. Please see this image: `http://i.imgur.com/bPQKM.png`, thats an left side of my form. Please notice the arrows - thats what I'm talking about. – Scott Nov 21 '12 at 09:21
  • If that color comes from your form it could be just a matter of washing the alpha in f.x Photoshop (select the area which should be transparent with a non-feathered setting and cut the pixels). There might be some "leaking" anti-aliasing from the lines. As LarsTech said the color need to match 100% and if there is anti-aliasing data in the transparent area they will affect the actual color (alpha and color data are pre-multiplied). –  Nov 21 '12 at 11:37
  • @AbdiasSoftware You were right, I had to remove some pixels with GIMP and not everything is fine! Thank you. – Scott Nov 21 '12 at 13:02
0

just change the form's Opacity property.

Paul Ishak
  • 1,093
  • 14
  • 19