0

My OnPaint() function calls several other drawing functions.

 void CGraph::OnPaint ()
 {
    CPaintDC dc(this);
    // CMemDC DC(&dc);

    dc.SetViewportOrg (0, 400);
    dc.SetMapMode(MM_ISOTROPIC);
    dc.SetWindowExt(1000, 800);
    dc.SetViewportExt(1000, -800);

    ProcessData ();
    DrawCoordinateSystem (&dc);
    DrawGrid (&dc);
    DrawGraph (&dc);
}

Example of DrawCoordianteSystem:

void CGraph::DrawCoordinateSystem (CDC* pDC)
{
   CPen PenBlack (PS_SOLID, 1, RGB(0, 0, 0));
   pDC->SelectObject (PenBlack);
   // Rectangle round the system
   pDC->Rectangle (0, -400, 1000, 400);
   // Horizontal axis
   pDC->MoveTo (0, 0);
   pDC->LineTo (1000, 0);
   pDC->SetTextColor (RGB(0,0,0));
   pDC->SetBkColor (RGB(240, 240, 240));
   pDC->TextOut (1001, 0, L"0");
   // Vertical axis
   pDC->MoveTo (0, -400);
   pDC->LineTo (0, 400);
}

I now want to avoid flickering with CMemDC. However, i can't get it to work right. Can somebody show me how to implement CMemDC right?

Thanks

Andreas D.
  • 73
  • 1
  • 12

2 Answers2

3

You need to load the memory DC with a bitmap to then BitBlt to the screen DC.

Try something like:

CDC dcMem;
CBitmap bitmap;

dcMem.CreateCompatibleDC( pDC );
bitmap.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height())
CBitmap* pOldBitmap = dcMem.SelectObject(&bitmap);

... DO ALL YOUR DRAWING TO dcMem ...

pDC->BitBlt(rect.left, rect.top, rect.Width(), rect.Height(), &dcMem, 0, 0, SRCCOPY);

dcMem.SelectObject(pOldBitmap)
snowdude
  • 3,854
  • 1
  • 18
  • 27
1

To avoid flickers you should draw everything on CMemDC and BitBlt it to the real DC.

Secondly add windows message handler for WM_ERASEBKGND message and change the code to

BOOL CGraph::OnEraseBkgnd(CDC* pDC) 
{
    return TRUE;
}
Jeeva
  • 4,585
  • 2
  • 32
  • 56
  • I have changed the OnEraseBkgnd () before. Didn't change anything yet. Can you provide an example of CMemDC and BitBlt for my code? I tried it this way but don't really get the principle. Thanks. – Andreas D. Jul 12 '12 at 09:29
  • http://www.codeproject.com/Articles/2432/Do-a-flicker-free-drawing-using-MFC-methods – Jeeva Jul 12 '12 at 09:31
  • I still don't know how to use it in the DrawCoordinateSystem () function. It doesn't give any output. – Andreas D. Jul 12 '12 at 11:46