Double buffering of the WinForm

Source: Internet
Author: User

Search WinForm Double buffer, you will find a lot of articles on the network, the mess said the shady. The first scenario:

   

SetStyle (Controlstyles.userpaint, true);  SetStyle (Controlstyles.allpaintinginwmpaint, true); Erase background is forbidden.  SetStyle (Controlstyles.doublebuffer, true); Double buffering

The second scenario:

This. Doulebuffered=true

The third scenario:

  Bitmap Bimtbitmap = new Bitmap (width, height);            Graphics g = graphics.fromimage (Bimtbitmap);            Plot Area            g.drawxxxx ();            G.drawimage (bimtbitmap,this. ClientRectangle);

Of course, there are other, is the above code mixed, the slightest unclear what the specific meaning of the code, I explained that the first scenario = the second scenario = The third scenario +userpaint and Allpaintinginvmpaint set to true.

First, why do you blink? For example, you want to draw two lines, but the writing algorithm is relatively high complexity, resulting in a slow intermediate process, will lead to a first out, after a, but your goal is suddenly appear two, so it flashes, similar to the following code.

    protected override void OnPaint (PaintEventArgs e)        {            base. OnPaint (e);            Pen pen = new Pen (color.red);            E.graphics.drawline (pen,0,0,this. Width,this. Height);            Thread.Sleep (+);            E.graphics.drawline (pen,10,0,this. Width,this. Height);           Thread.Sleep (+);        }

How to solve this problem? All-in-all solutions can be solved. Try to find, do not use the above 123 scheme, the line one by one, after use must be two together out, no flicker.  But flicker does not mean that the only reason for this to happen, before the drawing of the background will be drawn, if the time between OnPaintBackground and OnPaint too long? The background color to the control color, you draw again, between the time too long so flashing, at this time can solve the problem is 1, 2. The third solution does not solve the problem, want the third solution to solve the problem with the first two lines of a solution.

Why is that? In fact, the third solution is our program itself to achieve the double buffer, but if OnPaintBackground and OnPaint time too long, no matter what you do in the OnPaint double buffering, flashing is the middle of the interval, can not solve the problem. How did the 1, 2 solution be solved? Then I'll explain it below. See what happens in the third scenario.

First, when we do not open double buffering, in the WndProc function of the control class, when the message number is 20 o'clock, the background erase occurs, the specific code to decompile.

This is a complete message processing, followed by a walk OnPaint, the message number is 15

Then the interval between the two functions will cause flicker, but the general program can not see, if it is this flicker, artificial double buffering is not solve the problem, then why is the equivalent way? When we turn on double buffering, take a look at the code inside the control class:

Protected virtual bool doublebuffered    {      get      {        return this. GetStyle (Controlstyles.optimizeddoublebuffer);      }      Set      {        if (value = = this. doublebuffered)          return;        if (value) this          . SetStyle (Controlstyles.allpaintinginwmpaint | Controlstyles.optimizeddoublebuffer, value);        else this          . SetStyle (controlstyles.optimizeddoublebuffer, value);      }    }

In fact, it is open allpaintinginwmpaint, this is forbidden background erase, when we set this to true, in the upper of the WMERASEBKGND function, there is no background erase

So the background is not drawn? Of course not, you will receive message number 15th first:

After entering this function:

First see a Flag1, this is actually judge whether double buffer, can see it is by doubulebuffered property or behind the This.getstyle (Controlstyles.allpaintinginwmpaint) & & this. Doublebufferingenabled, and what is doublebufferingenabled? See Source:

So Flag1 is the doublebuffered attribute | | UserPaint | doublebuffer| AllPaintingInWmPaint are true, so I say 1, 2 of the two scenarios are in fact consistent, as to say 1, 22 scenarios better than 3 because, 1, 22 programs are forbidden to erase the message, but in this function is the background drawing, First save the graphics, and then draw the background, then paint in the code, the final render, is a one-time drawing up, so there is no flicker, and simple with a third scheme, although they have achieved a buffer, and then eggs, if the background and foreground time too long to blink, Therefore, the allpaintinwmpaint should be set to achieve the same effect. The specific relevant code for this function is as follows:

As for the UserPaint also have those attributes, the document is also vague, in fact, many see the source code to understand, UserPaint almost all is true, it is not true,onpaint function will not go, In the construction of the control class, both UserPaint and AllPaintingInWmPaint are set to true, and the subclass control is changed. AllPaintingInWmPaint can let the control message handle ignoring the erase background message, the control's Doulebuffered property setting and the three enumerations you directly set to arrive at an effect.

Double buffering of the WinForm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.