title: MFC CListCtrl CustomDraw() event handler code getting stuck
body: <p>I'm currently experiencing a problem with my OnNMCustomdrawlistctrlvalues() event handler being called non-stop once I've painted a CListCtrl row red (code gets stuck), causing my application to freeze once I try to perform another event, such as clicking a button.</p>
  
  <p>I have the following code:</p>
  
  <pre><code>void CSPID_FQA_Test_ClientDlg::OnNMCustomdrawlistctrlvalues(NMHDR *pNMHDR, LRESULT *pResult)
  {
      LPNMLVCUSTOMDRAW lpLVCustomDraw = reinterpret_cast&lt;LPNMLVCUSTOMDRAW&gt;(pNMHDR);
      int itemCnt = 0;
      CString text;
      RECT rc;
  
      switch(lpLVCustomDraw-&gt;nmcd.dwDrawStage)
      {
      case CDDS_ITEMPREPAINT:
      case CDDS_ITEMPREPAINT | CDDS_SUBITEM:
          //get each row text for 2nd column
          itemCnt = mListCtrl.GetItemCount();
          for (int i = 0; i &lt; itemCnt; i++)
          {
              text = mListCtrl.GetItemText(i, 2);
              if (text.Compare("No") == 0)
              {
                  if (i == (lpLVCustomDraw-&gt;nmcd.dwItemSpec))
                  {
                      lpLVCustomDraw-&gt;clrTextBk = RGB(255,50,50);
                      mListCtrl.GetItemRect(i,&amp;rc,LVIR_BOUNDS);
                      mListCtrl.InvalidateRect(&amp;rc, 0);
                  }
              }
          }
          break;
      default: break;
      }
  
      *pResult = 0;
      *pResult |= CDRF_NOTIFYITEMDRAW;
      *pResult |= CDRF_NOTIFYSUBITEMDRAW;
      *pResult |= CDRF_NOTIFYPOSTPAINT;
  }
  </code></pre>
  
  <p>Which results in:</p>
  
  <p><img src="https://i.stack.imgur.com/UlzDT.png" alt="result1"></p>
  
  <p>Within the CListCtrl, if I scroll to an area which isn't painted red, my application is fine.</p>
  
  <p>I'm not too sure how to stop this from happening... maybe I need an alternative way of dynamically painting a row red?</p>
  
  <hr>
  
  <p><strong>EDIT:</strong> I have changed my code to the below, but am experiencing this issue (only 2nd and 3rd column being coloured, but when I redraw the list control i.e. scroll down then back up, all columns are then drawn):</p>
  
  <p><img src="https://i.stack.imgur.com/AhEBY.png" alt="result2"></p>
  
  <pre><code>void CSPID_FQA_Test_ClientDlg::OnNMCustomdrawlistctrlvalues(NMHDR *pNMHDR, LRESULT *pResult)
  {
      LPNMLVCUSTOMDRAW lpLVCustomDraw = reinterpret_cast&lt;LPNMLVCUSTOMDRAW&gt;(pNMHDR);
      int itemCnt = 0;
      CString text;
      RECT rc;
  
      CDC* pDC = CDC::FromHandle (lpLVCustomDraw-&gt;nmcd.hdc);
  
      switch(lpLVCustomDraw-&gt;nmcd.dwDrawStage)
      {
      case CDDS_ITEMPOSTPAINT:
      //case CDDS_ITEMPREPAINT:
      //case CDDS_ITEMPREPAINT | CDDS_SUBITEM:
          //get each row text for 2nd column
          itemCnt = mListCtrl.GetItemCount();
          for (int i = 0; i &lt; itemCnt; i++)
          {
              text = mListCtrl.GetItemText(i, 2);
              if (text.Compare("No") == 0)
              {
                  if (i == (lpLVCustomDraw-&gt;nmcd.dwItemSpec))
                  {
                      //lpLVCustomDraw-&gt;clrTextBk = RGB(255,50,50);
                      mListCtrl.GetItemRect(i,&amp;rc,LVIR_BOUNDS);
                      pDC-&gt;FillSolidRect (&amp;rc, RGB (0, 255, 0));
  
                      //mListCtrl.InvalidateRect(&amp;rc, 0);
                  }
              }
          }
          break;
      default: break;
      }
  
      *pResult = 0;
      *pResult |= CDRF_NOTIFYITEMDRAW;
      *pResult |= CDRF_NOTIFYSUBITEMDRAW;
      *pResult |= CDRF_NOTIFYPOSTPAINT;
  }
  </code></pre>
  
  <hr>
  
  <p><strong>EDIT2:</strong> I changed to the below code, but am still getting the same issue:</p>
  
  <pre><code>LPNMLVCUSTOMDRAW lpLVCustomDraw = reinterpret_cast&lt;LPNMLVCUSTOMDRAW&gt;(pNMHDR);
  CString text;
  RECT rc;
  
  CDC* pDC = CDC::FromHandle (lpLVCustomDraw-&gt;nmcd.hdc);
  
  switch(lpLVCustomDraw-&gt;nmcd.dwDrawStage)
  {
  case CDDS_ITEMPOSTPAINT:
  
      mListCtrl.GetItemRect(lpLVCustomDraw-&gt;nmcd.dwItemSpec,&amp;rc,LVIR_BOUNDS);
      text = mListCtrl.GetItemText(lpLVCustomDraw-&gt;nmcd.dwItemSpec, 2);
  
      if (text.Compare("No") == 0)
      {
          pDC-&gt;FillSolidRect (&amp;rc, RGB(0, 0, 255));
      }
  
      break;
  default: break;
  }
  </code></pre>
  
  <p><img src="https://i.imgur.com/UU2Rqo9.png" alt="result3"></p>