Friday, February 5, 2016

MFC OnPaint refresh draw Flickering

The best way to stop flickering is to have another member variable, and to redraw only when necessary. For instance, I chance the cursor every now but that can cause significant redrawing. Using an extra variable, I only redraw when necessary.

void COGraphCtrl::OnPaint()
{
   if(m_RePaint)
   {
           CPaintDC dc(this);

      dc.FillSolidRect(&clientRect,RGB(255,255,255));

1 comment:

  1. If that doesn't work, try

    -----------------------
    void COGraphCtrl::OnPaint()
    {
    CPaintDC dc(this);
    GetClientRect(&clientRect);
    GetClientRect(&graphRect);
    graphRect.DeflateRect(marginRect);

    //Get the X XNorm and offset
    xNorm = (graphRect.Width() / (maxXVal - minXVal));//%%%%
    xOffset = -minXVal + (marginRect.left / xNorm);
    int xN = ((int)xNorm);
    m_PointRectWidth = xN<=1?1:xN>4?4:xN;//allowing width 1 - 4

    //Get the Y YNorm and offset
    yNorm = (graphRect.Height() / (maxYVal - minYVal));
    yOffset = -(minYVal);

    // Create memory dc and update it's mapping state
    CDC memDC;
    memDC.CreateCompatibleDC(&dc);
    memDC.SetMapMode(dc.GetMapMode());
    memDC.SetViewportOrg(dc.GetViewportOrg());
    memDC.IntersectClipRect(clientRect);

    // Create memory bitmap
    CBitmap bmp;
    bmp.CreateCompatibleBitmap(&dc, clientRect.Width(), clientRect.Height());
    CBitmap* pOldBmp = memDC.SelectObject(&bmp);

    //Draw
    Draw(&memDC);
    //Blit to the original dc

    dc.BitBlt(clientRect.left, clientRect.top, clientRect.Width(), clientRect.Height(), &memDC, clientRect.left, clientRect.top, SRCCOPY);

    // Restore old bitmap
    memDC.SelectObject(pOldBmp);
    if(!m_FullyLoaded)
    m_FullyLoaded = true;
    }

    ReplyDelete