Tuesday, March 25, 2008

Flashing and Flickering


Since its creating a couple years ago, our C# charting product has had an issue with flashing and flickering. Several of us have tried several different ways to enable double buffering. This seemed like the solution, especially drawing from our experience in Java. But no matter how we set it, nothing ever changed. So I figured we must just be calling some Paint() method somewhere too many times. I dug through our source again with a debugger trying to figure things out. On occasion, Paint() methods were being called twice, but this didn't seem to correlate with flashing at all. And it was probably part of the setup as well to make sure things appeared correctly.

I then embarked on another search of the Internet trying to find out how to really enable double buffering in .NET. I stumbled upon an answer that was slightly different than anything I've tried before. Specifically, I've always been trying to use various versions of

this.SetStyle(ControlStyles.DoubleBuffer, true);

in various different places. There seemed to be a lot of potential places due to the object hierarchy we have in place. But the latest suggestion I found this morning was to set

this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);

and set all those bits on the Control object in its constructor. Well, this was new to me and actually points to one specific object in the package - the Panel that holds the Charts. I added this line to each of its constructors, rebuilt the package and ran our tests. Everything not only passed, but there was no more flashing! Then I ran our demo gallery (a set of examples and much more demanding than the simple tests) and everything looked great!

So thank you to random forum poster somewhere!

No comments: