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

前端 未结 1 1604
野性不改
野性不改 2021-01-26 03:37

i got a problem with flickering in C# WinForms proyect.

I simply made a SlideButton control: this is the code:

using System;
using System.Collections.Gen         


        
1条回答
  •  别那么骄傲
    2021-01-26 04:11

    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.

    0 讨论(0)
提交回复
热议问题