14

How I make a background transparent on my form? Is it possible in C#?

Thanks in advance!

Ry-
  • 218,210
  • 55
  • 464
  • 476
The Mask
  • 17,007
  • 37
  • 111
  • 185

4 Answers4

39

You can set the BackColor of your form to an uncommon color (say Color.Magenta) then set the form's TransparencyKey property to the same color. Then, set the FormBorderStyle to None.

Of course, that's just the quick and easy solution. The edges of controls are ugly, you have to keep changing the background color of new controls you add (if they're Buttons or something like that) and a whole host of other problems.

It really depends what you want to achieve. What is it? If you want to make a widget sort of thing, there are much better ways. If you need rounded corners or a custom background, there are much better ways. So please provide some more information if TransparencyKey isn't quite what you had in mind.

Ry-
  • 218,210
  • 55
  • 464
  • 476
17

Put the following in the constructor of the form:

public Form1()
{
    this.TransparencyKey = Color.Turquoise;
    this.BackColor = Color.Turquoise;
}

Note: This method prevents you from clicking through the form.

Nahydrin
  • 13,197
  • 12
  • 59
  • 101
4

Update:

How to: Give Your Control a Transparent Background

Deprecated: How to: Create Transparent Windows Forms:

Note: As transparent forms are only supported in Windows 2000 or later, Windows Forms will be completely opaque when run on older operating systems, such as Windows 98, regardless of the value set for the Opacity property.

Nahydrin
  • 13,197
  • 12
  • 59
  • 101
Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
1

A simple solution to get a transparent background in a winform is to overwrite the OnPaintBackground method like this:

protected override void OnPaintBackground(PaintEventArgs e)
{
//empty implementation
}

(Notice that the base.OnpaintBackground(e) is removed from the function)

Alexander Cosman
  • 1,117
  • 11
  • 20
  • 4
    But that just draws a completely black background… – Ry- Apr 10 '13 at 14:43
  • This doesn't draw a completely black background. If you override this method and don't call the base class method nothing is drawn as a background. Three people (including myself) have tested this and it works for all of them. – Alexander Cosman Apr 11 '13 at 14:10
  • 1
    This works, however any controls in the form that have Transparent set as their BackColor end up with the form's configured BackColor as their background. Would you have to override every control's OnPaintBackground event that you wanted to be transparent? – Nick Shaw Jun 13 '14 at 13:11
  • This suits me more because using this doesn't make the edges of the round controls ugly, also when using transparency key is messed up if you are using shadows on controls (at least thats what i've noticed) – Igor Meszaros Jun 16 '14 at 12:11