2

How to stop flickering on windows form?

I have used below code that will help to stop flickering

protected override CreateParams CreateParams
{
    get
    {
         CreateParams cp = base.CreateParams;
         cp.ExStyle |= 0x02000000;  // Turn on WS_EX_COMPOSITED
         Thread.Sleep(100);
         return cp;
    }
}

but using this my CPU usage goes to 100%.

Please help me. Thanks in advance.

  • 1
    When you say windows form.. do you mean winforms? when does the flickering occur? what controls are on your form? if any tab controls your already in a bad position. the only real solution is to convert to wpf. – Sayse Jun 09 '14 at 07:24
  • Do u use the Onpaint event of the form,also check if any one your controls is responsible for this. u can set the DoubleBuffer of the form to true. – Sara S. Jun 09 '14 at 07:44

2 Answers2

2

Most often than not, just setting the Form's DobuleBuffered property to True will correct flickering and related drawing problems. Doesn't require any specialized code.

Have you given it a try?

enter image description here

Pradeep Kumar
  • 6,836
  • 4
  • 21
  • 47
1

It would be helpful if you posted the code that causes your form to flicker.
without that this is the most general solution I can think of (except for turning off your monitor :0)):

Try using the answer suggested in this SO thread.

Basically you need to:

Create the following class:

class DrawingControl
{
    [DllImport("user32.dll")]
    public static extern int SendMessage(IntPtr hWnd, Int32 wMsg, bool wParam, Int32 lParam);

    private const int WM_SETREDRAW = 11; 

    public static void SuspendDrawing( Control parent )
    {
        SendMessage(parent.Handle, WM_SETREDRAW, false, 0);
    }

    public static void ResumeDrawing( Control parent )
    {
        SendMessage(parent.Handle, WM_SETREDRAW, true, 0);
        parent.Refresh();
    }
 }

In your form add a call before the flickering to SuspendDrawing and after it to ResumeDrawing
Something like:

public void MyFlickeringFunction()
 {
     try
     {
         SuspendDrawing(this);
         /*Your code goes here...*/
     }
     finally
     {
          ResumeDrawing(this)
     }
 }
Community
  • 1
  • 1
Avi Turner
  • 10,234
  • 7
  • 48
  • 75