I'm drawing a panel on a bitmap in picturebox using C#. I used below code, which is fine when I don't minimize the Form. When I minimize the Form and again maximize it to the first size, all panels which were drawn by this class, show black background. I found that when I change ControlStyles.Opaque to something else such as "SupportsTransparentBackColor" the problem would be fixed but the panels would not be transparent anymore.
public class ExtendedPanel : Panel
{
private const int WS_EX_TRANSPARENT = 0x00;
public ExtendedPanel()
{
SetStyle(ControlStyles.Opaque, true);
}
private int opacity = 1;
[DefaultValue(1)]
public int Opacity
{
get
{
return this.opacity;
}
set
{
if (value < 0 || value > 100)
throw new ArgumentException("value must be between 0 and 100");
this.opacity = value;
}
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle = cp.ExStyle | WS_EX_TRANSPARENT;
return cp;
}
}
protected override void OnPaint(PaintEventArgs e)
{
using (var brush = new SolidBrush(Color.FromArgb(this.opacity * 1 / 100, this.BackColor)))
{
e.Graphics.FillRectangle(brush, this.ClientRectangle);
}
base.OnPaint(e);
}
}