flickering problem - invalidate function

   i am currently working on a chess project. I have used the fuction Invalidate() to reflect each move of piece. But the consequece is that the whole screen is redrawn on each move ... specially on MouseMove() function where invalidate is called at each point moved.
Kindly tell an alternate solution ... to avoid flickering.

Who is Participating?
Jaime OlivaresSoftware ArchitectCommented:
you need a flicker free technique:

Modify your OnPaint or OnDraw event, similar to this:

void CMainFrame::OnPaint()
      CPaintDC dc(this); // device context for painting
      CDC NoFlick;
      CBitmap bmp, *oldBmp;
      CRect rect;

      bmp.CreateCompatibleBitmap(&dc, rect.Width(), rect.Height());
      oldBmp = NoFlick.SelectObject(&bmp);

      dc.BitBlt(0, 0, rect.right, rect.bottom, &NoFlick, 0, 0, SRCCOPY);

YourDrawFunction(CDC *pDC)
    // Do all your drawing here
Override the EraseBkgnd
BOOL CExampleView::OnEraseBkgnd(CDC* pDC)
      // TODO: Add your message handler code here and/or call default
      return CView::OnEraseBkgnd(pDC);
// To this code
BOOL CExampleView::OnEraseBkgnd(CDC* pDC)
      return FALSE;
Change your OnDraw code to the following: void CExampleView::OnDraw(CDC* dc)
    CMemDC pDC(dc);
    CExampleDoc* pDoc = GetDocument();
    // TODO: add draw code for native data here - use pDC
     //as the device context to draw to

Download the CmemnDC from CodeProject.com
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.