Make a form's background transparent

2019-01-14 23:46发布

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

Thanks in advance!

5条回答
迷人小祖宗
2楼-- · 2019-01-15 00:26

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)

查看更多
beautiful°
3楼-- · 2019-01-15 00:27

Put the following in the constructor of the form:

public Form1()
{
    this.TransparencyKey = Color.Turquoise;
    this.BackColor = Color.Turquoise;
}
查看更多
姐就是有狂的资本
4楼-- · 2019-01-15 00:27

I'm using this code

this.TransparencyKey = (BackColor);

Just add this line in the construcror under InitializeComponent();

Source:

http://mishelshaji.co.in/2017/creating-a-transparent-window-in-windows-form-application/

查看更多
Rolldiameter
5楼-- · 2019-01-15 00:40

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.

查看更多
Fickle 薄情
6楼-- · 2019-01-15 00:50

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.

查看更多
登录 后发表回答