Flickering in C# doing a SlideButton (DoubleBuffer not working apparently)

久未见 提交于 2019-12-02 11:34:59

One day I ran into this problem as well. The simplest (maybe not the most elegant) solution was to remove the OnPaintBackground() and add it into the normal OnPaint() method because otherwise the background is painted onto your last Gfx each time the control is invalidated what causes the flickering:

    public ctor()
    {
        // this does just work with OptimizedDoubleBuffer set to true
        this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
    }

    protected override void OnPaintBackground(PaintEventArgs e)
    {
        // do NOT paint the background here but in OnPaint() to prevent flickering!
        //base.OnPaintBackground(e);
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        // do the background and the base stuff first (if required)
        base.OnPaintBackground(e);
        base.OnPaint(e);

        // ... custom paint code goes here ...
    }

You might give it a try, good luck.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!