Transparent background on winforms?

2019-01-08 21:30发布

I wanted to make my windows form transparent so removed the borders, controls and everything leaving only the forms box, then I tried to the BackColor and TransparencyKey to transparent but it didnt work out as BackColor would not accept transparent color. After searching around I found this at msdn:

SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.BackColor = Color.Transparent;
this.TransparencyKey = BackColor;

Unhappyly it did not work either. I still get the grey or any other selected color background.

All I wanted to do is to have the windows form transparent so I could use a background image that would act as if it was my windows form.

I searched around here and saw many topics in regards opacity which is not what I am looking for and also saw some in regards this method I was trying but have not found an answer yet.

Hope anyone can light my path.

UPDATE:

image removed as problem is solved

9条回答
Deceive 欺骗
2楼-- · 2019-01-08 22:11

I've tried the solutions above (and also) many other solutions from other posts.

In my case, I did it with the following setup:

public partial class WaitingDialog : Form
{
    public WaitingDialog()
    {
        InitializeComponent();

        SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        this.BackColor = Color.Transparent;

        // Other stuff
    }

    protected override void OnPaintBackground(PaintEventArgs e) { /* Ignore */ }
}

As you can see, this is a mix of previously given answers.

查看更多
地球回转人心会变
3楼-- · 2019-01-08 22:13

Should it have anything to do with "opacity" of the form / its background ? Did you try opacity = 0

Also see if this CP article helps:

查看更多
霸刀☆藐视天下
4楼-- · 2019-01-08 22:15

Here was my solution:

In the constructors add these two lines:

        this.BackColor = Color.LimeGreen;
        this.TransparencyKey = Color.LimeGreen;

In your form, add this method:

    protected override void OnPaintBackground(PaintEventArgs e)
    {
        e.Graphics.FillRectangle(Brushes.LimeGreen, e.ClipRectangle);
    }

Be warned, not only is this form fully transparent inside the frame, but you can also click through it. However, it might be cool to draw an image onto it and make the form able to be dragged everywhere to create a custom shaped form.

查看更多
登录 后发表回答