Based on the solution of using double buffering to avoid flicker in WTL

  • 2020-04-01 23:39:42
  • OfStack

In the window that oneself draws, sometimes can have flicker phenomenon. Why does it flicker? Actually because the program in the drawing window needs to use the background color to empty the display area, and then draw. Because the contrast between these two is bigger, can be caught by the person's eye, feeling flicker.
Double buffering is the process of drawing a picture in memory and then copying it directly to the screen so that the contrast is small and the flicker is not felt.

< img border = 0 SRC = "/ / files.jb51.net/file_images/article/201305/201305171620473.png" >


In the WTL CDoubleBufferImpl
WTL has a ready-made double-buffered class implementation, can be very convenient to use the effect.
CDoubleBufferImpl is in atlframe.h.
1. First inherited from CDoubleBufferImpl


class TCtrl: 
        public CWindowImpl< TCtrl>,  
        public WTL::CDoubleBufferImpl<TCtrl>  //Inherits a double-buffered class

2. Since the WM_ERASEBKGND and WM_PAINT messages have been processed in the double-buffered class, you need to remove the processing of these messages from your code. Then add double-buffered message processing.

 BEGIN_MSG_MAP(TCtrl)
//        MESSAGE_HANDLER(WM_PAINT,        OnPaint)
        CHAIN_MSG_MAP( WTL::CDoubleBufferImpl<TCtrl>)
    END_MSG_MAP()

3. Add a DoPaint function, the function declaration is as follows:

void DoPaint(CDCHandle dc);

4. Move the code from the original OnPaint function into DoPaint, and note that the original CPaintDC needs to use the CDCHandler parameter instead

void TCtrl::DoPaint( CDCHandle dc )
{
    //CPaintDC dc(m_hWnd);     dc.MoveTo( xx ...  )}

OK, compile.


Related articles: