user7305
user7305

Reputation: 6001

How to eliminate flicker in Windows.Forms custom control when scrolling?

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?

Upvotes: 8

Views: 18117

Answers (4)

Shaun Austin
Shaun Austin

Reputation: 3842

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.

Upvotes: 13

Brad Bruce
Brad Bruce

Reputation: 7807

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);

Upvotes: 8

Eric W
Eric W

Reputation: 570

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.

Upvotes: 1

Grokys
Grokys

Reputation: 16526

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?

Upvotes: 0

Related Questions