title: How to call OnEraseBkgnd() to repaint the window in another function? (MFC)
body: <p>I am trying to call <code>OnEraseBkgnd()</code> to repaint the window in another function.</p>
  
  <p>For example, like the code below:</p>
  
  <pre><code>...
  
  CBitmap Background;
  BITMAP bm;
  CDC dcMemory;
  int width, height;
  
  ...
  
  BOOL CSplashDlg::OnInitDialog()
  {
      CDialog::OnInitDialog();
  
      CDC* dc;
  
      Background.LoadBitmap(IDB_COVER);   //Load Bitmap
      Background.GetBitmap(&amp;bm);      //Load Bitmap into handle
  
      width = 0;
      height = 0;
  
      while(width &lt; 300)
      {
          width += 10;
          height += 10;
          OnEraseBkgnd(dc);       //&lt;--- Here I want to call OnEraseBkgnd()
  
          Sleep(5);           //Delay 5 millisecond
      }
  
      return TRUE;
  }
  
  
  
  BOOL CSplashDlg::OnEraseBkgnd(CDC* pDC)
  {
      ///////////////////////////////////
        Invalidate();             //I don't know where I should put this function
      ///////////////////////////////////
  
      dcMemory.CreateCompatibleDC(pDC);
      CBitmap *pOldbmp = dcMemory.SelectObject(&amp;Background);
  
      pDC-&gt;SetStretchBltMode(HALFTONE);
  
      pDC-&gt;StretchBlt(0, 0, width, height, &amp;dcMemory, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
  
      return TRUE;
  }
  </code></pre>
  
  <p>In the above example, I want to call <code>OnEraseBkgnd()</code> to repaint the window while executing inside <code>OnInitDialog()</code>. I have searched the Internet and it always says using <code>Invalidate();</code> or <code>RedrawWindow();</code> to repaint it and it'll also call the <code>OnEraseBkgnd()</code> function. The question is: How should I use the <code>Invalidate();</code> or the <code>RedrawWindow();</code>? Where should I put those function? </p>
  
  <p>I have put those two functions in anywhere but it still not works.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I have modified it. Though now <code>Invalidate()</code> and <code>UpdateWindow()</code> are success to call the <code>OnEraseBkgnd()</code> function. But I found another problem: Why didn't the <code>StretchBlt</code> work when I used <code>Invalidate()</code> or <code>UpdateWindow()</code> to repaint it but did the <code>FillSolidRect</code> work?</p>
  
  <pre><code>...
  
  BOOL CMainDlg::OnInitDialog()
  {
      CSplashDlg Frame;
  
      Frame.width = 0;
      Frame.height = 0;
  
      while(Frame.width &lt;= 300)
      {
          Frame.width += 10;
          Frame.height += 10;
  
          Frame.Invalidate();     //&lt;---Here I use both Invalidate() and UpdateWindow()
          Frame.UpdateWindow();       //&lt;---Here I use both Invalidate() and UpdateWindow()
  
          Sleep(5);           //Delay 5 millisecond
      }
  
      CDialog::OnInitDialog();
  
      return TRUE;
  }
  
  BOOL CSplashDlg::OnInitDialog()
  {
      CDialog::OnInitDialog();
  
      width = 0;
      height = 0;
  
      Background.LoadBitmap(IDB_COVER);   //Load Bitmap
      Background.GetBitmap(&amp;bm);      //Load Bitmap into handle
  
  
      return TRUE;
  }
  
  
  
  BOOL CSplashDlg::OnEraseBkgnd(CDC* pDC)
  {
      dcMemory.CreateCompatibleDC(pDC);
      CBitmap *pOldbmp = dcMemory.SelectObject(&amp;Background);
  
      ///////////////////////////////////////////////////////////////
      pDC-&gt;SetStretchBltMode(HALFTONE);
      pDC-&gt;StretchBlt(0, 0, width, height, &amp;dcMemory, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);        //It doesn't work when using this one (StretchBlt)
      ///////////////////////////////////////////////////////////////
      pDC-&gt;FillSolidRect(0, 0, width, height, RGB(255,0,0));                      //But it works when using this one (FillSolidRect)
      ///////////////////////////////////////////////////////////////
  
      return TRUE;
  }
  
  ...
  </code></pre>