0

How to get the width and height of a specified number of characters, font Size, font type and font style?

Examples:

font size = 14 <br>
font type = "Times New Roman" <br>
font style = "Regular"<br>
No of Characters = 50<br>

What is the width and height?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
shubha
  • 1
  • 1

2 Answers2

1

In C++, look at the "GetTextExtent" functions. Example:

CDC * dc = GetDC();
dc->SelectObject(GetFont());//select the font you want to measure the text in
CSize size = dc->GetTextExtent(chars);//get the dimensions
size.cx;//width
size.cy;//height

For C#, use MeasureString

mdw7326
  • 434
  • 7
  • 28
1

For Windows GDI font/display, use DrawText with the DT_CALCRECT flag in the uFormat parameter. It will calculate and return the bounding box for the string you provide using the font currently selected in the HDC.

dthorpe
  • 35,318
  • 5
  • 75
  • 119
  • i am using mfc dialog based application,so i have no idea how to calculate width and height , please give me an example. – shubha Apr 05 '10 at 05:55
  • In MFC, DrawText is a member of the CDC class. See MSDN for details: http://msdn.microsoft.com/en-us/library/a6x7y2a4.aspx – dthorpe Apr 05 '10 at 08:35
  • @dthorpe, while that link is helpful and provides useful documentation, it does not show an example of how the function is regularly used. If you know how, would you be kind enough to post a simple example? – mdw7326 Sep 24 '14 at 17:35