title: SelectObject on memory DC
body: <p>In my program I have a memory DC to which I frequenty draw, then in response to <code>WM_PAINT</code> I quickly <code>BitBlt</code> it to the screen. I've always done the drawing like this, the memory DC is created in response to <code>WM_CREATE</code> btw.</p>
  
  <pre><code>void Draw()
  {
      HFONT hPrevFont = (HFONT)SelectObject(m_hMemDC, m_hFont);
      // draw text
      SelectObject(m_hMemDC, hPrevFont);
  }
  </code></pre>
  
  <p>Which should be correct, I've also found out now that I can use <code>SaveDC</code> and <code>RestoreDC</code> for that (especially useful when having a lot of GDI objects)</p>
  
  <pre><code>void Draw()
  {
      int nSavedDC = SaveDC(m_hMemDC);
      SelectObject(m_hMemDC, m_hFont);
      // draw text
      RestoreDC(m_hMemDC, nSavedDC);
  }
  </code></pre>
  
  <p>Now I wonder whether it is ok to just save the state of the DC after I created it, and restore it when I'm deleting it. Like this:</p>
  
  <pre><code>int OnCreate(...)
  {
      m_hMemDC = CreateMemDC();
      m_nSavedDC = SaveDC(m_hMemDC);
  }
  
  void Draw()
  {
      SelectObject(m_hMemDC, m_hFont);
      // draw text
  }
  
  void OnDestroy()
  {
      RestoreDC(m_hMemDC, m_nSavedDC);
      DeleteMemDC(m_hMemDC);
  }
  </code></pre>
  
  <p>This should be alright I think, It's not like anyone else is using my memory DC in the mean time. What do you think? </p>
  
  <p>Heck, is it even necessary to save and restore the DC at all in this case?</p>
  
  <p>EDIT: Yes, I do realize I should delete the memory DC first before deleting the font (because otherwise it would still be used by the memory DC).</p>