10.11 Performing Flicker-Free Animation

 <  Day Day Up  >  

You want to remove the flicker that occurs as a result of continuously repainting during animation sequences.


Technique

When performing animation, you might find that the rendered objects flash slightly. To remove this anomaly, call the SetStyle method, passing three values from the ControlStyles enumerated data type. These three values appear in the following code and are turned on and off with the last parameter:

 
 private void menuFlickerFree_Click(object sender, System.EventArgs e) {     if( menuFlickerFree.Checked == true )     {         menuFlickerFree.Checked = false;         SetStyle(ControlStyles.AllPaintingInWmPaint, false);         SetStyle(ControlStyles.DoubleBuffer, false);     }     else     {         menuFlickerFree.Checked = true;         SetStyle(ControlStyles.UserPaint, true );         SetStyle(ControlStyles.AllPaintingInWmPaint, true);         SetStyle(ControlStyles.DoubleBuffer, true);     } } 

Comments

The code in the technique portion of this recipe, which comes from the demonstration application created for Recipe 10.8, "Using Rotation and Translation Transformations," allows you to specify whether rendering should use double buffering to eliminate flickering based on the current state of a menu item. This code appears in the downloadable code for this book. When you run the application and double buffering is not enabled, you see brief flickers of white as a new frame in the animation renders . Double buffering works by creating a separate area of video memory identical to the rendering surface. GDI+ therefore works with two different surfaces, sometimes referred to as pages . When you make rendering calls such as DrawRectangle , GDI+ renders the output to the currently hidden buffer. Using synchronization with the horizontal scan lines of the display adapter, GDI+ performs a page flip so that the surface that is being rendered to is shown while the other page is hidden and used for subsequent operations. This method ensures that a rendered surface always displays rather than a surface that is continuously erased and redrawn, which in turn causes the flickering.

One thing to note is that you should always enable or disable the ControlStyles.DoubleBuffer style in conjunction with the ControlStyle.UserPaint and ControlStyle.AllPaintingInWmPaint styles. This step allows GDI+ to perform its double-buffering technique. The UserPaint style transfers rendering control from the operating system to GDI+. Forgetting to set that style would restrain GDI+ from performing its double-buffering technique. The AllPaintingInWmPaint rendering style refers to how painting should occur in response to the WM_PAINT window message. Windows within the operating system are controlled and manipulated by messages that are dispatched by the operating system. The WM_PAINT message is sent to your form whenever the operating system determines a repaint of the window should occur. Another message, WM_ERASEBKGND , also arrives before the WM_PAINT message to erase the current contents of the window. AllPaintingInWmPaint notifies the form that the contents of the form should not be erased in response to a WM_ERASEBKGND message, thereby allowing GDI+ to render using double buffering.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net