How can I set the panel1 transparency in WinForms? Except for this option, nothing works in VS2015.

public sealed class TransparentPanel : Panel { protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams; cp.ExStyle |= 0x20; return cp; } } protected override void OnPaint(PaintEventArgs e) { e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(0, 0, 0, 0)), this.ClientRectangle); } protected override void OnSizeChanged(EventArgs e) { base.OnSizeChanged(e); } } 
  • 2
    To draw in OnPaint having touched Parent. Controls? - cpp_user
  • one
    "How to set transparency" - see stackoverflow.com/a/9359642/5673266 , and if needed for a form / window, see WS_EX_LAYERED - Stack

2 answers 2

So you can make the panel, and any other control, transparent at any time, without rewriting OnPaint() . Well, not completely transparent. Just from the whole panel, only the controls placed on it remain for drawing. Unprotected parts of the panel are simply not processed.

 GraphicsPath gr = new GraphicsPath(); foreach (Control c in panel1.Controls) { gr.AddRectangle(new Rectangle(c.Location, c.Size)); } panel1.Region = new Region(gr); 

Using the Region property and GraphicsPath objects, you can give the WinForms control any arbitrarily complex shape, for example, a curvilinear polygon with holes or N independent fragments working as one control.

Upd

This option:

 panel1.BackColor = System.Drawing.Color.Transparent; 

does not work, or rather does not work as expected. The control copies the background color of the parent control, this is where the "transparency" ends. those. if you have another control under this panel, it will not be visible.

    There is such a way:

     panel1.BackColor = Color.FromArgb(0,0,0,0); panel1.Parent = this; 

    Instead of this, you need to substitute the control over which your panel is located. But it will be transparent only in relation to this control. Using the first parameter of the FromArgb () method, you can control the degree of transparency.