title: CScrollBar/MFC: flickering while move the thumb slider
body: <p>I implemented a <strong>MFC application</strong> which displays a chart. There are data points (e.g. 2000) where a subset of them will be displayed in the chart. This subset must be changed with a scroll bar. So I added in my dialog class the <code>CScrollBar</code> class and listen to the <code>ON_WM_HSCROLL</code> event which calls the <code>OnHScroll</code> method.</p>
  
  <pre><code>void CChartWithRealTimeDataDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
  {
      UINT nCurrentPos;
  
      nCurrentPos = pScrollBar-&gt;GetScrollPos();
  
      // nCurrentPos = m_X_Axis_Scrollbar-&gt;GetScrollPos();
  
      bool bDoRepaint = false;
  
      switch (nSBCode) 
      {
      //case SB_THUMBPOSITION:
      case SB_THUMBTRACK:
              nCurrentPos = nPos;
              pScrollBar-&gt;SetScrollPos (nPos);
              bDoRepaint = true;
          break;
  
      case SB_LINELEFT:
  
              nCurrentPos = (UINT)(std::max( (int)0, (int)nCurrentPos-1));
              pScrollBar-&gt;SetScrollPos (nCurrentPos);
              bDoRepaint = true;
          break;
  
      case SB_LINERIGHT:
              nCurrentPos = std::min((UINT)(m_ChartControl.getTotalEnd()), nCurrentPos+1);
              pScrollBar-&gt;SetScrollPos (nCurrentPos);
              bDoRepaint = true;
          break;
  
      case SB_PAGELEFT:
          nCurrentPos = (UINT)(std::max( (int)0, (int)nCurrentPos-10));
          pScrollBar-&gt;SetScrollPos (nCurrentPos);
          bDoRepaint = true;
          break;
  
      case SB_PAGERIGHT:
              nCurrentPos = std::min( (UINT)(m_ChartControl.getTotalEnd()), nCurrentPos+10);
              pScrollBar-&gt;SetScrollPos (nCurrentPos);
              bDoRepaint = true;
          break;
      }
  
      if ( bDoRepaint )
      {
          m_ChartControl.setShift(nCurrentPos);
          Invalidate();
      }
  
      CDialogEx::OnHScroll(nSBCode, nPos, pScrollBar);
  }
  </code></pre>
  
  <p>If the user moves the thumb slider every time the whole window will be repainted and a flickering begins.</p>
  
  <p>Is there a way to avoid the flickering?</p>
  
  <p>Thanks,</p>
  
  <p>Thomas</p>