title: Calculate font "size" in DIPs of a system font
body: <p>I have few <code>BS_OWNERDRAWN</code> buttons and using Direct2D and Direct Write to draw them.</p>
  <p>I also need to draw button text within button rectangle, for this I use <code>IDWriteTextFormat</code> which requires to specify &quot;font size&quot; in DIPs (device independent pixels).</p>
  <p>I want font size in those buttons to be of same size as other non owner drawn common controls or same as system font that is present in window caption bar.</p>
  <p>Following code is &quot;chopped out&quot; version to present my workaround which of course doesn't give expected results because I get <code>LOGFONT</code> structure of a font in caption bar which gives me the width of a font (single character) but not font size that the <code>IDWriteTextFormat</code> expects to specify font size in DIPs.</p>
  <pre><code>class CustomControl
  {
  protected:
      /** Caption text format used to draw text */
      CComPtr&lt;IDWriteTextFormat&gt; mpTextFormat;
  
      /** Caption font size (button text size) */
      float mFontSize;
  };
  
  // Calculate caption bar (default) font size of a top level window
  void CustomControl::CalculateFontSize()
  {
      NONCLIENTMETRICSW metrics;
      metrics.cbSize = sizeof(NONCLIENTMETRICSW);
      SystemParametersInfoW(SPI_GETNONCLIENTMETRICS, metrics.cbSize, &amp;metrics, 0);
  
      LOGFONTW font = metrics.lfCaptionFont;
      mFontSize = static_cast&lt;float&gt;(font.lfHeight);
  }
  
  // Create text format that is of same font size as default system font
  HRESULT CustomControl::CreateTextFormat()
  {
      HRESULT hr = S_OK;
  
      if (!mpTextFormat)
      {
          CalculateFontSize();
  
          hr = mpWriteFactory-&gt;CreateTextFormat(
              L&quot;Arial&quot;,
              NULL,
              DWRITE_FONT_WEIGHT_NORMAL,
              DWRITE_FONT_STYLE_NORMAL,
              DWRITE_FONT_STRETCH_NORMAL,
              mFontSize,  // &lt;-- Specifies font size in DIP's..
              L&quot;en-us&quot;,
              &amp;mpTextFormat);
      }
  
      return hr;
  }
  </code></pre>
  <p>Here is test program that shows the differences in font size between default system font in window caption and custom button text below.</p>
  <p><a href="https://i.stack.imgur.com/NBZD0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NBZD0.png" alt="enter image description here" /></a></p>
  <p>I need help in figuring out how to correctly calculate font size for <code>IDWriteTextFormat</code> parameter to be of same size as text in other common controls that are not <code>BS_OWNERDRAW</code> or in this example default font in window caption.</p>
  <p><strong>EDIT:</strong></p>
  <p>I figured out issue is in my <code>CalculateFontSize()</code> I wrote <code>mFontSize = static_cast&lt;float&gt;(font.lfHeight);</code> but this is negative number so appending <code>-</code> sign gives the expected result:</p>
  <p><code>mFontSize = static_cast&lt;float&gt;(-font.lfHeight);</code></p>
  <p>Why negative? I'm not sure yet but this answer helped:</p>
  <p><a href="https://stackoverflow.com/questions/52085740/how-to-set-font-size-using-createfonta">How to set font size using CreateFontA?</a></p>
  <p>Now my question remains in that how should I update <code>CalculateFontSize()</code> so that it gets font size of common controls that are not <code>BS_OWNERDRAW</code> instead of a window caption bar font size?</p>