title: MFC string width without CDC
body: <p>Is there any way to get the width of a string in pixels without using CDC or using a CDC not linked with a display. The class that needs to retrieve the string width does not inherit from CWnd in order to use CWnd::GetDC() and there is no way to pass an existing CDC to the function.</p>
  
  <p>I have tried to create a dummy CDC that is not linked with a display, however this causes MFC to crash. Ideally something like:</p>
  
  <pre><code>m_font = new CFont();
  m_font-&gt;CreatePointFont(size * 10, _T("Arial"));
  
  m_tempCDC = new CDC();
  m_tempCDC-&gt;SelectObject(m_font);
  
  return m_tempCDC-&gt;GetOutputTextExtent(_T("Test")).cx;
  </code></pre>
  
  <p>EDIT: Should have substituted the font name variable for a string literal.</p>