C control flicker solution

  • 2020-12-19 21:09:13
  • OfStack

This article illustrates the solution of C# control flicker. Share to everybody for everybody reference. The specific analysis is as follows:

If you're drawing in Form, with or without double caching, you'll see the image flicker as it updates. The solution is to add the following three lines of code to the form's constructor:
Please add the following lines below the constructor:

SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true); // Do not erase the background .
SetStyle(ControlStyles.DoubleBuffer, true); // double-buffering

Parameter description:

UserPaint
If it is true, the control will draw itself, rather than through the operating system. This style applies only to classes derived from Control.

AllPaintingInWmPaint
If it is true, the control ignores WM_ERASEBKGND window messages to reduce flicker. This style should be applied only if the UserPaint bit is set to true.

DoubleBuffer
In the case of true, the drawing takes place in the buffer and outputs the result to the screen when it is finished. Double buffers prevent flicker caused by control redraw. To fully enable double buffering, you must also set the UserPaint and AllPaintingInWmPaint style bits to true.

Hopefully this article has helped you with your C# programming.


Related articles: