What is the best way to make a C # animated user control flicker-free?

I am currently creating a custom control that should handle animation in a C # project. This is basically a list containing a fixed number of items that can be moved. An element (another user control with a background image and several generated labels) can move up, down, or be removed from the list.

I would like to create an animated movement as the elements move inside the container's user control, but it seems to me that moving the controls around the lines, such as

myCustomControl.left -= m_iSpeed; 

triggered in a timer event flickers and has terrible rendering, even if double buffering is enabled.

So here's the question: What is the best way to create an animated C # flicker-free control? . Should I just create custom controls and process the entire drawing in the background image of the panel so that I generate? Is there a super animation method that I have not found? :)

Thanks!

+4
source share
4 answers

Your best bet for flicker-free animation is to make the picture yourself (use the Graphics object in the Paint event handler) and use double buffering. In your user control, you need the following code in the constructor:

 this.SetStyle(ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.SupportsTransparentBackColor, true); 
+3
source

A similar discussion took place this morning on this subject. visual results of updating C # form in flicker. Therefore, I will be lazy and give the same answer that I gave there:

You can try calling this.SuspendLayout (); before you start moving, and this.ResumeLayout (false); when you finish moving all the controls, So all the controls should draw right away, and you should have less flicker.

On a side note, I tried to reproduce this here at work, but didn't seem to work. Can you give another example of code that I can fix, maybe?

+1
source

The usual way to get flicker-free animation is to implement double buffering. Take a look at the Code Project article

http://www.codeproject.com/KB/GDI-plus/flickerFreeDrawing.aspx

Minimizing drawing calls until you're ready is also a good idea.

0
source

Source: https://habr.com/ru/post/1277560/


All Articles