calc button size needed to show its text

  • Thread starter Thread starter Steve Richter
  • Start date Start date
S

Steve Richter

is there a "GetTextExtentPoint32" equivalent in Windows Form or Font class?

from the button text I would like to calculate the size that the button needs to be.

thanks,

-Steve
 
Steve,

Check out the Graphics.MeasureCharacterRanges method which is generally
recognized as a more accurate way to measure a string than
Graphics.MeasureString.

Here is a snip of code that may push you in the right direction:

StringFormat textFormat = new StringFormat(StringFormat.GenericTypographic);

textFormat.SetMeasurableCharacterRanges(new CharacterRange[] { new
CharacterRange(0, this.Text.Length()) } );

Region characterRegion = graphics.MeasureCharacterRanges(this.Text,
this.Font, new RectangleF(0, 0, float.MaxValue, float.MaxValue),
textFormat)[0];

Then you can convert the region to a rect if you like.

Byron Cullen
ThunderTools
 
Back
Top