Transparent control flickers when scrolling

I created a C # application that has custom buttons. They all have a transparent background, so I inherited from Control and redefined CreateParams / OnPaintBackground and added the InvalidateEx function.

Now I need a grid similar to the layout (TableLayoutPanel), which is also transparent. Again I created my own control and overridden CreateParams / OnPaintBackground and added the InvalidateEx function. In the scroll, I see that the backgrounds β€œremember” what he drew, and therefore I see ghosting (if that's the right word). Connecting the InvalidateEx function to the Scroll event will cause the panel to flicker when scrolling.

Is there any way to remove this flicker?

public class GridLayout : TableLayoutPanel
{
    public GridLayout()
    {
        this.Scroll += delegate { this.InvalidateEx(); };
    }

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= 0x20; //Transparent
            return cp;
        }
    }

    protected override void OnPaintBackground(PaintEventArgs e)
    {
        // do nothing
    }

    protected void InvalidateEx()
    {
        if (Parent == null)
            return;

        Rectangle rc = new Rectangle(this.Location, this.Size);
        Parent.Invalidate(rc, true);
    }
}

WPF, , β†’ Winforms.

+3

All Articles