8

I want to create a custom control in C#. But every time I have to fully redraw my control, it flickers, even if I use double buffering (drawing to an Image first, and blitting that).

How do I eliminate flicker when I have to fully redraw?

Lars Truijens
  • 42,837
  • 6
  • 126
  • 143
user7305
  • 5,741
  • 9
  • 28
  • 23

4 Answers4

13

You could try putting the following in your constructor after the InitiliseComponent call.

SetStyle(ControlStyles.OptimizedDoubleBuffer | 
         ControlStyles.UserPaint |
         ControlStyles.AllPaintingInWmPaint, true);

EDIT:

If you're giving this a go, if you can, remove your own double buffering code and just have the control draw itself in response to the appropriate virtual methods being called.

Julien Roncaglia
  • 17,397
  • 4
  • 57
  • 75
Shaun Austin
  • 3,782
  • 3
  • 23
  • 27
  • I usually put that stuff in before the Application.Run, but yea this I think is the best solution to this problem. – Quibblesome Sep 15 '08 at 16:38
  • I agree. I've also found a good url in another flicker-related question. http://www.codeproject.com/KB/graphics/DoubleBuffering.aspx – user7305 Sep 15 '08 at 17:12
  • I've tried soooo mant different solutions but this fixes it!! +1 for telling to put it after InitializeComponent(); !! xoxoxo – Emily Dec 27 '16 at 09:45
8

I pulled this from a working C# program. Other posters have syntax errors and clearly copied from C++ instead of C#

SetStyle(ControlStyles.OptimizedDoubleBuffer | 
                        ControlStyles.UserPaint |
                        ControlStyles.AllPaintingInWmPaint, true);
Brad Bruce
  • 7,638
  • 3
  • 39
  • 60
1

It may be good enough to just call

SetStyle(ControlStyles::UserPaint | ControlStyles::AllDrawingInWmPaint, true);

The flickering you are seeing most likely because Windows draws the background of the control first (via WM_ERASEBKGND), then asks your control to do whatever drawing you need to do (via WM_PAINT). By disabling the background paint and doing all painting in your OnPaint override can eliminate the problem in 99% of the cases without the need to use all the memory needed for double buffering.

Eric W
  • 579
  • 4
  • 14
0

You say you've tried double buffering, but then you say drawing to an Image first and blitting that. Have you tried setting DoubleBuffered = true in the constructor rather than doing it yourself with an Image?

Grokys
  • 16,228
  • 14
  • 69
  • 101