Draw Transparent Panel without Being Black by Form Minimization

大城市里の小女人 提交于 2019-12-02 08:29:56
Jimi

Reza Aghaei already told you what was actually preventing the Panel transparency to work at all:
WS_EX_TRANSPARENT was set to 0x00 instead of 0x20.

Some suggestions to improve the appearace of the translucent Panel.

  • Test the Panel setting these Styles:

This will prevent any artifact on the Panel when you're moving it both at Desing-Time and Run-Time.

this.SetStyle(ControlStyles.AllPaintingInWmPaint |
              ControlStyles.UserPaint |
              ControlStyles.Opaque, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, false);
this.DoubleBuffered = false;
this.UpdateStyles();
  • When setting the opacity value:

Use Refresh() if it's Desing-Time, this will update the new Opacity visual immediately. Othrerwise, you'll have to click on the Form to see the changes. At Run-Time, Invalidate() is enough (usually).

set {
    if (value < 0 || value > 255) throw new ArgumentException("value must be between 0 and 255");
    this.opacity = value;
    if (this.DesignMode) this.FindForm().Refresh();
    this.Invalidate();
}

Modified test class:

public class ExtendedPanel : Panel
{
    private const int WS_EX_TRANSPARENT = 0x20;
    public ExtendedPanel()
    {
        this.SetStyle(ControlStyles.AllPaintingInWmPaint |
                      ControlStyles.UserPaint |
                      ControlStyles.Opaque, true);
        this.SetStyle(ControlStyles.OptimizedDoubleBuffer, false);
        this.DoubleBuffered = false;
        this.UpdateStyles();
    }

    private int opacity = 1;

    [DefaultValue(1)]
    public int Opacity
    {
        get => this.opacity;
        set {
            if (value < 0 || value > 255) throw new ArgumentException("value must be between 0 and 255");
            this.opacity = value;
            if (this.DesignMode) this.FindForm().Refresh();
            this.Invalidate();
        }
    }

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.ExStyle = cp.ExStyle | WS_EX_TRANSPARENT;
            return cp;
        }
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        using (SolidBrush bgbrush = new SolidBrush(Color.FromArgb(this.opacity, this.BackColor)))
        {
            e.Graphics.FillRectangle(bgbrush, this.ClientRectangle);
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!