title: Measure ToolStripItem in custom ToolStrip renderer
body: <p>I have a WinForms app for .NET Framework in which the text of items in context menus based on the the <a href="https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.contextmenustrip" rel="nofollow noreferrer">ContextMenuStrip</a> component are drawn using <a href="https://docs.microsoft.com/en-us/dotnet/api/system.drawing.graphics.drawstring" rel="nofollow noreferrer">Graphics.DrawString()</a> to provide consistent look with other interface elements. The core part of the custom renderer looks like this:</p>
  
  <pre class="lang-c# prettyprint-override"><code>private class CustomContextMenuRenderer : ToolStripProfessionalRenderer
  {
      private static StringFormat fStringFormatLeft = new StringFormat()
      { Alignment = StringAlignment.Near, LineAlignment = StringAlignment.Center};
      private static StringFormat fStringFormatRight = new StringFormat()
      { Alignment = StringAlignment.Far, LineAlignment = StringAlignment.Center };
          
      protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
      {
          StringFormat sf = (e.TextFormat &amp; TextFormatFlags.Right) == 0 ? fStringFormatLeft : fStringFormatRight;
          using (SolidBrush brush = new SolidBrush(e.Item.ForeColor))
          {
              e.Graphics.DrawString(e.Text, e.TextFont, brush, e.TextRectangle, sf);
          }
      }
  }
  </code></pre>
  <p>For some fonts, the height of menu items calculated by the ContextMenuStrip component is not enough to display item text. Most likely, this happens because the standard implementation is based on the <a href="https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.textrenderer" rel="nofollow noreferrer">TextRenderer</a> class to output item texts. Is there a way to measure and tell the component the expected size of the item if we use GDI+ to render item texts?</p>